Permalink
Browse files

Upgraded to rspec3 and removed rspec-fire as rspec 3.0 introduced ver…

…ifying doubles; Refactored Word and moved the code to initialise parameters to util.
  • Loading branch information...
andhapp committed Aug 25, 2014
1 parent f633513 commit 48aeff496be89c7433dadfbbedd3e1d3e4375b64
View
@@ -1,5 +1,6 @@
require "textrazor/version"
require "textrazor/configuration"
require "textrazor/util"
require "textrazor/client"
require "textrazor/request"
require "textrazor/response"
View
@@ -9,7 +9,8 @@ class Client
DEFAULT_EXTRACTORS = ['entities', 'topics', 'words', 'phrases', 'dependency-trees',
'relations', 'entailments', 'senses']
REQUEST_OPTIONS = [:extractors, :cleanup_html, :language, :filter_dbpedia_types, :filter_freebase_types]
REQUEST_OPTIONS = [:extractors, :cleanup_html, :language,
:filter_dbpedia_types, :filter_freebase_types]
attr_reader :response, :api_key, :request_options
View
@@ -2,6 +2,8 @@ module TextRazor
class Entity
extend Util
attr_reader :id, :type, :matching_tokens, :entity_id, :freebase_types, :confidence_score,
:wiki_link, :matched_text, :freebase_id, :relevance_score, :entity_english_id,
:starting_pos, :ending_pos
@@ -14,19 +16,10 @@ def initialize(params = {})
end
def self.create_from_hash(params)
params = Hash[params.map {|k, v| [underscore(k), v] }]
params = Hash[params.map {|k, v| [standardize(k), v] }]
new(params)
end
def self.underscore(text)
text.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
private_class_method :underscore
end
end
View
@@ -15,9 +15,7 @@ def self.create_from_hash(params, words)
def match_words(positions, words)
phrase = []
positions.each { |position| phrase.push words[position].token }
phrase.join(' ')
end
View
@@ -0,0 +1,11 @@
module TextRazor
module Util
def standardize(param)
param.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
end
end
View
@@ -2,27 +2,20 @@ module TextRazor
class Word
attr_reader :position,
:starting_pos,
:ending_pos,
:stem,
:lemma,
:token,
:part_of_speech,
:parent_position
extend Util
def initialize(params)
@position = params["position"]
@starting_pos = params["startingPos"]
@ending_pos = params["endingPos"]
@stem = params["stem"]
@lemma = params["lemma"]
@token = params["token"]
@part_of_speech = params["partOfSpeech"]
@parent_position = params["parentPosition"]
attr_reader :position, :starting_pos, :ending_pos, :stem, :lemma,
:token, :part_of_speech, :parent_position
def initialize(params = {})
@type = []
params.each do |k, v|
instance_variable_set(:"@#{k}", v) if v && self.respond_to?(:"#{k}")
end
end
def self.create_from_hash(params)
params = Hash[params.map {|k, v| [standardize(k), v] }]
new(params)
end
@@ -70,12 +70,12 @@ module TextRazor
it "should make correct calls" do
request = Object.new
Request.should_receive(:post).
expect(Request).to receive(:post).
with('text', {api_key: 'api_key', extractors: %w(entities topics words), cleanup_html: true,
language: 'fre', filter_dbpedia_types: %w(type1), filter_freebase_types: %w(type2)}).
and_return(request)
Response.should_receive(:new).with(request)
expect(Response).to receive(:new).with(request)
client.analyse('text')
end
@@ -125,11 +125,11 @@ module TextRazor
client = OpenStruct.new
response = OpenStruct.new topics: ['topic1'], coarseTopics: ['topic1']
Client.should_receive(:new).
expect(Client).to receive(:new).
with(api_key, {extractors: ['topics']}).
and_return(client)
client.should_receive(:analyse).
expect(client).to receive(:analyse).
with("text").
and_return(response)
@@ -144,11 +144,11 @@ module TextRazor
client = OpenStruct.new
response = OpenStruct.new topics: ['topic1'], coarseTopics: ['topic1']
Client.should_receive(:new).
expect(Client).to receive(:new).
with(api_key, {extractors: ['topics']}).
and_return(client)
client.should_receive(:analyse).
expect(client).to receive(:analyse).
with("text").
and_return(response)
@@ -163,11 +163,11 @@ module TextRazor
client = OpenStruct.new
response = OpenStruct.new entities: ['Entity1']
Client.should_receive(:new).
expect(Client).to receive(:new).
with(api_key, {extractors: ['entities']}).
and_return(client)
client.should_receive(:analyse).
expect(client).to receive(:analyse).
with("text").
and_return(response)
@@ -182,11 +182,11 @@ module TextRazor
client = OpenStruct.new
response = OpenStruct.new words: ['Word1']
Client.should_receive(:new).
expect(Client).to receive(:new).
with(api_key, {extractors: ['words']}).
and_return(client)
client.should_receive(:analyse).
expect(client).to receive(:analyse).
with("text").
and_return(response)
@@ -201,11 +201,11 @@ module TextRazor
client = OpenStruct.new
response = OpenStruct.new phrases: ['Phrase1']
Client.should_receive(:new).
expect(Client).to receive(:new).
with(api_key, {extractors: ['phrases', 'words']}).
and_return(client)
client.should_receive(:analyse).
expect(client).to receive(:analyse).
with("text").
and_return(response)
@@ -45,7 +45,7 @@ module TextRazor
it "should make correct calls" do
options = {api_key: 'api_key', extractors: %w(entities topics words dependency-trees relations entailments)}
::RestClient.should_receive(:post).
expect(::RestClient).to receive(:post).
with("https://api.textrazor.com/", { "text" => 'text', "apiKey" => 'api_key',
"extractors" => "entities,topics,words,dependency-trees,relations,entailments" }, accept_encoding: 'gzip')
@@ -60,7 +60,7 @@ module TextRazor
options = {api_key: 'api_key', extractors: %w(entities topics words), cleanup_html: true,
language: 'fre', filter_dbpedia_types: %w(type1), filter_freebase_types: %w(type2)}
::RestClient.should_receive(:post).
expect(::RestClient).to receive(:post).
with("https://api.textrazor.com/", { "text" => 'text', "apiKey" => 'api_key', "extractors" => "entities,topics,words",
"cleanupHTML" => true, "languageOverride" => 'fre', "entities.filterDbpediaTypes" => "type1",
"entities.filterFreebaseTypes" => "type2" },
@@ -12,7 +12,7 @@ module TextRazor
body = "{\"response\":\"{}\"}"
http_response = ::OpenStruct.new code: 200, body: body
JSON.should_receive(:parse).
expect(JSON).to receive(:parse).
with(body).
and_return({"response"=>"{}"})
@@ -5,7 +5,7 @@
describe ".topics" do
it "should make correct calls" do
TextRazor::Client.should_receive(:topics).
expect(TextRazor::Client).to receive(:topics).
with('api_key', 'text', {})
TextRazor.topics('api_key', 'text', {})
@@ -16,7 +16,7 @@
describe ".entities" do
it "should make correct calls" do
TextRazor::Client.should_receive(:entities).
expect(TextRazor::Client).to receive(:entities).
with('api_key', 'text', {})
TextRazor.entities('api_key', 'text', {})
@@ -27,7 +27,7 @@
describe ".words" do
it "should make correct calls" do
TextRazor::Client.should_receive(:words).
expect(TextRazor::Client).to receive(:words).
with('api_key', 'text', {})
TextRazor.words('api_key', 'text', {})
@@ -38,7 +38,7 @@
describe ".phrases" do
it "should make correct calls" do
TextRazor::Client.should_receive(:phrases).
expect(TextRazor::Client).to receive(:phrases).
with('api_key', 'text', {})
TextRazor.phrases('api_key', 'text', {})
View
@@ -2,9 +2,4 @@
Bundler.require
require 'ostruct'
require 'rspec/fire'
require File.expand_path("../../lib/textrazor" ,__FILE__)
RSpec.configure do |config|
config.include(RSpec::Fire)
end
View
@@ -23,6 +23,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
spec.add_development_dependency "rspec-fire"
spec.add_development_dependency "rspec", "~> 3.0.0"
end

1 comment on commit 48aeff4

@abrisse

This comment has been minimized.

Show comment
Hide comment
@abrisse

abrisse Aug 25, 2014

Collaborator

👍

Collaborator

abrisse commented on 48aeff4 Aug 25, 2014

👍

Please sign in to comment.