Class: YahooApiDAService

Inherits:
Object
  • Object
show all
Defined in:
lib/yahoo_api.rb

Constant Summary collapse

REQUEST_HOST =
'jlp.yahooapis.jp'
API_PATH =
'/DAService/V1/parse'

Instance Method Summary collapse

Constructor Details

#initialize(app_id, sentence) ⇒ YahooApiDAService

Returns a new instance of YahooApiDAService.



10
11
12
13
14
15
# File 'lib/yahoo_api.rb', line 10

def initialize app_id, sentence
  @app_id   = app_id
  @sentence = sentence
  request
  self
end

Instance Method Details

#parsed_arrayObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/yahoo_api.rb', line 29

def parsed_array
  result = Array.new
  xml_document.elements.each('ResultSet/Result/ChunkList/Chunk') do |chunk|
    chunk.elements.each('MorphemList/Morphem') do |morphem|
      result << {
        'POS'     => morphem.elements['POS'].text,
        'Surface' => morphem.elements['Surface'].text,
      }
    end
  end
  result
end

#requestObject



17
18
19
20
21
22
# File 'lib/yahoo_api.rb', line 17

def request
  http  = Net::HTTP.new(REQUEST_HOST,80)
  query = "?appid=#{@app_id}&sentence=#{CGI.escape(@sentence)}"
  req   = Net::HTTP::Get.new(API_PATH + query)
  @response = http.request(req)
end

#xml_documentObject



24
25
26
27
# File 'lib/yahoo_api.rb', line 24

def xml_document
  @xml_doc ||= REXML::Document.new @response.body
  @xml_doc
end