Class: RubySpeech::GRXML::Grammar
- Includes:
- XML::Language
- Defined in:
- lib/ruby_speech/grxml/grammar.rb
Overview
The Speech Recognition Grammar Language is an XML application. The root element is grammar.
www.w3.org/TR/speech-grammar/#S4.3
Attributes: uri, language, root, tag-format
tag-format declaration is an optional declaration of a tag-format identifier that indicates the content type of all tags contained within a grammar.
NOTE: A grammar without rules is allowed but cannot be used for processing input – www.w3.org/Voice/2003/srgs-ir/
TODO: Look into lexicon (probably a sub element)
Constant Summary collapse
Instance Attribute Summary
Attributes included from RubySpeech::GenericElement
Instance Method Summary collapse
- #<<(arg) ⇒ Object
-
#assert_has_matching_root_rule ⇒ Grammar
Checks for a root rule matching the value of the root tag.
- #dtmf? ⇒ Boolean
- #embed(other) ⇒ Object
- #eql?(o) ⇒ Boolean
-
#inline ⇒ Grammar
An inlined copy of self.
-
#inline! ⇒ Object
Replaces rulerefs in the document with a copy of the original rule.
-
#mode ⇒ String
The mode of a grammar indicates the type of input that the user agent should be detecting.
- #mode=(ia) ⇒ Object
-
#normalize_whitespace ⇒ Object
Normalizes whitespace within tokens in the document according to the rules in the SRGS spec (www.w3.org/TR/speech-grammar/#S2.1) Leading and trailing whitespace is removed, and multiple spaces within the string are collapsed down to single spaces.
-
#root ⇒ String
The root (“rule”) attribute indicates declares a single rule to be the root rle of the grammar.
- #root=(ia) ⇒ Object
-
#root_rule ⇒ Rule
The root rule node for the document.
- #tag_format ⇒ String
- #tag_format=(s) ⇒ Object
-
#tokenize! ⇒ Object
Replaces textual content of the document with token elements containing such content.
- #voice? ⇒ Boolean
Methods included from XML::Language
Methods inherited from Element
module, namespace, #regexp_content, root_element, #to_doc
Methods included from RubySpeech::GenericElement
#+, #==, #base_uri, #base_uri=, #build, #children, #clone, #create_node, #eval_dsl_block, included, #inherit, #initialize, #inspect, #mass_assign, #method_missing, #namespace=, #namespace_href, #node, #nokogiri_children, #read_attr, #respond_to_missing?, #string, #to_s, #traverse, #version, #version=, #write_attr
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class RubySpeech::GenericElement
Instance Method Details
#<<(arg) ⇒ Object
194 195 196 197 |
# File 'lib/ruby_speech/grxml/grammar.rb', line 194 def <<(arg) raise InvalidChildError, "A Grammar can only accept Rule and Tag as children" unless VALID_CHILD_TYPES.include? arg.class super end |
#assert_has_matching_root_rule ⇒ Grammar
Checks for a root rule matching the value of the root tag
100 101 102 103 |
# File 'lib/ruby_speech/grxml/grammar.rb', line 100 def assert_has_matching_root_rule raise InvalidChildError, "A GRXML document must have a rule matching the root rule name" unless has_matching_root_rule? self end |
#dtmf? ⇒ Boolean
186 187 188 |
# File 'lib/ruby_speech/grxml/grammar.rb', line 186 def dtmf? mode == :dtmf end |
#embed(other) ⇒ Object
203 204 205 206 |
# File 'lib/ruby_speech/grxml/grammar.rb', line 203 def (other) raise InvalidChildError, "Embedded grammars must have the same mode" if other.is_a?(self.class) && other.mode != mode super end |
#eql?(o) ⇒ Boolean
199 200 201 |
# File 'lib/ruby_speech/grxml/grammar.rb', line 199 def eql?(o) super o, :language, :base_uri, :mode, :root end |
#inline ⇒ Grammar
Returns an inlined copy of self.
108 109 110 |
# File 'lib/ruby_speech/grxml/grammar.rb', line 108 def inline clone.inline! end |
#inline! ⇒ Object
Replaces rulerefs in the document with a copy of the original rule. Removes all top level rules except the root rule
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 |
# File 'lib/ruby_speech/grxml/grammar.rb', line 122 def inline! previous_uris = {} loop do rule = nil uris = {} xpath('//ns:ruleref', ns: GRXML_NAMESPACE).each do |ref| uri = ref[:uri].sub(/^#/, '') uris[uri] = 1 rule = rule_with_id uri unless rule raise MissingReferenceError, "Ruleref '##{uri}' is referenced but not defined" end ref.swap rule.dup.children end break unless rule if previous_uris.keys.eql? uris.keys raise ReferentialLoopError, 'GRXML document contains cycles with ruleref(s): ' << uris.keys.join(', ') end previous_uris = uris end query = "./ns:rule[@id!='#{root}']" query += "|./ns:rule[@ns:id!='#{root}']" if Nokogiri.jruby? non_root_rules = xpath query, ns: namespace_href non_root_rules.remove self end |
#mode ⇒ String
The mode of a grammar indicates the type of input that the user agent should be detecting. The default mode is “voice” for speech recognition grammars. An alternative input mode is “dtmf” input“.
42 43 44 |
# File 'lib/ruby_speech/grxml/grammar.rb', line 42 def mode read_attr :mode, :to_sym end |
#mode=(ia) ⇒ Object
49 50 51 |
# File 'lib/ruby_speech/grxml/grammar.rb', line 49 def mode=(ia) self[:mode] = ia end |
#normalize_whitespace ⇒ Object
Normalizes whitespace within tokens in the document according to the rules in the SRGS spec (www.w3.org/TR/speech-grammar/#S2.1) Leading and trailing whitespace is removed, and multiple spaces within the string are collapsed down to single spaces.
177 178 179 180 181 182 183 184 |
# File 'lib/ruby_speech/grxml/grammar.rb', line 177 def normalize_whitespace traverse do |element| next if element === self imported_element = self.class.import element imported_element.normalize_whitespace if imported_element.respond_to?(:normalize_whitespace) end end |
#root ⇒ String
The root (“rule”) attribute indicates declares a single rule to be the root rle of the grammar. This attribute is OPTIONAL. The rule declared must be defined within the scope of the grammar. It specified rule can be scoped “public” or “private.”
59 60 61 |
# File 'lib/ruby_speech/grxml/grammar.rb', line 59 def root read_attr :root end |
#root=(ia) ⇒ Object
66 67 68 |
# File 'lib/ruby_speech/grxml/grammar.rb', line 66 def root=(ia) self[:root] = ia end |
#root_rule ⇒ Rule
Returns The root rule node for the document.
88 89 90 91 |
# File 'lib/ruby_speech/grxml/grammar.rb', line 88 def root_rule element = rule_with_id root self.class.import element if element end |
#tag_format ⇒ String
74 75 76 |
# File 'lib/ruby_speech/grxml/grammar.rb', line 74 def tag_format read_attr :'tag-format' end |
#tag_format=(s) ⇒ Object
81 82 83 |
# File 'lib/ruby_speech/grxml/grammar.rb', line 81 def tag_format=(s) self['tag-format'] = s end |
#tokenize! ⇒ Object
Replaces textual content of the document with token elements containing such content. This homogenises all tokens in the document to a consistent format for processing.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/ruby_speech/grxml/grammar.rb', line 158 def tokenize! traverse do |element| next unless element.is_a? Nokogiri::XML::Text element_type = self.class.import(element.parent).class next if [Token, Tag].include?(element_type) tokens = split_tokens(element).map do |string| Token.new(document).tap { |token| token << string }.node end element.swap Nokogiri::XML::NodeSet.new(document, tokens) end end |
#voice? ⇒ Boolean
190 191 192 |
# File 'lib/ruby_speech/grxml/grammar.rb', line 190 def voice? mode == :voice end |