Module: RubySpeech::GRXML::Builtins

Defined in:
lib/ruby_speech/grxml/builtins.rb

Class Method Summary (collapse)

Class Method Details

+ (RubySpeech::GRXML::Grammar) boolean(options = {})

Create a grammar for interpreting a boolean response, where 1 is true and two is false.

Parameters:

  • options (Hash) (defaults to: {})

    Options to parameterize the grammar

Options Hash (options):

  • :y (#to_s)

    The positive/truthy/affirmative digit

  • :n (#to_s)

    The negative/falsy digit

Returns:

Raises:

  • (ArgumentError)

    if the :y and :n options are the same



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby_speech/grxml/builtins.rb', line 13

def self.boolean(options = {})
  truthy_digit = (options[:y] || options['y'] || '1').to_s
  falsy_digit = (options[:n] || options['n'] || '2').to_s

  raise ArgumentError, "Yes and no values cannot be the same" if truthy_digit == falsy_digit

  RubySpeech::GRXML.draw mode: :dtmf, root: 'boolean' do
    rule id: 'boolean', scope: 'public' do
      one_of do
        item do
          tag { 'true' }
          truthy_digit
        end
        item do
          tag { 'false' }
          falsy_digit
        end
      end
    end
  end
end

+ (RubySpeech::GRXML::Grammar) currency(options = nil)

Create a grammar for interpreting a monetary value. Uses '*' as the decimal point. Matches any number of digits, optionally followed by a '*' and up to two more digits.

Returns:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ruby_speech/grxml/builtins.rb', line 90

def self.currency(options = nil)
  RubySpeech::GRXML.draw mode: :dtmf, root: 'currency' do
    rule id: 'currency', scope: 'public' do
      item repeat: '0-' do
        ruleref uri: '#digit'
      end
      item repeat: '0-1' do
        item { '*' }
        item repeat: '0-2' do
          ruleref uri: '#digit'
        end
      end
    end

    rule id: 'digit' do
      one_of do
        0.upto(9) { |d| item { d.to_s } }
      end
    end
  end
end

+ (RubySpeech::GRXML::Grammar) date(options = nil)

Create a grammar for interpreting a date.

Returns:



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby_speech/grxml/builtins.rb', line 40

def self.date(options = nil)
  RubySpeech::GRXML.draw mode: :dtmf, root: 'date' do
    rule id: 'date', scope: 'public' do
      item repeat: '8' do
        one_of do
          0.upto(9) { |d| item { d.to_s } }
        end
      end
    end
  end
end

+ (RubySpeech::GRXML::Grammar) digits(options = {})

Create a grammar for interpreting a string of digits.

Parameters:

  • options (Hash) (defaults to: {})

    Options to parameterize the grammar

Options Hash (options):

  • :minlength (#to_i)

    Minimum length for the string of digits.

  • :maxlength (#to_i)

    Maximum length for the string of digits.

  • :length (#to_i)

    Absolute length for the string of digits.

Returns:

Raises:

  • (ArgumentError)

    if any of the length attributes logically conflict



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ruby_speech/grxml/builtins.rb', line 64

def self.digits(options = {})
  raise ArgumentError, "Cannot specify both absolute length and a length range" if options[:length] && (options[:minlength] || options[:maxlength])

  minlength = options[:minlength] || options['minlength'] || 0
  maxlength = options[:maxlength] || options['maxlength']
  length = options[:length] || options['length']

  repeat = length ? length : "#{minlength}-#{maxlength}"

  RubySpeech::GRXML.draw mode: :dtmf, root: 'digits' do
    rule id: 'digits', scope: 'public' do
      item repeat: repeat do
        one_of do
          0.upto(9) { |d| item { d.to_s } }
        end
      end
    end
  end
end

+ (RubySpeech::GRXML::Grammar) number(options = nil)

Create a grammar for interpreting a numeric value. Uses '*' as the decimal point. Matches any number of digits, optionally followed by a '*' and any number more digits.

Returns:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ruby_speech/grxml/builtins.rb', line 118

def self.number(options = nil)
  RubySpeech::GRXML.draw mode: :dtmf, root: 'number' do
    rule id: 'number', scope: 'public' do
      item repeat: '0-' do
        ruleref uri: '#digit'
      end
      item repeat: '0-1' do
        item { '*' }
        item repeat: '0-' do
          ruleref uri: '#digit'
        end
      end
    end

    rule id: 'digit' do
      one_of do
        0.upto(9) { |d| item { d.to_s } }
      end
    end
  end
end

+ (RubySpeech::GRXML::Grammar) phone(options = nil)

Create a grammar for interpreting a phone number. Uses '*' to represent 'x' for a number with an extension.

Returns:



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/ruby_speech/grxml/builtins.rb', line 145

def self.phone(options = nil)
  RubySpeech::GRXML.draw mode: :dtmf, root: 'number' do
    rule id: 'number', scope: 'public' do
      item repeat: '1-' do
        ruleref uri: '#digit'
      end
      item repeat: '0-1' do
        item { '*' }
        item repeat: '0-' do
          ruleref uri: '#digit'
        end
      end
    end

    rule id: 'digit' do
      one_of do
        0.upto(9) { |d| item { d.to_s } }
      end
    end
  end
end

+ (RubySpeech::GRXML::Grammar) time(options = nil)

Create a grammar for interpreting a time.

Returns:



172
173
174
175
176
177
178
179
180
181
182
# File 'lib/ruby_speech/grxml/builtins.rb', line 172

def self.time(options = nil)
  RubySpeech::GRXML.draw mode: :dtmf, root: 'time' do
    rule id: 'time', scope: 'public' do
      item repeat: '1-4' do
        one_of do
          0.upto(9) { |d| item { d.to_s } }
        end
      end
    end
  end
end