Class: RubySpeech::GRXML::Item

Inherits:
Element
  • Object
show all
Includes:
XML::Language
Defined in:
lib/ruby_speech/grxml/item.rb

Overview

The item element is one of the valid expansion elements for the SGR rule element

http://www.w3.org/TR/speech-grammar/#S2.4 --> XML Form

The item element has four (optional) attributes: weight, repeat, repeat-prob, and xml:lang (language identifier)

http://www.w3.org/TR/speech-grammar/#S2.4.1
http://www.w3.org/TR/speech-grammar/#S2.3

A weight may be optionally provided for any number of alternatives in an alternative expansion. Weights are simple positive floating point values without exponentials. Legal formats are “n”, “n.”, “.n” and “n.n” where “n” is a sequence of one or many digits.

A weight is nominally a multiplying factor in the likelihood domain of a speech recognition search. A weight of 1.0 is equivalent to providing no weight at all. A weight greater than “1.0” positively biases the alternative and a weight less than “1.0” negatively biases the alternative.

repeat has several valid values…

Any repeated legal rule expansion is itself a legal rule expansion.

Operators are provided that define a legal rule expansion as being another sub-expansion that is optional, that is repeated zero or more times, that is repeated one or more times, or that is repeated some range of times.

repeat probability (repeat-prob) indicates the probability of successive repetition of the repeated expansion. It is ignored if repeat is not specified

xml:lang declares declaration declares the language of the grammar section for the item element just as xml:lang in the <grammar> element declares for the entire document

Constant Summary collapse

Inf =
1.0 / 0.0
VALID_CHILD_TYPES =
[Nokogiri::XML::Element, Nokogiri::XML::Text, OneOf, Item, String, Ruleref, Tag, Token].freeze

Instance Attribute Summary

Attributes included from RubySpeech::GenericElement

#parent

Instance Method Summary collapse

Methods included from XML::Language

#language, #language=

Methods inherited from Element

module, namespace, root_element, #to_doc

Methods included from RubySpeech::GenericElement

#+, #==, #base_uri, #base_uri=, #build, #children, #clone, #create_node, #embed, #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

Raises:



136
137
138
139
# File 'lib/ruby_speech/grxml/item.rb', line 136

def <<(arg)
  raise InvalidChildError, "A Item can only accept String, Ruleref, Tag or Token as children" unless VALID_CHILD_TYPES.include? arg.class
  super
end

#eql?(o) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/ruby_speech/grxml/item.rb', line 141

def eql?(o)
  super o, :weight, :repeat
end

#regexp_contentObject

:nodoc:



145
146
147
148
149
150
151
152
153
154
# File 'lib/ruby_speech/grxml/item.rb', line 145

def regexp_content # :nodoc:
  case repeat
  when Range
    "#{super}{#{repeat.min},#{repeat.max unless repeat.max == Inf}}"
  when Integer
    "#{super}{#{repeat}}"
  else
    super
  end
end

#repeatString

The repeat attribute

Returns:

  • (String)


77
78
79
80
81
82
83
84
85
86
# File 'lib/ruby_speech/grxml/item.rb', line 77

def repeat
  repeat = read_attr :repeat
  return nil unless repeat
  if repeat.include?('-')
    min, max = repeat.split('-').map &:to_i
    (min || 0)..(max || Inf)
  else
    repeat.to_i
  end
end

#repeat=(r) ⇒ Object

TODO: Raise ArgumentError after doing checking. See

http://www.w3.org/TR/speech-grammar/#S2.5

Parameters:

  • r (String)


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ruby_speech/grxml/item.rb', line 95

def repeat=(r)
  r = "#{r.min}-#{r.max unless r.max == Inf}" if r.is_a?(Range)
  r = r.to_s
  error = ArgumentError.new "A Item's repeat must be 0 or a positive integer"

  raise error unless r.match(/[^0-9-]/) == nil and r.scan("-").size <= 1

  raise error if case di = r.index('-')
  when nil
    r.to_i < 0  # must be 0 or a positive number
  when 0
    true  # negative numbers are illegal
  else
    if di == r.length - 1  # repeat 'm' or more times, m must be 0 or a positive number
      r[0, r.length - 1].to_i < 0
    else  # verify range m,n is valid
      m, n = r.split('-').map &:to_i
      m < 0 || n < m
    end
  end
  self[:repeat] = r
end

#repeat_probFloat

The optional repeat-prob attribute

Returns:

  • (Float)


124
125
126
# File 'lib/ruby_speech/grxml/item.rb', line 124

def repeat_prob
  read_attr :'repeat-prob', :to_f
end

#repeat_prob=(rp) ⇒ Object

Parameters:

  • ia (Numeric)

Raises:

  • (ArgumentError)


131
132
133
134
# File 'lib/ruby_speech/grxml/item.rb', line 131

def repeat_prob=(rp)
  raise ArgumentError, "A Item's repeat probablity attribute must be a floating point number between 0.0 and 1.0" unless rp.to_s.match(/[^0-9\.]/) == nil and rp.to_f >= 0 and rp.to_f <= 1.0
  self['repeat-prob'] = rp
end

#weightFloat

The optional weight attribute

Returns:

  • (Float)


54
55
56
# File 'lib/ruby_speech/grxml/item.rb', line 54

def weight
  read_attr :weight, :to_f
end

#weight=(w) ⇒ Object

The weight attribute takes a positive (floating point) number NOTE: the standard says a format of “n” is valid (eg. an Integer) TODO: possibly support string and check to see if its a valid digit with regex…

Parameters:

  • w (Numeric)

Raises:

  • (ArgumentError)


66
67
68
69
# File 'lib/ruby_speech/grxml/item.rb', line 66

def weight=(w)
  raise ArgumentError, "A Item's weight attribute must be a positive floating point number" unless w.to_s.match(/[^0-9\.]/) == nil and w.to_f >= 0
  self[:weight] = w
end