Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merging #11

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Changes
## 0.0.14
* fix problem with Nokogiri 1.5.6

## 0.0.13

Expand Down
5 changes: 2 additions & 3 deletions calais.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ Gem::Specification.new do |gem|
gem.summary = 'A Ruby interface to the Calais Web Service'
gem.description = 'A Ruby interface to the Calais Web Service'

gem.authors = ['Abhay Kumar']
gem.email = '[email protected]'
gem.homepage = 'http://github.com/abhay/calais'
gem.authors = ['enigmatic00']
gem.homepage = 'https://github.com/enigmatic00/calais'

gem.add_dependency("nokogiri", ">= 1.3.3")
gem.add_dependency("json", ">= 1.1.3")
Expand Down
1 change: 0 additions & 1 deletion lib/calais.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require 'net/http'
require 'uri'
require 'cgi'
require 'iconv'
require 'set'
require 'date'

Expand Down
2 changes: 1 addition & 1 deletion lib/calais/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def initialize(options={}, &block)
def enlighten
post_args = {
"licenseID" => @license_id,
"content" => Iconv.iconv('UTF-8//IGNORE', 'UTF-8', "#{@content} ").first[0..-2],
"content" => "#{@content} ".encode(Encoding::UTF_8, :invalid => :replace, :undef => :replace, :replace => '')[0..-2],
"paramsXML" => params_xml
}

Expand Down
23 changes: 10 additions & 13 deletions lib/calais/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ def self.from_node(node)
instance.suffix = node.xpath("c:suffix[1]").first.content
instance.offset = node.xpath("c:offset[1]").first.content.to_i
instance.length = node.xpath("c:length[1]").first.content.to_i

instance
end
end
Expand All @@ -82,14 +81,13 @@ def self.find_or_create(hash, hashes)
private
def extract_data
doc = Nokogiri::XML(@raw_response)

if doc.root.xpath("/Error[1]").first
raise Calais::Error, doc.root.xpath("/Error/Exception").first.content
end

doc.root.xpath("rdf:Description/rdf:type[contains(@rdf:resource, '#{MATCHERS[:docinfometa]}')]/..").each do |node|
@language = node['language']
@submission_date = DateTime.parse node['submissionDate']
@language = node['c:language']
@submission_date = DateTime.parse node['c:submissionDate']

attributes = extract_attributes(node.xpath("*[contains(name(), 'c:')]"))

Expand All @@ -100,7 +98,7 @@ def extract_data
end

doc.root.xpath("rdf:Description/rdf:type[contains(@rdf:resource, '#{MATCHERS[:docinfo]}')]/..").each do |node|
@request_id = node['calaisRequestID']
@request_id = node['c:calaisRequestID']

attributes = extract_attributes(node.xpath("*[contains(name(), 'c:')]"))

Expand Down Expand Up @@ -131,15 +129,15 @@ def extract_data
end

@relevances = doc.root.xpath("rdf:Description/rdf:type[contains(@rdf:resource, '#{MATCHERS[:relevances]}')]/..").inject({}) do |acc, node|
subject_hash = node.xpath("c:subject[1]").first[:resource].split('/')[-1]
subject_hash = node.xpath("c:subject[1]/@rdf:resource").first.content.split('/')[-1]
acc[subject_hash] = node.xpath("c:relevance[1]").first.content.to_f

node.remove
acc
end

@entities = doc.root.xpath("rdf:Description/rdf:type[contains(@rdf:resource, '#{MATCHERS[:entities]}')]/..").map do |node|
extracted_hash = node['about'].split('/')[-1] rescue nil
extracted_hash = node['rdf:about'].split('/')[-1] rescue nil

entity = Entity.new
entity.calais_hash = CalaisHash.find_or_create(extracted_hash, @hashes)
Expand All @@ -152,9 +150,8 @@ def extract_data
node.remove
entity
end

@relations = doc.root.xpath("rdf:Description/rdf:type[contains(@rdf:resource, '#{MATCHERS[:relations]}')]/..").map do |node|
extracted_hash = node['about'].split('/')[-1] rescue nil
extracted_hash = node['rdf:about'].split('/')[-1] rescue nil

relation = Relation.new
relation.calais_hash = CalaisHash.find_or_create(extracted_hash, @hashes)
Expand Down Expand Up @@ -187,7 +184,7 @@ def extract_data

def extract_instances(doc, hash)
doc.root.xpath("rdf:Description/rdf:type[contains(@rdf:resource, '#{MATCHERS[:instances]}')]/..").select do |instance_node|
instance_node.xpath("c:subject[1]").first[:resource].split("/")[-1] == hash
instance_node.xpath("c:subject[1]/@rdf:resource").first.content.split("/")[-1] == hash
end.map do |instance_node|
instance = Instance.from_node(instance_node)
instance_node.remove
Expand All @@ -197,15 +194,15 @@ def extract_instances(doc, hash)
end

def extract_type(node)
node.xpath("*[name()='rdf:type']")[0]['resource'].split('/')[-1]
node.xpath("*[name()='rdf:type']")[0]['rdf:resource'].split('/')[-1]
rescue
nil
end

def extract_attributes(nodes)
nodes.inject({}) do |hsh, node|
value = if node['resource']
extracted_hash = node['resource'].split('/')[-1] rescue nil
value = if node['rdf:resource']
extracted_hash = node['rdf:resource'].split('/')[-1] rescue nil
CalaisHash.find_or_create(extracted_hash, @hashes)
else
node.content
Expand Down