Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@yohfee
Created November 24, 2010 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yohfee/713759 to your computer and use it in GitHub Desktop.
Save yohfee/713759 to your computer and use it in GitHub Desktop.
# 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
# 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