Module: BioCReader

Defined in:
lib/simple_bioc/bioc_reader.rb

Class Method Summary collapse

Class Method Details

.read(path, options) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/simple_bioc/bioc_reader.rb', line 7

def read(path, options) 
  collection = nil
  File.open(path) do |file|
    collection = read_from_file_or_string(file, options)
  end

  collection
end

.read_annotation(xml, annotation, options = {}) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/simple_bioc/bioc_reader.rb', line 99

def read_annotation(xml, annotation, options = {}) 
  annotation.id = xml["id"]
  annotation.text = read_text(xml, "text")
  read_infon(xml, annotation)
  read_recursive(xml, annotation, "location")
  true
end

.read_collection(xml, collection, options = {}) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/simple_bioc/bioc_reader.rb', line 60

def read_collection(xml, collection, options = {})
  collection.source = read_text(xml, "source")
  collection.date = read_text(xml, "date")
  collection.key = read_text(xml, "key")
  read_infon(xml, collection)
  read_recursive(xml, collection, "document", options)
end

.read_document(xml, document, options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/simple_bioc/bioc_reader.rb', line 68

def read_document(xml, document, options = {})
  document.id = read_text(xml, "id")
  if options[:documents].kind_of?(Array) && !options[:documents].include?(document.id)
    return false
  end
  read_infon(xml, document)
  read_recursive(xml, document, "passage")
  read_recursive(xml, document, "relation")
  document.adjust_ref
  true
end

.read_from_file_or_string(file_or_string, options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/simple_bioc/bioc_reader.rb', line 16

def read_from_file_or_string(file_or_string, options)
  collection = nil

  xml_doc  = Nokogiri::XML(file_or_string) do |config|
    config.noent.strict.noblanks
  end
  xml = xml_doc.at_xpath("//collection")
  if xml.nil?
    fail 'Wrong format'
  end
  collection = SimpleBioC::Collection.new
  read_collection(xml, collection, options)

  collection
end

.read_infon(xml, obj) ⇒ Object



42
43
44
45
46
# File 'lib/simple_bioc/bioc_reader.rb', line 42

def read_infon(xml, obj)
  ret = xml.xpath("infon")
  return if ret.nil?
  ret.each{ |i| obj.infons[i["key"]] = i.content}
end

.read_int(xml, name) ⇒ Object



37
38
39
40
# File 'lib/simple_bioc/bioc_reader.rb', line 37

def read_int(xml, name)
  val = read_text(xml, name) 
  val && val.to_i
end

.read_location(xml, location, options = {}) ⇒ Object



114
115
116
117
118
119
# File 'lib/simple_bioc/bioc_reader.rb', line 114

def read_location(xml, location, options = {}) 
  location.offset = xml["offset"]
  location.length = xml["length"]
  # location.original_offset = xml["original_offset"]
  true
end

.read_node(xml, node, options = {}) ⇒ Object



121
122
123
124
125
# File 'lib/simple_bioc/bioc_reader.rb', line 121

def read_node(xml, node, options = {})
  node.refid = xml["refid"]
  node.role = xml["role"]
  true
end

.read_passage(xml, passage, options = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/simple_bioc/bioc_reader.rb', line 80

def read_passage(xml, passage, options = {})
  passage.text = read_text(xml, "text")
  passage.offset = read_int(xml, "offset")
  read_infon(xml, passage)
  read_recursive(xml, passage, "sentence")
  read_recursive(xml, passage, "annotation")
  read_recursive(xml, passage, "relation")
  true
end

.read_recursive(xml, obj, name, options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/simple_bioc/bioc_reader.rb', line 48

def read_recursive(xml, obj, name, options = {})
  target_class = SimpleBioC.const_get(name.capitalize)
  ret = xml.xpath(name)
  return if ret.nil?
  ret.each do |node|
    instance = target_class.new(obj)
    ret = send(:"read_#{name}", node, instance, options)
    arr = obj.instance_variable_get(:"@#{name}s")
    arr  << instance if ret && !arr.nil?
  end
end

.read_relation(xml, relation, options = {}) ⇒ Object



107
108
109
110
111
112
# File 'lib/simple_bioc/bioc_reader.rb', line 107

def read_relation(xml, relation, options = {}) 
  relation.id = xml["id"]
  read_infon(xml, relation)
  read_recursive(xml, relation, "node")
  true
end

.read_sentence(xml, sentence, options = {}) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/simple_bioc/bioc_reader.rb', line 90

def read_sentence(xml, sentence, options = {})
  sentence.text = read_text(xml, "text")
  sentence.offset = read_int(xml, "offset")
  read_infon(xml, sentence)
  read_recursive(xml, sentence, "annotation")
  read_recursive(xml, sentence, "relation")
  true
end

.read_text(xml, name) ⇒ Object



32
33
34
35
# File 'lib/simple_bioc/bioc_reader.rb', line 32

def read_text(xml, name)
  node = xml.at_xpath(name)
  node && node.content
end