Class: BotFramework::Dialogs::RegExpRecognizer
- Inherits:
-
Object
- Object
- BotFramework::Dialogs::RegExpRecognizer
- Defined in:
- lib/bot_framework/dialogs/reg_exp_recognizer.rb
Instance Attribute Summary collapse
-
#expressions ⇒ Object
Returns the value of attribute expressions.
-
#intent ⇒ Object
Returns the value of attribute intent.
Instance Method Summary collapse
-
#initialize(intent, expressions) ⇒ RegExpRecognizer
constructor
A new instance of RegExpRecognizer.
- #recognize(context) ⇒ Object
Constructor Details
#initialize(intent, expressions) ⇒ RegExpRecognizer
Returns a new instance of RegExpRecognizer.
6 7 8 9 10 11 12 13 |
# File 'lib/bot_framework/dialogs/reg_exp_recognizer.rb', line 6 def initialize(intent,expressions) @intent = intent if expressions.is_a? Regexp @expressions = {'*': expressions} else @expressions = expressions || {} end end |
Instance Attribute Details
#expressions ⇒ Object
Returns the value of attribute expressions.
5 6 7 |
# File 'lib/bot_framework/dialogs/reg_exp_recognizer.rb', line 5 def expressions @expressions end |
#intent ⇒ Object
Returns the value of attribute intent.
5 6 7 |
# File 'lib/bot_framework/dialogs/reg_exp_recognizer.rb', line 5 def intent @intent end |
Instance Method Details
#recognize(context) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/bot_framework/dialogs/reg_exp_recognizer.rb', line 15 def recognize(context) raise ArgumentError, "context must be a hash" unless context.is_a? Hash result = {score: 0.0, intent: nil} if context.fetch(:message, {}).fetch(:text, nil) utterance = context[:message][:text] locale = context[:message][:locale] || :* exp = @expressions[locale] ? @expressions[locale] : @expressions[:*] if exp matches = exp.match(utterance) if matches matched = matches.to_s result[:score] = matched.length / utterance.length result[:intent] = intent result[:expression] = exp result[:matched] = matches end yield nil, result else yield(ExpressionNotFoundForLocale, nil) end else yield(nil, result) end end |