natter 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 13f97d6f2534d07f6f183f0bbd8a11ef49bab3a8
4
- data.tar.gz: 7ff275a13e6e7aa256abb16f2a148f2c510b465a
3
+ metadata.gz: d23d6a31af6c237271183dfa0f5fc968e69189ff
4
+ data.tar.gz: 6dfcab88b1e463843c39bb65ba3c3e22d0aeaa8b
5
5
  SHA512:
6
- metadata.gz: 2eb357fc794b72db7015fdf2a8411184b03480cb94370e4a43aa4688c6d4bad64a8cc2869d69c71aacc9bbb7fa083ec0f6161647dd44ca4a624436be4bb08446
7
- data.tar.gz: ff00c69847b2a7c05d691840b35082fcd2e2f8abf808d6a3db71aa35b48dfa7f2841eb8165127f346c02a6cdc526f568fb365f907f584053ccfc4f874fd026d3
6
+ metadata.gz: 447999c1be544e5c8296568f6c291b2490b382a118bc1036f29dbde1ddc7e544e8928c89d3068c96e8583c4b99900e31611bac8ccefacaafc373107ffb96cc4e
7
+ data.tar.gz: d2c2db34529b8b8fabf91b1c91dfe50968f10e66ce1148d3abff0c6c98219cf32ae86fb7d28127b9061ff469024185f5cfc1d81857043d7acfd5a153857e69c1
data/lib/natter/parser.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Natter
2
- # Public: The parser is the main workhorse, responsible for deriving the
2
+ # Public: The parser is the main workhorse, responsible for deriving the
3
3
  # intent from an utterance.
4
4
  class Parser
5
5
  def initialize
@@ -8,6 +8,7 @@ module Natter
8
8
  @intent_cache = Hash.new # key = utterance, value = Intent
9
9
  @rules = Hash.new # key = rule regex pattern, value = Rule object
10
10
  end
11
+
11
12
  # Public: Adds a regex-based Rule to the parser.
12
13
  #
13
14
  # rule - The Natter::Rule to add.
@@ -20,9 +21,21 @@ module Natter
20
21
  @rules[rule.pattern] = rule
21
22
  end
22
23
 
24
+ # Public: Adds one or more regex-based Rules to the parser.
25
+ # A convenience method.
26
+ #
27
+ # rules - Either a Natter::Rule or an array of Natter::Rules.
28
+ def add_rules(rules)
29
+ if rules.kind_of?(Array)
30
+ rules.each { |rule| add_rule(rule) }
31
+ else
32
+ add_rule(rules)
33
+ end
34
+ end
35
+
23
36
  # Public: Adds a pre-computed utterance/intent pair to the parser.
24
- # Used when a specific utterance(s) match a predetermined intent. This saves
25
- # overhead as there is no regex processing required. These utterances are
37
+ # Used when a specific utterance(s) match a predetermined intent. This saves
38
+ # overhead as there is no regex processing required. These utterances are
26
39
  # evaluated before the regex rules.
27
40
  # Multiple examples can be added at once.
28
41
  # Adding an utterance that already exists will overwrite the old one.
@@ -37,12 +50,12 @@ module Natter
37
50
  # add_utterance(['what time is it', 'what is the time'] => Intent.new('currentTime'))
38
51
  # add_utterance(
39
52
  # 'night night' => Intent.new('goodnight'),
40
- # 'lock the door' => Intent.new('lock')
53
+ # 'lock the door' => Intent.new('lock')
41
54
  # )
42
55
  #
43
56
  # Returns nothing.
44
57
  def add_utterance(example)
45
- raise ArgumentError, "Expected {utterance => Intent} or {[utterances] => Intent}" unless example.is_a?(Hash)
58
+ raise ArgumentError, "Expected {utterance => Intent} or {[utterances] => Intent}" unless example.is_a?(Hash)
46
59
  example.map do |utterance, intent|
47
60
  if utterance.kind_of?(Array)
48
61
  utterance.each { |phrase| @known_utterances[phrase] = intent }
@@ -51,7 +64,7 @@ module Natter
51
64
  end
52
65
  end
53
66
  end
54
-
67
+
55
68
  # Public: Analyse an utterance and return any matching intents.
56
69
  #
57
70
  # utterance - The natural language string to analyse
@@ -59,17 +72,17 @@ module Natter
59
72
  # utterance/intent pairs to return rather than re-parsing.
60
73
  # (default: true)
61
74
  #
62
- # Returns an Intent, an array of Intents or nil if the intent cannot be
75
+ # Returns an Intent, an array of Intents or nil if the intent cannot be
63
76
  # determined.
64
77
  def parse(text, use_cache = true)
65
78
  raise ArgumentError, "Cannot parse thin air!" unless text.length > 0
66
-
79
+
67
80
  # Store the original string for later
68
81
  original = text
69
82
 
70
83
  # Tidy up the string for parsing
71
84
  utterance = purify(original)
72
-
85
+
73
86
  if @known_utterances.has_key?(utterance)
74
87
  return @known_utterances[utterance]
75
88
  end
@@ -86,7 +99,7 @@ module Natter
86
99
  else
87
100
  intent = intent_from_match(rule, m)
88
101
  if intent then intents << intent end
89
- end
102
+ end
90
103
  end
91
104
 
92
105
  if intents.empty? then return nil end
@@ -100,9 +113,9 @@ module Natter
100
113
  return intents
101
114
  end
102
115
 
103
- # Internal: Determines the confidence of each intent in the passed array
116
+ # Internal: Determines the confidence of each intent in the passed array
104
117
  # and then sorts them based on the calculated confidence values.
105
- # Basically, if we have more than one intent then whichever intent has the
118
+ # Basically, if we have more than one intent then whichever intent has the
106
119
  # greatest number of entities is likely to be the best match.
107
120
  #
108
121
  # intents - An array of Intent objects.
@@ -144,7 +157,7 @@ module Natter
144
157
  # rule - The Rule definining this intent.
145
158
  # m - The positive regex match.
146
159
  #
147
- # Returns Intent.
160
+ # Returns Intent.
148
161
  def intent_from_match(rule, m)
149
162
  if m.named_captures.empty?
150
163
  # No capture groups found. Double-check the rule doesn't need any entities
@@ -162,7 +175,7 @@ module Natter
162
175
  e = Entity.new(entity.name, entity.type, m.named_captures[entity.name].strip)
163
176
  intent.entities << e
164
177
  else
165
- # Found a named capture group that doesn't match an entity defined
178
+ # Found a named capture group that doesn't match an entity defined
166
179
  # in the rule
167
180
  return nil
168
181
  end
@@ -289,7 +302,7 @@ module Natter
289
302
  end
290
303
 
291
304
  # Expand the contractions within this string.
292
- #
305
+ #
293
306
  # Examples
294
307
  #
295
308
  # t = "I'm hot"
@@ -303,4 +316,4 @@ module Natter
303
316
  return result.strip
304
317
  end
305
318
  end
306
- end
319
+ end
@@ -1,3 +1,3 @@
1
1
  module Natter
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: natter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Garry Pettet
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-27 00:00:00.000000000 Z
11
+ date: 2017-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chronic