Skip to content

Instantly share code, notes, and snippets.

@sh4869
Created August 29, 2014 01:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sh4869/d0530539c67f525b6f88 to your computer and use it in GitHub Desktop.
Save sh4869/d0530539c67f525b6f88 to your computer and use it in GitHub Desktop.
RubyでYahoo日本語形態解析APIを使って文章中から名詞を取り出すサンプル
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