textrazor 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 17c7396459d35be185762cdd33e48422c550382f
4
- data.tar.gz: 5162e912ea4c29574f42a291c08945d4be062f03
3
+ metadata.gz: 5a18bfa0d8979cd6af68c481ff1200802d989dfc
4
+ data.tar.gz: 71666356571e6e4d6cad269ad6f7f6da306d90d0
5
5
  SHA512:
6
- metadata.gz: e77134b8e36ed76bd5c779100cb203b2542cebd86d436d7054bb64b695f06aa1509077d811b838246cf86531cd90134562e9d6f225cf83b5e7e7d9b8ab797c50
7
- data.tar.gz: 18b6342068846a68bd645599fb2a6c55da017320a1fe2d11cf7386ea192d4998431368cdd3acf72d360632fe9518dfe3f1b5e38af7baf774975b2df3b9c626f4
6
+ metadata.gz: 653c8e38cf0d1ab4b42796eec8853bc1104c5122ba6e87e6073f74fb2e75b76b75911136a2583f0f0320ab93711da5d269a6ee67280a6c90b3884ca1b7c24961
7
+ data.tar.gz: c65e0182196f36cf12a080772dd4953ec8d239a6bc66cd3897ef97e2bb83c8bdea5823ef14c91e76d0249980628ca762ae734f1d445c3238f6a5de86c88bc990
data/README.md CHANGED
@@ -48,6 +48,8 @@ response.entities # Returns an array of TextRazor::Entity instances.
48
48
 
49
49
  response.words # Returns an array of TextRazor::Word instances.
50
50
 
51
+ response.phrases # Returns an array of TextRazor::Phrase instances.
52
+
51
53
  ```
52
54
 
53
55
  ### One off requests
@@ -65,11 +67,13 @@ TextRazor.entities('api_key', 'text')
65
67
 
66
68
  TextRazor.words('api_key', 'text')
67
69
 
70
+ TextRazor.phrases('api_key', 'text')
71
+
68
72
  ```
69
73
 
70
74
  ## Next steps
71
75
 
72
- Only implemented this for topics, entities, and words. Also, implement
76
+ Only implemented this for topics, entities, words and phrases. Also, implement
73
77
  it for other information that we can retrieve from the public API.
74
78
 
75
79
 
data/Rakefile CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'bundler/gem_tasks'
2
+ require 'bundler/setup'
2
3
  require 'rspec/core/rake_task'
3
4
 
4
5
  RSpec::Core::RakeTask.new(:spec)
@@ -1,11 +1,13 @@
1
1
  require "textrazor/version"
2
2
  require "textrazor/configuration"
3
+ require "textrazor/util"
3
4
  require "textrazor/client"
4
5
  require "textrazor/request"
5
6
  require "textrazor/response"
6
7
  require "textrazor/topic"
7
8
  require "textrazor/entity"
8
9
  require "textrazor/word"
10
+ require "textrazor/phrase"
9
11
 
10
12
  module TextRazor
11
13
 
@@ -21,6 +23,10 @@ module TextRazor
21
23
  Client.words(api_key, text, options)
22
24
  end
23
25
 
26
+ def self.phrases(api_key, text, options = {})
27
+ Client.phrases(api_key, text, options)
28
+ end
29
+
24
30
  class << self
25
31
  attr_writer :configuration
26
32
  end
@@ -9,7 +9,8 @@ module TextRazor
9
9
  DEFAULT_EXTRACTORS = ['entities', 'topics', 'words', 'phrases', 'dependency-trees',
10
10
  'relations', 'entailments', 'senses']
11
11
 
12
- REQUEST_OPTIONS = [:extractors, :cleanup_html, :language, :filter_dbpedia_types, :filter_freebase_types]
12
+ REQUEST_OPTIONS = [:extractors, :cleanup_html, :language,
13
+ :filter_dbpedia_types, :filter_freebase_types]
13
14
 
14
15
  attr_reader :response, :api_key, :request_options
15
16
 
@@ -49,6 +50,12 @@ module TextRazor
49
50
  entities
50
51
  end
51
52
 
