class Mexico::Fiesta::Interfaces::B6ChatGameInterface

Public Instance Methods

convert_phrases(fdoc, parent_item, layer, node) click to toggle source

A recursive method that converts phrase structures with variable depth into a linked FiESTA annotation structure. @param fdoc [FiestaDocument] The FiESTA document to which all items shall be added. @param parent_item [Item] The parent item object to which children shall be added. @param layer [Layer] The FiESTA layer that shall contain all annotations. @param node [Node] The XML node that contains the phrase structures to be parsed. @return nil

# File lib/mexico/fiesta/interfaces/b6_chat_game_interface.rb, line 182
def convert_phrases(fdoc, parent_item, layer, node)

  k = 1
  node.xpath('./*').each do |p|

    if p.element?

      i = Mexico::FileSystem::Item.new identifier: "#{parent_item.identifier}-#{k}", document: fdoc

      i.item_links << ItemLink.new(identifier: "#{i.identifier}-il", target_object: parent_item, role: 'parent', document: fdoc)
      i.layer_links <<  LayerLink.new(identifier:"#{i.identifier}-to-layer", target_object: layer, document: fdoc  )

      i.data = Data.new string_value: p.name, item: i, document: fdoc

      fdoc.items << i

      convert_phrases fdoc, i, layer, p



      if p.children.first.text?

        j = Mexico::FileSystem::Item.new identifier: "#{parent_item.identifier}-#{k}-val", document: fdoc

        j.item_links << ItemLink.new(identifier: "#{i.identifier}-il", target_object: i, role: 'parent', document: fdoc)
        j.layer_links <<  LayerLink.new(identifier:"#{i.identifier}-to-layer", target_object: layer, document: fdoc )

        j.data = Data.new string_value: p.text, item: j, document: fdoc

        fdoc.items << j
      end

    end

  k=k+1
  end

end
export(doc, io=$stdout) click to toggle source

Attempts to export the given FiESTA document to the B6 chat game format. Currently, this does not work since the B6 format is too specialised.

# File lib/mexico/fiesta/interfaces/b6_chat_game_interface.rb, line 172
def export(doc, io=$stdout)

end
import(io=$stdin) click to toggle source

Imports a B6 chat game document by reading contents from the given IO object. @param io [IO] The IO object to read from. @return [FiestaDocument] on success, the corresponding FiESTA document.

