Created
August 29, 2014 01:26
-
-
Save sh4869/d0530539c67f525b6f88 to your computer and use it in GitHub Desktop.
RubyでYahoo日本語形態解析APIを使って文章中から名詞を取り出すサンプル
This file contains 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
require 'net/http' | |
require 'uri' | |
require 'rexml/document' | |
id = 'your appid' | |
text = '庭には二羽ニワトリがいる' | |
response = Net::HTTP.post_form(URI.parse('http://jlp.yahooapis.jp/MAService/V1/parse'), | |
{'appid'=> id, | |
'sentence' => text, | |
'results' => 'ma'}) | |
xml = REXML::Document.new(response.body) | |
word_array = [] | |
xml.elements.each('ResultSet/ma_result/word_list/word') do |element| | |
if element.elements['pos'].text == "名詞" | |
word_array << element.elements['surface'].text | |
end | |
end | |
word_array.each{|word| puts word} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment