swdyh (owner)

Revisions

gist: 198829 Download_button fork
public
Public Clone URL: git://gist.github.com/198829.git
Embed All Files: show embed
yks.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
 
require 'net/http'
require 'rubygems'
require 'json'
 
class YKS
  def initialize appid
    @appid = appid
  end
 
  def extract str
    if str.size > 1024 * 100
      raise 'size over'
    end
 
    uri = URI 'http://jlp.yahooapis.jp/KeyphraseService/V1/extract'
    res = Net::HTTP.post_form uri, :appid => @appid,
      :sentence => str, :output => 'json'
    json = JSON.parse res.body
    json.map { |k, v| { :keyphrase => k, :score => v } }.sort_by { |i| i[:score] }.reverse
  end
end
 
# A wrapper of Yahoo Keyphrase extract
# http://developer.yahoo.co.jp/webapi/jlp/keyphrase/v1/extract.html
#
# require 'pit'
# yks = YKS.new Pit.get('yahoo_web_service')['appid']
# r = yks.extract '東京ミッドタウンから国立新美術館まで歩いて5分で着きます'
# r.each { |i| puts i[:keyphrase] }