Module: RubySpeech::GRXML::Builtins

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

Class Method Summary collapse

Class Method Details

.boolean(options = {}) ⇒ RubySpeech::GRXML::Grammar

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

.currency(options = nil) ⇒ RubySpeech::GRXML::Grammar

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:



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

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

.date(options = nil) ⇒ RubySpeech::GRXML::Grammar

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

.digits(options = {}) ⇒ RubySpeech::GRXML::Grammar

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



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

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

.number(options = nil) ⇒ RubySpeech::GRXML::Grammar

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:



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

def self.number(options = nil)
  RubySpeech::GRXML.draw mode: :dtmf, root: 'number' do
    rule id: 'number', scope: 'public' do
      one_of do
        item { ruleref uri: '#less_than_one' }
        item { ruleref uri: '#one_or_more' }
      end
    end

    rule id: 'less_than_one' do
      item { '*' }
      item { ruleref uri: '#digit_series' }
    end

    rule id: 'one_or_more' do
      item { ruleref uri: '#digit_series' }
      item repeat: '0-1' do
        item { '*' }
        item(repeat: '0-1') { ruleref uri: '#digit_series' }
      end
    end

    rule(id: 'digit_series') { item(repeat: '1-') { ruleref uri: '#digit' } }

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

.phone(options = nil) ⇒ RubySpeech::GRXML::Grammar

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

Returns:



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ruby_speech/grxml/builtins.rb', line 152

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

.time(options = nil) ⇒ RubySpeech::GRXML::Grammar

Create a grammar for interpreting a time.

Returns:



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/ruby_speech/grxml/builtins.rb', line 179

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