Class: Opener::Kaf::Visualizer::HTMLTextPresenter
- Inherits:
-
Object
- Object
- Opener::Kaf::Visualizer::HTMLTextPresenter
- Defined in:
- lib/opener/outlet/visualizer.rb
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
Instance Method Summary collapse
-
#initialize(document) ⇒ HTMLTextPresenter
constructor
A new instance of HTMLTextPresenter.
- #opinions_for(*ids) ⇒ Object
- #targets_for(variable, *ids) ⇒ Object
- #to_html ⇒ Object
Constructor Details
#initialize(document) ⇒ HTMLTextPresenter
Returns a new instance of HTMLTextPresenter.
206 207 208 |
# File 'lib/opener/outlet/visualizer.rb', line 206 def initialize(document) @document = document end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
205 206 207 |
# File 'lib/opener/outlet/visualizer.rb', line 205 def document @document end |
Instance Method Details
#opinions_for(*ids) ⇒ Object
265 266 267 268 269 270 271 272 273 |
# File 'lib/opener/outlet/visualizer.rb', line 265 def opinions_for(*ids) targets = document.opinions.values.select do |value| value.has_target?(ids.flatten) end ids = targets.map(&:id) sentiments = targets.map(&:expression) return ids.concat(sentiments).uniq end |
#targets_for(variable, *ids) ⇒ Object
257 258 259 260 261 262 263 |
# File 'lib/opener/outlet/visualizer.rb', line 257 def targets_for(variable, *ids) targets = document.public_send(variable.to_sym).values.select do |value| value.has_target?(ids.flatten) end targets.map(&:id) end |
#to_html ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/opener/outlet/visualizer.rb', line 210 def to_html offset = 0 prev = Struct.new(:offset).new(0) builder = Nokogiri::HTML::Builder.new do |html| html.div(:class=>"opener") do html.p do document.words.values.sort_by(&:offset).each do |word| if offset < word.offset spacer = word.offset - offset spacer = Array.new(spacer, " ").join html.span(spacer) end terms = targets_for(:terms, word.id) entities = targets_for(:entities, terms) opinions = opinions_for(terms) properties = targets_for(:properties, terms) generics = [] generics << "term" if terms.size > 0 generics << "entity" if entities.size > 0 generics << "opinion" if opinions.size > 0 generics << "property" if properties.size > 0 classes = [terms, entities, opinions, properties, generics].flatten.uniq word_annotations = classes.join(" ") html.span(word.content, :class=>word_annotations, :id=>word.id) offset = word.offset + word.length end end [:entities, :opinions, :properties].each do |sym| html.div(:class=>sym) do document.public_send(sym).values.each do |entity| html.div(entity.to_s, :id=>entity.id, :class=>entity.target_ids.join(" ")) end end end end end builder.to_html end |