Module: NlgXmlRealiserBuilder::PrimitiveOperations

Included in:
DSL
Defined in:
lib/nlg_xml_realiser_builder/primitive_operations.rb

Instance Method Summary collapse

Instance Method Details

#builder(simple = false, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/nlg_xml_realiser_builder/primitive_operations.rb', line 4

def builder(simple = false, &block)
  Nokogiri::XML::Builder.new do |xml|
    @xml = xml
    xml.NLGSpec(Consts::XML_SCHEMA) {
      xml.Recording {
        if simple
          record {
            doc {
              instance_eval(&block) if block
            }
          }
        else
          instance_eval(&block) if block
        end
      }
    }
  end
end

#doc(tag = :Document, options = {}, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/nlg_xml_realiser_builder/primitive_operations.rb', line 31

def doc(tag = :Document, options = {}, &block)
  category = options.delete(:cat) || 'PARAGRAPH'
  unless Consts::DOCUMENT_CATEGORIES.include?(category)
    raise "Document category '#{category}' is not in [#{Consts::DOCUMENT_CATEGORIES.join(", ")}]"
  end
  attributes = {}
  attributes.merge!('xsi:type' => 'DocumentElement') unless tag == :Document
  attributes.merge!('cat' => category)
  @xml.send(tag, attributes) {
    nlg_eval('DocumentElement', &block)
  }
end

#head(base, cat = 'NOUN') ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/nlg_xml_realiser_builder/primitive_operations.rb', line 51

def head(base, cat = 'NOUN')
  unless Consts::NLG_LEXICAL_CATEGORY.include?(cat)
    raise "Head category '#{cat}' is not in [#{Consts::NLG_LEXICAL_CATEGORY.join(", ")}]"
  end
  @xml.head('cat' => cat) {
    @xml.base base
  }
end

#record(name = nil, &block) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/nlg_xml_realiser_builder/primitive_operations.rb', line 23

def record(name = nil, &block)
  attributes = {}
  attributes.merge!(name: name) if name
  @xml.Record(attributes) {
    instance_eval(&block) if block
  }
end

#spec(base, cat = 'DETERMINER') ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/nlg_xml_realiser_builder/primitive_operations.rb', line 60

def spec(base, cat = 'DETERMINER')
  unless Consts::NLG_LEXICAL_CATEGORY.include?(cat)
    raise "Specifier category must be in [#{Consts::NLG_LEXICAL_CATEGORY.join(", ")}]"
  end
  @xml.spec_('xsi:type' => 'WordElement', 'cat' => cat) {
    @xml.base base
  }
end

#str(tag = :compl, val = nil, options = nil, &block) ⇒ Object



44
45
46
47
48
49
# File 'lib/nlg_xml_realiser_builder/primitive_operations.rb', line 44

def str(tag = :compl, val = nil, options = nil, &block)
  return unless val
  @xml.send(tag, 'xsi:type' => 'StringElement') {
    @xml.val val.to_s
  }
end