Guest User

Untitled

a guest
Jun 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. # -*- encoding: utf-8 -*-
  2. require 'rexml/document'
  3. require 'net/http'
  4.  
  5. class Keyphrase
  6. def initialize
  7. path = File.join(File.dirname(__FILE__), "..", "config", "yahooapis.yml")
  8. config = YAML::load(File.open(path))
  9. @app_id = config['app_id']
  10. end
  11.  
  12. def split(text)
  13.  
  14. host = 'jlp.yahooapis.jp'
  15. request = "/KeyphraseService/V1/extract?appid=#{@app_id}"
  16.  
  17. http = Net::HTTP.new(host)
  18. response = http.post(request, "sentence=#{text}")
  19.  
  20. result = {}
  21. xml = REXML::Document.new response.body
  22. xml.elements.each("/ResultSet/Result") do |item|
  23. key = item.elements['Keyphrase'].text
  24. v = item.elements['Score'].text
  25. result[key] = v
  26. end
  27. result
  28. end
  29.  
  30. end
Add Comment
Please, Sign In to add comment