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

Issue with namespace prefixes? #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 10 additions & 14 deletions lib/calais/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ def extract_data
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 +100,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,16 +131,14 @@ 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]").first["rdf:resource"].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)
entity.type = extract_type(node)
Expand All @@ -154,7 +152,7 @@ def extract_data
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 All @@ -168,7 +166,6 @@ def extract_data

@geographies = doc.root.xpath("rdf:Description/rdf:type[contains(@rdf:resource, '#{MATCHERS[:geographies]}')]/..").map do |node|
attributes = extract_attributes(node.xpath("*[contains(name(), 'c:')]"))

geography = Geography.new
geography.name = attributes.delete('name')
geography.calais_hash = attributes.delete('subject')
Expand All @@ -187,25 +184,24 @@ 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]").first["rdf:resource"].split("/")[-1] == hash
end.map do |instance_node|
instance = Instance.from_node(instance_node)
instance_node.remove

instance
end
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