-
-
Save kwappa/717641 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
require 'test/unit' | |
require File.join(File.dirname(File.expand_path(__FILE__)), '..', 'lib', 'zenra') | |
class ZenraTest < Test::Unit::TestCase | |
def setup | |
@appid = ENV['YAHOO_APPID'] | |
end | |
def test_zenrize_verb | |
zenra = Zenra.new(@appid) | |
assert_equal('お腹が全裸で空きました', | |
zenra.zenrize('お腹が空きました')) | |
end | |
def test_zenrize_noun | |
zenra = Zenra.new(@appid, '名詞', '夜の') | |
assert_equal('夜のお腹が空いたので夜のスパゲッティが食べたい', | |
zenra.zenrize('お腹が空いたのでスパゲッティが食べたい')) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
require 'open-uri' | |
require 'rexml/document' | |
class Zenra | |
def initialize(appid, position='動詞', text='全裸で', base_url='http://jlp.yahooapis.jp/MAService/V1/parse') | |
@appid = appid | |
@position = position | |
@text = text | |
@base_url = base_url | |
end | |
def zenrize(sentence) | |
raise "appid is necessary!" if @appid.nil? || @appid.empty? | |
raise "Japanese sentence is necessary!" if sentence.nil? || sentence.empty? | |
req = "#{@base_url}?results=ma&appid=#{@appid}&sentence=#{URI.encode(sentence)}" | |
res = open(req) | |
xml = REXML::Document.new(res.read) | |
return sentence if xml.elements['ResultSet/ma_result/total_count'].text == '0' | |
result = '' | |
xml.elements.each('ResultSet/ma_result/word_list/word') do |e| | |
result << @text if e.elements['pos'].text == @position | |
result << e.elements['surface'].text | |
end | |
result | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment