Class: TAO::RDFizer
- Inherits:
-
Object
- Object
- TAO::RDFizer
- Defined in:
- lib/tao_rdfizer/tao_rdfizer.rb
Instance Method Summary collapse
-
#initialize(mode = nil) ⇒ RDFizer
constructor
if mode == :spans then produces span descriptions if mode == :annotations then produces annotation descriptions if mode == nil then produces both.
- #rdfize(annotations_col, with_prefix = true) ⇒ Object
Constructor Details
#initialize(mode = nil) ⇒ RDFizer
if mode == :spans then produces span descriptions if mode == :annotations then produces annotation descriptions if mode == nil then produces both
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/tao_rdfizer/tao_rdfizer.rb', line 10 def initialize(mode = nil) @mode = mode template = if !mode.nil? && mode == :spans ERB_SPANS_TTL else ERB_ANNOTATIONS_TTL end @tao_ttl_erb = ERB.new(template, nil, '-') @prefix_ttl_erb = ERB.new(ERB_PREFIXES_TTL, nil, '-') end |
Instance Method Details
#rdfize(annotations_col, with_prefix = true) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/tao_rdfizer/tao_rdfizer.rb', line 22 def rdfize(annotations_col, with_prefix = true) # check the format annotations_col.each do |annotations| raise "'target' is missing" unless annotations.has_key? :target end # namespaces namespaces = {} anns = annotations_col.first anns[:namespaces].each {|n| namespaces[n[:prefix]] = n[:uri]} unless anns[:namespaces].nil? unless @mode ==:spans raise ArgumentError, "A project name has to be specified." unless anns.has_key?(:project) prefix_for_this = anns[:project].downcase.gsub(/ /, '_') raise ArgumentError, "'#{prefix_for_this}' is a reserved prefix for this project." if namespaces.has_key?(prefix_for_this) project_uri = 'http://pubannotation.org/projects/' + anns[:project] namespaces[prefix_for_this] = project_uri + '/' end denotations = [] attributes = [] relations = [] spans = [] annotations_col.each do |annotations| text = annotations[:text] text_uri = annotations[:target] text_id = begin sourcedb, sourceid, divid = get_target_info(text_uri) divid.nil? ? "#{sourcedb}-#{sourceid}" : "#{sourcedb}-#{sourceid}-#{divid}" end # denotations and relations _denotations = annotations[:denotations] || [] _attributes = annotations[:attributes] || [] _relations = annotations[:relations] || [] if @mode == :spans && annotations.has_key?(:tracks) annotations[:tracks].each do |track| _denotations += track[:denotations] _attributes += track[:attributes] _relations += track[:relations] end end begin if @mode == :annotations # index attributes attributesh = _attributes.inject({}) do |h, a| if a[:pred].end_with?('_id') subj = a[:subj] h[subj] = [] unless h.has_key? subj h[subj] << a[:obj] end h end # denotations preprocessing _denotations.each do |d| span_uri = "<#{text_uri}/spans/#{d[:span][:begin]}-#{d[:span][:end]}>" d[:span_uri] = span_uri d[:obj_uri] = "#{prefix_for_this}:#{text_id}-#{d[:id]}" class_uris = (attributesh[d[:id]] || []).push(d[:obj]) d[:class_uris] = class_uris.map{|uri| find_uri(uri, namespaces, prefix_for_this)} rescue ArgumentError => e raise ArgumentError, "[#{sourcedb}-#{sourceid}-#{d[:id]}] " + e. end # relations preprocessing _relations.each do |r| r[:subj_uri] = "#{prefix_for_this}:#{text_id}-#{r[:subj]}" r[:obj_uri] = "#{prefix_for_this}:#{text_id}-#{r[:obj]}" r[:pred_uri] = find_uri(r[:pred], namespaces, prefix_for_this) rescue ArgumentError => e raise ArgumentError, "[#{sourcedb}-#{sourceid}-#{r[:id]}] " + e. end else # denotations preprocessing _denotations.each do |d| span_uri = "<#{text_uri}/spans/#{d[:span][:begin]}-#{d[:span][:end]}>" d[:span_uri] = span_uri rescue ArgumentError => e raise ArgumentError, "[#{sourcedb}-#{sourceid}-#{d[:id]}] " + e. end end end unless @mode == :annotations # collect spans _spans = _denotations.map{|d| d[:span]} _spans.uniq! # add_infomation _spans.each do |s| s[:span_uri] = "<#{text_uri}/spans/#{s[:begin]}-#{s[:end]}>" s[:source_uri] = text_uri s[:text] = text[s[:begin] ... s[:end]] end # index spans spanh = _spans.inject({}){|r, s| r[s[:span_uri]] = s; r} # add denotation information _denotations.each do |d| span_uri = d[:span_uri] if spanh[span_uri][:denotations].nil? spanh[span_uri][:denotations] = [d] else spanh[span_uri][:denotations] << d end end _spans.sort!{|a, b| (a[:begin] <=> b[:begin]).nonzero? || b[:end] <=> a[:end]} ## begin indexing len = text.length num = _spans.length # initilaize the index (0 ... num).each do |i| _spans[i][:followings] = [] _spans[i][:children] = [] end (0 ... num).each do |i| # find the first following span j = i + 1 while j < num && _spans[j][:begin] < _spans[i][:end] unless include_parent?(_spans[i][:children], _spans[j]) _spans[i][:children] << _spans[j] end j += 1 end # find adjacent positions fp = _spans[i][:end] fp += 1 while fp < len && text[fp].match(/\s/) next if fp == len # index adjacent spans while j < num && _spans[j][:begin] == fp _spans[i][:followings] << _spans[j] j += 1 end end end denotations += _denotations relations += _relations spans += _spans unless @mode == :annotations end ttl = '' ttl += @prefix_ttl_erb.result(binding) if with_prefix ttl += @tao_ttl_erb.result(binding) end |