Module: BioCWriter

Defined in:
lib/simple_bioc/bioc_writer.rb

Class Method Summary collapse

Class Method Details

.write(collection, options = {}) ⇒ Object



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

def write(collection, options = {})
  options[:save_with] = 1 if options[:save_with].nil?
  builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
    xml.doc.create_internal_subset( 'collection', nil, 'BioC.dtd' )
    write_collection(xml, collection)
  end
  builder.to_xml(options)
end

.write_annotation(xml, annotation) ⇒ Object



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

def write_annotation(xml, annotation) 
  if annotation.id.nil?
    attribute = nil
  else
    attribute = {id: annotation.id}
  end
  xml.annotation(attribute) {
    write_infon(xml, annotation)
    annotation.locations.each{|l| write_location(xml, l)} unless annotation.locations.nil?
    xml.text_ annotation.text
  }
end

.write_collection(xml, collection) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/simple_bioc/bioc_writer.rb', line 26

def write_collection(xml, collection)
  xml.collection {
    xml.source collection.source
    xml.date collection.date
    xml.key collection.key
    write_infon(xml, collection)
    collection.documents.each{|d| write_document(xml, d)} unless collection.documents.nil?
  }
end

.write_document(xml, document) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/simple_bioc/bioc_writer.rb', line 36

def write_document(xml, document)
  xml.document {
    xml.id_ document.id
    write_infon(xml, document)
    document.passages.each{|p| write_passage(xml, p)} unless document.passages.nil?
    document.relations.each{|r| write_relation(xml, r)} unless document.relations.nil?
  }
end

.write_infon(xml, obj) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/simple_bioc/bioc_writer.rb', line 17

def write_infon(xml, obj)
  return if obj.infons.nil?
  obj.infons.each do |k, v| 
    xml.infon(:key => k) {
      xml.text v
    }
  end
end

.write_location(xml, location) ⇒ Object



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

def write_location(xml, location) 
  return if location.nil? || location == false
  # if location.original_offset.nil? || location.original_offset == location.offset
    xml.location(:offset => location.offset, :length => location.length)
  # else
    # xml.location(:offset => location.offset, :length => location.length, :original_offset => location.original_offset)
  # end
end

.write_node(xml, node) ⇒ Object



100
101
102
# File 'lib/simple_bioc/bioc_writer.rb', line 100

def write_node(xml, node)
  xml.node_(:refid => node.refid, :role => node.role)
end

.write_passage(xml, passage) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/simple_bioc/bioc_writer.rb', line 45

def write_passage(xml, passage)
  xml.passage {
    write_infon(xml, passage)
    xml.offset passage.offset
    xml.text_ passage.text unless passage.text.nil?
    passage.annotations.each{|a| write_annotation(xml, a)} unless passage.annotations.nil?
    passage.sentences.each{|s| write_sentence(xml, s)} unless passage.sentences.nil?
    passage.relations.each{|r| write_relation(xml, r)} unless passage.relations.nil?
  }
end

.write_relation(xml, relation) ⇒ Object



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

def write_relation(xml, relation) 
  if relation.id.nil?
    attribute = nil
  else
    attribute = {id: relation.id}
  end
  xml.relation(attribute) {
    write_infon(xml, relation)
    relation.nodes.each{|n| write_node(xml, n)} unless relation.nodes.nil?
  }
end

.write_sentence(xml, sentence) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/simple_bioc/bioc_writer.rb', line 56

def write_sentence(xml, sentence)
  xml.sentence {
    write_infon(xml, sentence)
    xml.offset sentence.offset
    xml.text_ sentence.text unless sentence.text.nil?
    sentence.annotations.each{|a| write_annotation(xml, a)} unless sentence.annotations.nil?
    sentence.relations.each{|r| write_relation(xml, r)} unless sentence.relations.nil?
  }
end