Class: Mexico::Fiesta::Interfaces::B6ChatGameInterface

Inherits:
Object
  • Object
show all
Includes:
Mexico::FileSystem, Singleton
Defined in:
lib/mexico/fiesta/interfaces/b6_chat_game_interface.rb

Instance Method Summary collapse

Instance Method Details

#convert_phrases(fdoc, parent_item, layer, node) ⇒ Object

A recursive method that converts phrase structures with variable depth into a linked FiESTA annotation structure.

Parameters:

  • fdoc (FiestaDocument)

    The FiESTA document to which all items shall be added.

  • parent_item (Item)

    The parent item object to which children shall be added.

  • layer (Layer)

    The FiESTA layer that shall contain all annotations.

  • node (Node)

    The XML node that contains the phrase structures to be parsed.

Returns:

  • nil


182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# 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) ⇒ Object

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.


172
173
174
# File 'lib/mexico/fiesta/interfaces/b6_chat_game_interface.rb', line 172

def export(doc, io=$stdout)

end

#import(io = $stdin) ⇒ FiestaDocument

Imports a B6 chat game document by reading contents from the given IO object.

Parameters:

  • io (IO) (defaults to: $stdin)

    The IO object to read from.

Returns:


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
# 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