53
+ def self.phrases(api_key, text, options = {})
54
+ new(api_key, options.merge(extractors: ['phrases', 'words'])).
55
+ analyse(text).
56
+ phrases
57
+ end
58
+
52
59
  private
53
60
 
54
61
  def assign_api_key(api_key)
@@ -2,6 +2,8 @@ module TextRazor
2
2
 
3
3
  class Entity
4
4
 
5
+ extend Util
6
+
5
7
  attr_reader :id, :type, :matching_tokens, :entity_id, :freebase_types, :confidence_score,
6
8
  :wiki_link, :matched_text, :freebase_id, :relevance_score, :entity_english_id,
7
9
  :starting_pos, :ending_pos
@@ -14,19 +16,10 @@ module TextRazor
14
16
  end
15
17
 
16
18
  def self.create_from_hash(params)
17
- params = Hash[params.map {|k, v| [underscore(k), v] }]
19
+ params = Hash[params.map {|k, v| [standardize(k), v] }]
18
20
  new(params)
19
21
  end
20
22
 
21
- def self.underscore(text)
22
- text.gsub(/::/, '/').
23
- gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
24
- gsub(/([a-z\d])([A-Z])/,'\1_\2').
25
- tr("-", "_").
26
- downcase
27
- end
28
- private_class_method :underscore
29
-
30
23
  end
31
24
 
32
25
  end
@@ -0,0 +1,24 @@
1
+ module TextRazor
2
+
3
+ class Phrase
4
+
5
+ attr_reader :id, :text
6
+
7
+ def initialize(params, words)
8
+ @id = params["id"]
9
+ @text = match_words(params["wordPositions"], words)
10
+ end
11
+
12
+ def self.create_from_hash(params, words)
13
+ new(params, words)
14
+ end
15
+
16
+ def match_words(positions, words)
17
+ phrase = []
18
+ positions.each { |position| phrase.push words[position].token }
19
+ phrase.join(' ')
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -55,6 +55,10 @@ module TextRazor
55
55
  end
56
56
  end
57
57
 
58
+ def phrases
59
+ @phrases ||= parse_phrases(raw_response["nounPhrases"], words)
60
+ end
61
+
58
62
  private
59
63
 
60
64
  def bad_request?(code)
@@ -77,6 +81,14 @@ module TextRazor
77
81
  end
78
82
  end
79
83
 
84
+ def parse_phrases(raw_phrases, words)
85
+ return nil if raw_phrases.nil?
86
+
87
+ raw_phrases.map do |phrase_hash|
88
+ Phrase.create_from_hash(phrase_hash, words)
89
+ end
90
+ end
91
+
80
92
  end
81
93
 
82
94
  end
@@ -0,0 +1,11 @@
1
+ module TextRazor
2
+ module Util
3
+ def standardize(param)
4
+ param.gsub(/::/, '/').
5
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
6
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
7
+ tr("-", "_").
8
+ downcase
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module TextRazor
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -2,20 +2,20 @@ module TextRazor
2
2
 
3
3
  class Word
4
4
 
5
- attr_reader :position, :starting_pos, :ending_pos, :stem,
6
- :lemma, :token, :part_of_speech
5
+ extend Util
7
6
 
8
- def initialize(params)
9
- @position = params["position"]
10
- @starting_pos = params["startingPos"]
11
- @ending_pos = params["endingPos"]
12
- @stem = params["stem"]
13
- @lemma = params["lemma"]
14
- @token = params["token"]
15
- @part_of_speech = params["partOfSpeech"]
7
+ attr_reader :position, :starting_pos, :ending_pos, :stem, :lemma,
8
+ :token, :part_of_speech, :parent_position
9
+
10
+ def initialize(params = {})
11
+ @type = []
12
+ params.each do |k, v|
13
+ instance_variable_set(:"@#{k}", v) if v && self.respond_to?(:"#{k}")
14
+ end
16
15
  end
17
16
 
18
17
  def self.create_from_hash(params)
18
+ params = Hash[params.map {|k, v| [standardize(k), v] }]
19
19
  new(params)
20
20
  end
