Class: PROIEL::Converter::Tiger

Inherits:
Object
  • Object
show all
Defined in:
lib/proiel/cli/converters/tiger.rb

Overview

Converter for the TigerXML format (www.ims.uni-stuttgart.de/projekte/TIGER/TIGERSearch/doc/html/TigerXML.html) in the variant used by VISL under the name ‘TIGER dependency format’ (beta.visl.sdu.dk/treebanks.html#TIGER_dependency_format).

Constant Summary collapse

SCHEMA_FILE =
File.join('tigerxml', 'TigerXML.xsd')
MORPHOLOGICAL_FEATURES =
%i(person_number tense_mood_voice case_number gender degree strength inflection)
OTHER_FEATURES =
%i(lemma pos information_status antecedent_id word)

Class Method Summary collapse

Class Method Details

.declare_annotation(builder, features, annotation_schema) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/proiel/cli/converters/tiger.rb', line 51

def declare_annotation(builder, features, annotation_schema)
  builder.annotation do
    features.each do |name, domain|
      # FIXME: we may want to list possible values for some of these
      builder.feature(name: name, domain: domain)
    end

    builder.edgelabel do
      builder.value(name: '--')

      annotation_schema.primary_relations.each do |tag, features|
        builder.value({ name: tag }, features.summary)
      end
    end

    builder.secedgelabel do
      annotation_schema.secondary_relations.each do |tag, features|
        builder.value({name: tag }, features.summary)
      end
    end
  end
end

.process(tb, options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/proiel/cli/converters/tiger.rb', line 16

def process(tb, options)
  selected_features = MORPHOLOGICAL_FEATURES + OTHER_FEATURES
  @features = selected_features.map { |f| [f, 'FREC'] }.to_h

  builder = Builder::XmlMarkup.new(target: STDOUT, indent: 2)
  builder.instruct! :xml, version: '1.0', encoding: 'UTF-8'

  tb.sources.each do |source|
    @hack = tb.annotation_schema
    write_source(builder, source) do
      source.divs.each do |div|
        div.sentences.each do |sentence|
          write_sentence(builder, sentence)
        end
      end
    end
  end
end

.token_attrs(s, t, type) ⇒ Object



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
# File 'lib/proiel/cli/converters/tiger.rb', line 74

def token_attrs(s, t, type)
  attrs = {}

  @features.each do |name, domain|
    if domain == 'FREC' or domain == type
      case name
      when :word, :cat
        attrs[name] = t.pro? ? "PRO-#{t.relation.upcase}" : t.form
      when *@semantic_features
        attrs[name] = t.sem_tags_to_hash[attr]
      when :lemma
        attrs[name] = t.lemma
      when :pos
        if t.empty_token_sort
          attrs[name] = t.empty_token_sort + "-"
        else
          attrs[name] = t.pos
        end
      when *MORPHOLOGICAL_FEATURES
        attrs[name] = name.to_s.split("_").map { |a| t.morphology_hash[a.to_sym] || '-' }.join
      else
        if t.respond_to?(name)
          attrs[name] = t.send(name)
        else
          raise "Do not know how to get required attribute #{name}"
        end
      end
      attrs[name] ||= "--"
    end
  end

  attrs
end

.write_nonterminals(builder, s) ⇒ Object



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
# File 'lib/proiel/cli/converters/tiger.rb', line 116

def write_nonterminals(builder, s)
  builder.nonterminals do
    # Add an empty root node
    h = @features.select { |_, domain| ['FREC', 'NT'].include?(domain) }.map { |name, _| [name, '--'] }.to_h
    h[:id] = "s#{s.id}_root"

    builder.nt(h) do
      s.tokens.reject { |t| t.head or t.pro? }.each do |t|
        builder.edge(idref: "p#{t.id}", label: t.relation)
      end
    end

    # Add other NTs
    s.tokens.each do |t|
      builder.nt(token_attrs(s, t, 'NT').merge(id: "p#{t.id}")) do
        # Add an edge to the correspoding terminal node
        builder.edge(idref: "w#{t.id}", label: '--')

        # Add primary dependency edges
        t.children.each { |d| builder.edge(idref: "p#{d.id}", label: d.relation) }

        # Add secondary dependency edges
        t.slashes.each do |relation, target_id|
          builder.secedge(idref: "p#{target_id}", label: relation)
        end
      end
    end
  end
end

.write_sentence(builder, s) ⇒ Object



146
147
148
149
150
151
152
153
# File 'lib/proiel/cli/converters/tiger.rb', line 146

def write_sentence(builder, s)
  builder.s(id: "s#{s.id}") do
    builder.graph(root: "s#{s.id}_root") do
      write_terminals(builder, s)
      write_nonterminals(builder, s)
    end
  end
end

.write_source(builder, s) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/proiel/cli/converters/tiger.rb', line 35

def write_source(builder, s)
  builder.corpus(id: s.id) do
    builder.head do
      builder.meta do
        builder.name(s.title)
      end

      declare_annotation(builder, @features, @hack)
    end

    builder.body do
      yield
    end
  end
end

.write_terminals(builder, s) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/proiel/cli/converters/tiger.rb', line 108

def write_terminals(builder, s)
  builder.terminals do
    s.tokens.each do |t|
      builder.t(token_attrs(s, t, 'T').merge({ id: "w#{t.id}"}))
    end
  end
end