# File lib/mexico/fiesta/interfaces/b6_chat_game_interface.rb, line 33
def import(io=$stdin)

  io.rewind

  fiesta_document = FiestaDocument.new
  f = fiesta_document

  t = fiesta_document.add_standard_timeline('s')
  x = Scale.new(identifier: 'spatial_x', name: 'Spatial coordinate X', unit: 'pixel', document: f)
  y = Scale.new(identifier: 'spatial_y', name: 'Spatial coordinate Y', unit: 'pixel', document: f)
  fiesta_document.add_scale x
  fiesta_document.add_scale y

  lChats = Layer.new(identifier: 'chats',     name: 'Chats',     document: f)
  lMoves = Layer.new(identifier: 'moves',     name: 'Moves',     document: f)
  lSents = Layer.new(identifier: 'sentences', name: 'Sentences', document: f)
  lParsT = Layer.new(identifier: 'parsedTrees', name: 'Parsed Trees', document: f)
  lParsP = Layer.new(identifier: 'parsedPhrases', name: 'Parsed Phrases', document: f)

  # additional, secondary annotations for:
  # - word / correction pairs
  # - forms    // LATER
  # - colors   // LATER
  # - sentences, with attributes
  # - their parsetrees, with attributes

  fiesta_document.add_layer lChats
  fiesta_document.add_layer lMoves
  fiesta_document.add_layer lSents
  fiesta_document.add_layer lParsT
  fiesta_document.add_layer lParsP

  # B6 data is avaiable in XML documents, so we read
  # those into a Nokogiri object.
  xml_document = ::Nokogiri::XML(io)

  # puts xml_document.root

  round_counter = 0

  last_chat_elem = nil
  last_chat_item = nil
  xml_document.xpath('/match/round').each do |round|

    round_counter += 1
    actions = round.xpath('./*')
    el_counter=0
    actions.each do |action|
      el_counter += 1
      tag_name = action.name
      if tag_name == 'move'
        # import moves.
        i = Item.new(identifier: "round-#{round_counter}-move-#{el_counter}", document: f)
        time_val = action['time'].gsub(/^\+/, '').to_i
        i.point_links << PointLink.new(identifier: "move-#{el_counter}-t", point: time_val , target_object: t, document: f)
        # get x and y values
        to = action['to'].split(",").map(&:to_i)
        i.point_links << PointLink.new(identifier: "move-#{el_counter}-x", point: to[0], target_object: x, document: f)
        i.point_links << PointLink.new(identifier: "move-#{el_counter}-y", point: to[1], target_object: y, document: f)
        i.data = Data.new(item: i, document: f)
        # link layer
        i.layer_links << LayerLink.new(identifier: "move-#{el_counter}-layer", target_object: lMoves, document: f)
        fiesta_document.add_item i
      end

      if tag_name == 'chat'
        i = Item.new(identifier: "round-#{round_counter}-chat-#{el_counter}", document: f)
        time_val = action['time'].gsub(/^\+/, '').to_i
        i.point_links << PointLink.new(identifier: "chat-#{el_counter}-t", point: time_val , target_object: t, document: f)
        i.data = Data.new(:string_value => action['message'], item: i, document: f)
        i.layer_links << LayerLink.new(identifier: "chat-#{el_counter}-layer", target_object: lChats, document: f)
        fiesta_document.add_item i
        # todo: remember this chat item, the next annotations refer to it

        last_chat_elem = action
        last_chat_item = i
      end

      if tag_name == 'annotation'
        # - ./spelling
        #     - word/correction pairs
        #     - ./forms
        #     - ./colors
        # - ./sentence
        #     - @value
        #     - @type
        #     - @lok
        #     - @no
        #     - ./parsetree
        #         - @tiefe
        #         - @verzweigung
        #         - @hoeflichkeit

        action.xpath('./sentence').each do |sentence|
          # sentence : xml node of the sentence

          # get running number
          s_no = sentence['no'].to_i
          s_id = sentence['id']

          sent_item = Item.new identifier: s_id, document: f
          sent_item.item_links << ItemLink.new(identifier: "#{s_id}-to-chat", target_object: last_chat_item, role: 'parent', document: f )
          sent_item.layer_links << LayerLink.new(identifier:"#{s_id}-to-layer", target_object: lSents, document: f  )

          sent_item.data = Data.new map: Mexico::FileSystem::FiestaMap.new({
                                        value: sentence['value'],
                                        type:  sentence['type'],
                                         lok:  sentence['lok'],
                                         no:   sentence['no']}),
                                    item: sent_item, document: f
          sidm = sent_item.data.map
          f.add_item sent_item

          parsetree_elem = sentence.xpath('./parsetree').first
          if parsetree_elem.nil?
            $stderr.puts "There is a parsetree missing in sentence #{s_id}"
          else
            pt_id = parsetree_elem['id']
            parsetree_item = Item.new identifier: pt_id , document: f
            parsetree_item.item_links << ItemLink.new(identifier: "#{pt_id}-to-sentence", target_object: sent_item, role: 'parent', document: f)
            parsetree_item.layer_links << LayerLink.new(identifier:"#{pt_id}-to-layer", target_object: lParsT, document: f  )
            parsetree_item.data = Data.new map: Mexico::FileSystem::FiestaMap.new({
                                                                         tiefe: parsetree_elem['tiefe'],
                                                                         verzweigung:  parsetree_elem['verzweigung'],
                                                                         hoeflichkeit:  parsetree_elem['hoeflichkeit']}),
                                           item: parsetree_item, document: f
            f.add_item parsetree_item
            convert_phrases f, parsetree_item, lParsP, parsetree_elem
          end
        end
      end
    end
  end

  return fiesta_document
end