21
21
 
@@ -70,12 +70,12 @@ module TextRazor
70
70
  it "should make correct calls" do
71
71
  request = Object.new
72
72
 
73
- Request.should_receive(:post).
73
+ expect(Request).to receive(:post).
74
74
  with('text', {api_key: 'api_key', extractors: %w(entities topics words), cleanup_html: true,
75
75
  language: 'fre', filter_dbpedia_types: %w(type1), filter_freebase_types: %w(type2)}).
76
76
  and_return(request)
77
77
 
78
- Response.should_receive(:new).with(request)
78
+ expect(Response).to receive(:new).with(request)
79
79
 
80
80
  client.analyse('text')
81
81
  end
@@ -125,11 +125,11 @@ module TextRazor
125
125
  client = OpenStruct.new
126
126
  response = OpenStruct.new topics: ['topic1'], coarseTopics: ['topic1']
127
127
 
128
- Client.should_receive(:new).
128
+ expect(Client).to receive(:new).
129
129
  with(api_key, {extractors: ['topics']}).
130
130
  and_return(client)
131
131
 
132
- client.should_receive(:analyse).
132
+ expect(client).to receive(:analyse).
133
133
  with("text").
134
134
  and_return(response)
135
135
 
@@ -144,11 +144,11 @@ module TextRazor
144
144
  client = OpenStruct.new
145
145
  response = OpenStruct.new topics: ['topic1'], coarseTopics: ['topic1']
146
146
 
147
- Client.should_receive(:new).
147
+ expect(Client).to receive(:new).
148
148
  with(api_key, {extractors: ['topics']}).
149
149
  and_return(client)
150
150
 
151
- client.should_receive(:analyse).
151
+ expect(client).to receive(:analyse).
152
152
  with("text").
153
153
  and_return(response)
154
154
 
@@ -163,11 +163,11 @@ module TextRazor
163
163
  client = OpenStruct.new
164
164
  response = OpenStruct.new entities: ['Entity1']
165
165
 
166
- Client.should_receive(:new).
166
+ expect(Client).to receive(:new).
167
167
  with(api_key, {extractors: ['entities']}).
168
168
  and_return(client)
169
169
 
170
- client.should_receive(:analyse).
170
+ expect(client).to receive(:analyse).
171
171
  with("text").
172
172
  and_return(response)
173
173
 
@@ -182,11 +182,11 @@ module TextRazor
182
182
  client = OpenStruct.new
183
183
  response = OpenStruct.new words: ['Word1']
184
184
 
185
- Client.should_receive(:new).
185
+ expect(Client).to receive(:new).
186
186
  with(api_key, {extractors: ['words']}).
187
187
  and_return(client)
188
188
 
189
- client.should_receive(:analyse).
189
+ expect(client).to receive(:analyse).
190
190
  with("text").
191
191
  and_return(response)
192
192
 
@@ -195,6 +195,25 @@ module TextRazor
195
195
 
196
196
  end
197
197
 
198
+ context ".phrases" do
199
+
200
+ it "should make correct calls" do
201
+ client = OpenStruct.new
202
+ response = OpenStruct.new phrases: ['Phrase1']
203
+
204
+ expect(Client).to receive(:new).
205
+ with(api_key, {extractors: ['phrases', 'words']}).
206
+ and_return(client)
207
+
208
+ expect(client).to receive(:analyse).
209
+ with("text").
210
+ and_return(response)
211
+
212
+ Client.phrases(api_key, 'text', {})
213
+ end
214
+
215
+ end
216
+
198
217
  end
199
218
 
200
219
  end
@@ -0,0 +1,26 @@
1
+ require "spec_helper"
2
+
3
+ module TextRazor
4
+
5
+ describe Phrase do
6
+
7
+ context "#create_from_hash" do
8
+
9
+ it "should create a new instance" do
10
+ phrase_hash = {"id" => 1, "wordPositions" => [0, 1, 2]}
11
+ words = [
12
+ OpenStruct.new(token: 'Word1'),
13
+ OpenStruct.new(token: 'Word2'),
14
+ OpenStruct.new(token: 'Word3')
15
+ ]
16
+
17
+ phrase = Phrase.create_from_hash(phrase_hash, words)
18
+
19
+ expect(phrase.id).to eq(1)
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+
26
+ end
@@ -45,7 +45,7 @@ module TextRazor
45
45
  it "should make correct calls" do
