Class: Opener::Kaf::Visualizer::HTMLTextPresenter

Inherits:
Object
  • Object
show all
Defined in:
lib/opener/s3_outlet/visualizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ HTMLTextPresenter

Returns a new instance of HTMLTextPresenter.



203
204
205
# File 'lib/opener/s3_outlet/visualizer.rb', line 203

def initialize(document)
  @document = document
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



202
203
204
# File 'lib/opener/s3_outlet/visualizer.rb', line 202

def document
  @document
end

Instance Method Details

#opinions_for(*ids) ⇒ Object



262
263
264
265
266
267
268
269
270
# File 'lib/opener/s3_outlet/visualizer.rb', line 262

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



254
255
256
257
258
259
260
# File 'lib/opener/s3_outlet/visualizer.rb', line 254

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_htmlObject



207
208
209
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
# File 'lib/opener/s3_outlet/visualizer.rb', line 207

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