lib/ruby_speech/grxml/item.rb in ruby_speech-0.5.0 vs lib/ruby_speech/grxml/item.rb in ruby_speech-0.5.1
- old
+ new
@@ -24,10 +24,11 @@
# 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
#
class Item < Element
+ Inf = 1.0 / 0.0
register :item
VALID_CHILD_TYPES = [Nokogiri::XML::Element, Nokogiri::XML::Text, OneOf, Item, String, Ruleref, Tag, Token].freeze
@@ -59,22 +60,29 @@
# The repeat attribute
#
# @return [String]
#
def repeat
- read_attr :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
##
#
# TODO: Raise ArgumentError after doing checking. See
# http://www.w3.org/TR/speech-grammar/#S2.5
#
# @param [String] r
#
def repeat=(r)
- r = "#{r.min}-#{r.max}" if r.is_a?(Range)
+ 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
@@ -120,16 +128,37 @@
def eql?(o)
super o, :weight, :repeat
end
def regexp_content # :nodoc:
- return super unless repeat
+ case repeat
+ when Range
+ "#{super}{#{repeat.min},#{repeat.max unless repeat.max == Inf}}"
+ when Integer
+ "#{super}{#{repeat}}"
+ else
+ super
+ end
+ end
- if repeat.include?('-')
- min, max = repeat.split '-'
- "#{super}{#{min},#{max}}"
+ def potential_match?(other)
+ tokens = children
+ return false if other.length > max_input_length
+ other.chars.each_with_index do |digit, index|
+ index -= tokens.size until index < tokens.size if repeat
+ return false unless tokens[index].potential_match?(digit)
+ end
+ true
+ end
+
+ def max_input_length # :nodoc:
+ case repeat
+ when Range
+ children.size * repeat.max
+ when Integer
+ children.size * repeat
else
- "#{super}{#{repeat}}"
+ children.size
end
end
end # Item
end # GRXML
end # RubySpeech