Module: Srgs

Extended by:
Srgs
Included in:
Srgs
Defined in:
lib/srgs.rb,
lib/srgs/dsl.rb,
lib/srgs/version.rb,
lib/srgs/elements/tag.rb,
lib/srgs/elements/item.rb,
lib/srgs/elements/meta.rb,
lib/srgs/elements/rule.rb,
lib/srgs/elements/token.rb,
lib/srgs/elements/one_of.rb,
lib/srgs/grammar_builder.rb,
lib/srgs/elements/example.rb,
lib/srgs/elements/grammar.rb,
lib/srgs/elements/lexicon.rb,
lib/srgs/elements/metadata.rb,
lib/srgs/elements/rule_ref.rb

Defined Under Namespace

Modules: DSL Classes: Example, Grammar, Item, Lexicon, Meta, Metadata, OneOf, Rule, RuleRef, Tag, Token

Constant Summary collapse

VERSION =
"1.1.2"

Instance Method Summary collapse

Instance Method Details

#build(grammar) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/srgs/grammar_builder.rb', line 6

def build(grammar)
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.grammar(:'xml:lang' => "en-US",
                root: grammar.root,
                xmlns:"http://www.w3.org/2001/06/grammar",
                version: "1.0",
                :'tag-format' => "semantics/1.0") do
      lexicon(grammar.lexicon, xml) unless grammar.lexicon.nil?
      grammar.metas.each do |meta|
        meta(meta, xml)
      end
      grammar.rules.each do |rule|
        rule(rule, xml)
      end
    end
  end.to_xml
end

#example(example, xml) ⇒ Object



24
25
26
# File 'lib/srgs/grammar_builder.rb', line 24

def example(example, xml)
  xml.example example.text
end

#item(item, xml) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/srgs/grammar_builder.rb', line 28

def item(item, xml)
  att = {}
  set(:repeat, att, item.repeat)
  set(:'repeat-prob', att, item.repeat_prob)
  set(:weight, att, item.weight)
  xml.item(att) do
    item.elements.each do |element|
      case element
      when Token
        token(element, xml)
      when RuleRef
        rule_ref(element, xml)
      when Tag
        tag(element, xml)
      when String
        text(element, xml)
      when OneOf
        one_of(element, xml)
      when Item
        item(element, xml)
      else
        raise "Can't add #{element.class} to item."
      end
    end
  end
end

#lexicon(lexicon, xml) ⇒ Object



55
56
57
58
59
# File 'lib/srgs/grammar_builder.rb', line 55

def lexicon(lexicon, xml)
  att = {uri: lexicon.uri }
  set(:type, att, lexicon.type)
  xml.lexicon(att)
end

#meta(meta, xml) ⇒ Object



61
62
63
64
65
66
# File 'lib/srgs/grammar_builder.rb', line 61

def meta(meta, xml)
  att = { content: meta.content }
  set(:'http-equiv', att, meta.http_equiv)
  set(:name, att, meta.name)
  xml.meta(att)
end

#metadata(metadata, xml) ⇒ Object



68
69
70
71
# File 'lib/srgs/grammar_builder.rb', line 68

def (, xml)
  #TODO
  raise "Not yet implemented"
end

#one_of(one_of, xml) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/srgs/grammar_builder.rb', line 73

def one_of(one_of, xml)
  xml.send('one-of') do
    one_of.items.each do |item|
      item(item, xml)
    end
  end
end

#rule(rule, xml) ⇒ Object



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
# File 'lib/srgs/grammar_builder.rb', line 81

def rule(rule, xml)
  att = {id: rule.id}
  set(:scope, att, rule.scope)
  set(:'sapi:dynamic', att, rule.dynamic)
  xml.rule(att) do
    rule.elements.each do |element|
      case element
      when Item
        item(element, xml)
      when RuleRef
        rule_ref(element, xml)
      when OneOf
        one_of(element, xml)
      when Token
        token(element, xml)
      when Tag
        tag(element, xml)
      when Example
        example(element, xml)
      else
        raise "Can't add #{element.class} to item"
      end
    end
  end
end

#rule_ref(rule_ref, xml) ⇒ Object



107
108
109
110
111
112
# File 'lib/srgs/grammar_builder.rb', line 107

def rule_ref(rule_ref, xml)
  att = {}
  set(:uri, att, rule_ref.uri)
  set(:special, att, rule_ref.special)
  xml.ruleref(att)
end

#set(sym, att, value) ⇒ Object



129
130
131
# File 'lib/srgs/grammar_builder.rb', line 129

def set(sym, att, value)
  att[sym] = value unless value.nil?
end

#tag(tag, xml) ⇒ Object



114
115
116
# File 'lib/srgs/grammar_builder.rb', line 114

def tag(tag, xml)
  xml.tag tag.text
end

#text(text, xml) ⇒ Object



118
119
120
# File 'lib/srgs/grammar_builder.rb', line 118

def text(text, xml)
  xml.text text
end

#token(token, xml) ⇒ Object



122
123
124
125
126
127
# File 'lib/srgs/grammar_builder.rb', line 122

def token(token, xml)
  att = {}
  set(:'sapi:display', att, token.display)
  set(:'sapi:pron', att, token.pron)
  xml.token(token.text, att)
end