-
Notifications
You must be signed in to change notification settings - Fork 1
/
semantic_hacker.rb
88 lines (68 loc) · 2.03 KB
/
semantic_hacker.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require 'rubygems'
require 'hpricot'
require 'cgi'
require 'open-uri'
class SemanticHacker
URL = "http://api.semantichacker.com"
attr_accessor :token, :doc, :content, :content_type, :content
attr_reader :api_call
def initialize(token)
@token = token
@signatures = YAML::load(File.open(File.join(File.dirname(__FILE__), 'db/signatures.yml')).read)
end
def query_for(content_type, content)
@content_type = content_type # uri or content
@content = sanitize(content)
self
end
def get_signatures
@api_call = "#{URL}/#{@token}/signature?#{@content_type}=#{@content}"
@doc = Hpricot.XML(open(@api_call))
signatures
end
def get_concepts
@api_call = "#{URL}/#{@token}/concept?#{@content_type}=#{@content}"
@doc = Hpricot.XML(open(@api_call))
concepts
end
def get_categories
@api_call = "#{URL}/#{@token}/category?#{@content_type}=#{@content}&showLabels=true"
@doc = Hpricot.XML(open(@api_call))
categories
end
private
def type
(doc/:response/:about/:systemType).inner_html
end
def config_id
(doc/:response/:about/:configId).inner_html
end
def categories
response = []
(doc/:response/:categorizer/:categorizerResponse/:categories/:category).each do |item|
response << {:label => item.attributes['label'], :weight => item.attributes['weight']}
end
response
end
def concepts
response = []
(doc/:response/:conceptExtractor/:conceptExtractorResponse/:concepts/:concept).each do |item|
response << {:label => item.attributes['label'], :weight => item.attributes['weight']}
end
response
end
def signatures
response = []
(doc/:response/:siggen/:siggenResponse/:signature/:dimension).each do |item|
response << {:index => item.attributes['index'], :weight => item.attributes['weight'], :label => @signatures[item.attributes['index']]}
end
response
end
def sanitize(content)
if @content_type == :uri
return content
else
return ::CGI::escape(content)
end
end
end