Class: NCBO::Parser::ResourceIndex

Inherits:
BaseParser show all
Defined in:
lib/ncbo_resource_index_client/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseParser

#parse_xml, #safe_to_i

Constructor Details

#initialize(results) ⇒ ResourceIndex

Returns a new instance of ResourceIndex.



20
21
22
23
# File 'lib/ncbo_resource_index_client/parser.rb', line 20

def initialize(results)
  @root = "/success/data/annotatorResultBean"
  @results = parse_xml(results)
end

Class Method Details

.parse_element_annotations(results) ⇒ Object



136
137
138
# File 'lib/ncbo_resource_index_client/parser.rb', line 136

def self.parse_element_annotations(results)
  new(results).parse_element_annotations
end

.parse_element_results(results) ⇒ Object



121
122
123
# File 'lib/ncbo_resource_index_client/parser.rb', line 121

def self.parse_element_results(results)
  new(results).parse_element_results
end

.parse_included_ontologies(ontologies) ⇒ Object



25
26
27
# File 'lib/ncbo_resource_index_client/parser.rb', line 25

def self.parse_included_ontologies(ontologies)
  new(ontologies).parse_included_ontologies
end


105
106
107
# File 'lib/ncbo_resource_index_client/parser.rb', line 105

def self.parse_popular_concepts(results)
  new(results).parse_popular_concepts
end

.parse_ranked_element_results(results) ⇒ Object



75
76
77
# File 'lib/ncbo_resource_index_client/parser.rb', line 75

def self.parse_ranked_element_results(results)
  new(results).parse_ranked_element_results
end

.parse_resources(resources) ⇒ Object



34
35
36
# File 'lib/ncbo_resource_index_client/parser.rb', line 34

def self.parse_resources(resources)
  new(resources).parse_resources
end

.parse_results(results, options = {}) ⇒ Object



49
50
51
# File 'lib/ncbo_resource_index_client/parser.rb', line 49

def self.parse_results(results, options = {})
  new(results).parse_results(options)
end

Instance Method Details

#parse_element_annotationsObject



140
141
142
143
144
145
146
147
# File 'lib/ncbo_resource_index_client/parser.rb', line 140

def parse_element_annotations
  annotation_location = "annotation"
  with_context = false
  
  annotations = NCBO::ResourceIndex::Annotations.new
  annotations.annotations = parse_annotations(@results.find_first("/success/data/list"), with_context, annotation_location)
  annotations
end

#parse_element_resultsObject



125
126
127
128
129
130
131
132
133
134
# File 'lib/ncbo_resource_index_client/parser.rb', line 125

def parse_element_results
  element = parse_element(@results.find_first("/success/data"), "obs.obr.populate.Element")
  new_element = NCBO::ResourceIndex::Element.new
  new_element.id = element[:localElementId]
  new_element.weights = element[:weights]
  new_element.ontoIds = element[:ontoIds]
  new_element.text = element[:text]
  new_element.resource = element[:text].first[0].split("_")[0] unless element[:text].first.nil?
  new_element
end

#parse_included_ontologiesObject



29
30
31
32
# File 'lib/ncbo_resource_index_client/parser.rb', line 29

def parse_included_ontologies
  @root = "/success/data/set"
  ontologies = parse_ontologies("ontology")
end


109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ncbo_resource_index_client/parser.rb', line 109

def parse_popular_concepts
  concepts = []
  @results.find("/success/data/list/*").each do |concept_frequency|
    concept = {}
    concept[:counts] = concept_frequency.find_first("counts").content.to_i
    concept[:score] = concept_frequency.find_first("score").content.to_i
    concept[:concept] = parse_concept(concept_frequency)
    concepts << concept
  end
  concepts
end

#parse_ranked_element_resultsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ncbo_resource_index_client/parser.rb', line 79

def parse_ranked_element_results
  ranked_elements = NCBO::ResourceIndex::RankedElements.new

  ranked_elements.concepts = []
  @results.find("/success/data/map/entry[string='concepts']/list/*").each do |concept|
    concept = parse_concept(concept, ".")
    ranked_elements.concepts << concept
  end

  ranked_elements.resources = []
  @results.find("/success/data/map/entry[string='elements']/list/resourceElements").each do |resource|
    r = {}
    r[:resourceId] = resource.find_first("resourceId").content
    r[:offset] = resource.find_first("offset").content.to_i
    r[:limit] = resource.find_first("limit").content.to_i
    r[:totalResults] = resource.find_first("totalResults").content.to_i
    r[:elements] = []
    resource.find("./elementResults/elementResult").each do |element|
      r[:elements] << parse_element(element)
    end
    ranked_elements.resources << r
  end

  ranked_elements
end

#parse_resourcesObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/ncbo_resource_index_client/parser.rb', line 38

def parse_resources
  resources = []
  @results.find("/success/data/set/resource").each do |resource|
    a = {}
    resource.children.each {|child| a[child.name.to_sym] = safe_to_i(child.content) if !child.first.nil? && !child.first.children?}
    a[:contexts] = parse_resource_structure(resource.find_first("resourceStructure"))
    resources << a
  end
  resources
end

#parse_results(options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ncbo_resource_index_client/parser.rb', line 53

def parse_results(options = {})
  annotation_location ||= options[:annotation_location]
  
  results = []
  @results.find("/success/data/list/*").each do |result|
    resource_annotations = NCBO::ResourceIndex::Annotations.new
    resource_annotations.resource = result.find_first("resourceId").content

    # Check to see if parameters are enabled that will change how we process the output
    with_context = result.find_first("withContext").content.eql?("true") rescue false
    counts = result.find_first("counts").content.eql?("true") rescue false

    # Update total count (if available)
    resource_annotations.total_annotation_count = result.find_first("resultStatistics/statistics/annotationCount").content.to_i if counts
    
    resource_annotations.annotations = parse_annotations(result, with_context, annotation_location)
    results << resource_annotations
  end
  results = results[0] if results.kind_of?(Array) && results.length == 1
  results
end