module Srgs

Constants

VERSION

Public Instance Methods

build(grammar) click to toggle source
# 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) click to toggle source
# File lib/srgs/grammar_builder.rb, line 24
def example(example, xml)
  xml.example example.text
end
item(item, xml) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/srgs/grammar_builder.rb, line 68
def metadata(metadata, xml)
  #TODO
  raise "Not yet implemented"
end
one_of(one_of, xml) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/srgs/grammar_builder.rb, line 129
def set(sym, att, value)
  att[sym] = value unless value.nil?
end
tag(tag, xml) click to toggle source
# File lib/srgs/grammar_builder.rb, line 114
def tag(tag, xml)
  xml.tag tag.text
end
text(text, xml) click to toggle source
# File lib/srgs/grammar_builder.rb, line 118
def text(text, xml)
  xml.text text
end
token(token, xml) click to toggle source
# 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