class Opener::Kaf::Visualizer::HTMLTextPresenter

Attributes

document[R]

Public Class Methods

new(document) click to toggle source
# File lib/opener/s3_outlet/visualizer.rb, line 203
def initialize(document)
  @document = document
end

Public Instance Methods

opinions_for(*ids) click to toggle source
# 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) click to toggle source
# 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_html() click to toggle source
# 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