46
46
  options = {api_key: 'api_key', extractors: %w(entities topics words dependency-trees relations entailments)}
47
47
 
48
- ::RestClient.should_receive(:post).
48
+ expect(::RestClient).to receive(:post).
49
49
  with("https://api.textrazor.com/", { "text" => 'text', "apiKey" => 'api_key',
50
50
  "extractors" => "entities,topics,words,dependency-trees,relations,entailments" }, accept_encoding: 'gzip')
51
51
 
@@ -60,7 +60,7 @@ module TextRazor
60
60
  options = {api_key: 'api_key', extractors: %w(entities topics words), cleanup_html: true,
61
61
  language: 'fre', filter_dbpedia_types: %w(type1), filter_freebase_types: %w(type2)}
62
62
 
63
- ::RestClient.should_receive(:post).
63
+ expect(::RestClient).to receive(:post).
64
64
  with("https://api.textrazor.com/", { "text" => 'text', "apiKey" => 'api_key', "extractors" => "entities,topics,words",
65
65
  "cleanupHTML" => true, "languageOverride" => 'fre', "entities.filterDbpediaTypes" => "type1",
66
66
  "entities.filterFreebaseTypes" => "type2" },
@@ -12,7 +12,7 @@ module TextRazor
12
12
  body = "{\"response\":\"{}\"}"
13
13
  http_response = ::OpenStruct.new code: 200, body: body
14
14
 
15
- JSON.should_receive(:parse).
15
+ expect(JSON).to receive(:parse).
16
16
  with(body).
17
17
  and_return({"response"=>"{}"})
18
18
 
@@ -7,8 +7,16 @@ module TextRazor
7
7
  context "#create_from_hash" do
8
8
 
9
9
  it "should create a new instance" do
10
- word_hash = {"position" => 0, "startingPos" => 0, "endingPos" => 3, "stem" => "the",
11
- "lemma" => "the", "token" => "The", "partOfSpeech" => "DT"}
10
+ word_hash = {
11
+ "position" => 0,
12
+ "startingPos" => 0,
13
+ "endingPos" => 3,
14
+ "stem" => "the",
15
+ "lemma" => "the",
16
+ "token" => "The",
17
+ "partOfSpeech" => "DT",
18
+ "parentPosition" => 2
19
+ }
12
20
 
13
21
  word = Word.create_from_hash(word_hash)
14
22
 
@@ -19,6 +27,7 @@ module TextRazor
19
27
  expect(word.lemma).to eq("the")
20
28
  expect(word.token).to eq("The")
21
29
  expect(word.part_of_speech).to eq("DT")
30
+ expect(word.parent_position).to eq(2)
22
31
  end
23
32
 
24
33
  end
@@ -5,7 +5,7 @@ describe TextRazor do
5
5
  describe ".topics" do
6
6
 
7
7
  it "should make correct calls" do
8
- TextRazor::Client.should_receive(:topics).
8
+ expect(TextRazor::Client).to receive(:topics).
9
9
  with('api_key', 'text', {})
10
10
 
11
11
  TextRazor.topics('api_key', 'text', {})
@@ -16,7 +16,7 @@ describe TextRazor do
16
16
  describe ".entities" do
17
17
 
18
18
  it "should make correct calls" do
19
- TextRazor::Client.should_receive(:entities).
19
+ expect(TextRazor::Client).to receive(:entities).
20
20
  with('api_key', 'text', {})
21
21
 
22
22
  TextRazor.entities('api_key', 'text', {})
@@ -27,7 +27,7 @@ describe TextRazor do
27
27
  describe ".words" do
28
28
 
29
29
  it "should make correct calls" do
30
- TextRazor::Client.should_receive(:words).
30
+ expect(TextRazor::Client).to receive(:words).
31
31
  with('api_key', 'text', {})
32
32
 
33
33
  TextRazor.words('api_key', 'text', {})
@@ -35,6 +35,17 @@ describe TextRazor do
35
35
 
36
36
  end
37
37
 
38
+ describe ".phrases" do
39
+
40
+ it "should make correct calls" do
41
+ expect(TextRazor::Client).to receive(:phrases).
42
+ with('api_key', 'text', {})
43
+
44
+ TextRazor.phrases('api_key', 'text', {})
45
+ end
46
+
47
+ end
48
+
38
49
  describe ".reset" do
39
50
  before :each do
40
51
  TextRazor.configure do |config|
@@ -2,9 +2,4 @@ require "bundler"
2
2
  Bundler.require
3
3
 
4
4
  require 'ostruct'
5
- require 'rspec/fire'
6
5
  require File.expand_path("../../lib/textrazor" ,__FILE__)
7
-
8
- RSpec.configure do |config|
9
- config.include(RSpec::Fire)
10
- end
@@ -22,7 +22,6 @@ Gem::Specification.new do |spec|
22
22
  spec.add_dependency "fast_open_struct"
23
23
 
24
24
  spec.add_development_dependency "bundler", "~> 1.3"
25
- spec.add_development_dependency "rake"
26
- spec.add_development_dependency "rspec"
27
- spec.add_development_dependency "rspec-fire"
25
+ spec.add_development_dependency "rake", "~> 10.4"
26
+ spec.add_development_dependency "rspec", "~> 3.0.0"
28
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: textrazor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anuj Dutta
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-06 00:00:00.000000000 Z
11
+ date: 2015-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -56,44 +56,30 @@ dependencies:
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: '10.4'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: '10.4'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: rspec-fire
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
73
+ - - "~>"
88
74
  - !ruby/object:Gem::Version
89
- version: '0'
75
+ version: 3.0.0
90
76
  type: :development
91
77
  prerelease: false
92
78
  version_requirements: !ruby/object:Gem::Requirement
93
79
  requirements:
94
- - - ">="
80
+ - - "~>"
95
81
  - !ruby/object:Gem::Version
96
- version: '0'
82
+ version: 3.0.0
97
83
  description: Api wrapper for text razor
98
84
  email:
99
85
  - anuj@andhapp.com
@@ -111,14 +97,17 @@ files:
111
97
  - lib/textrazor/client.rb
112
98
  - lib/textrazor/configuration.rb
113
99
  - lib/textrazor/entity.rb
100
+ - lib/textrazor/phrase.rb
114
101
  - lib/textrazor/request.rb
115
102
  - lib/textrazor/response.rb
116
103
  - lib/textrazor/topic.rb
104
+ - lib/textrazor/util.rb
117
105
  - lib/textrazor/version.rb
118
106
  - lib/textrazor/word.rb
119
107
  - spec/lib/textrazor/client_spec.rb
120
108
  - spec/lib/textrazor/configuration_spec.rb
121
109
  - spec/lib/textrazor/entity_spec.rb
110
+ - spec/lib/textrazor/phrase_spec.rb
122
111
  - spec/lib/textrazor/request_spec.rb
123
112
  - spec/lib/textrazor/response_spec.rb
124
113
  - spec/lib/textrazor/topic_spec.rb
@@ -146,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
135
  version: '0'
147
136
  requirements: []
148
137
  rubyforge_project:
149
- rubygems_version: 2.2.2
138
+ rubygems_version: 2.4.5
150
139
  signing_key:
151
140
  specification_version: 4
152
141
  summary: An api wrapper for text razor in ruby
@@ -154,6 +143,7 @@ test_files:
154
143
  - spec/lib/textrazor/client_spec.rb
155
144
  - spec/lib/textrazor/configuration_spec.rb
156
145
  - spec/lib/textrazor/entity_spec.rb
146
+ - spec/lib/textrazor/phrase_spec.rb
157
147
  - spec/lib/textrazor/request_spec.rb
158
148
  - spec/lib/textrazor/response_spec.rb
159
149
  - spec/lib/textrazor/topic_spec.rb