Greg Thompson commited on
Commit
b5b1714
1 Parent(s): b1bbf8f

Update keyword matching to work with phrases

Browse files
Files changed (1) hide show
  1. mathtext_fastapi/nlu.py +19 -9
mathtext_fastapi/nlu.py CHANGED
@@ -1,9 +1,12 @@
 
 
1
  from collections.abc import Mapping
2
  from logging import getLogger
3
  import datetime as dt
4
  from dateutil.parser import isoparse
5
 
6
  from fuzzywuzzy import fuzz
 
7
  from mathtext_fastapi.intent_classification import predict_message_intent
8
  from mathtext_fastapi.logging import prepare_message_data_for_logging
9
  from mathtext.sentiment import sentiment
@@ -118,10 +121,10 @@ def run_intent_classification(message_text):
118
  >>> run_intent_classification("hardier")
119
  {'type': 'intent', 'data': 'harder', 'confidence': 0.92}
120
  """
121
- label = ''
122
  ratio = 0
123
  nlu_response = {'type': 'intent', 'data': label, 'confidence': ratio}
124
- commands = [
125
  'easier',
126
  'exit',
127
  'harder',
@@ -141,15 +144,22 @@ def run_intent_classification(message_text):
141
  'skip',
142
  'menu'
143
  ]
144
-
145
- for command in commands:
 
 
 
 
 
146
  try:
147
- ratio = fuzz.ratio(command, message_text.lower())
148
  except:
149
- ratio = 0
150
- if ratio > 80:
151
- nlu_response['data'] = command
152
- nlu_response['confidence'] = ratio / 100
 
 
153
 
154
  return nlu_response
155
 
 
1
+ import re
2
+
3
  from collections.abc import Mapping
4
  from logging import getLogger
5
  import datetime as dt
6
  from dateutil.parser import isoparse
7
 
8
  from fuzzywuzzy import fuzz
9
+ from fuzzywuzzy import process
10
  from mathtext_fastapi.intent_classification import predict_message_intent
11
  from mathtext_fastapi.logging import prepare_message_data_for_logging
12
  from mathtext.sentiment import sentiment
 
121
  >>> run_intent_classification("hardier")
122
  {'type': 'intent', 'data': 'harder', 'confidence': 0.92}
123
  """
124
+ label = 'no_match'
125
  ratio = 0
126
  nlu_response = {'type': 'intent', 'data': label, 'confidence': ratio}
127
+ keywords = [
128
  'easier',
129
  'exit',
130
  'harder',
 
144
  'skip',
145
  'menu'
146
  ]
147
+
148
+ try:
149
+ tokens = re.findall(r"[-a-zA-Z'_]+", message_text.lower())
150
+ except AttributeError:
151
+ tokens = ''
152
+
153
+ for keyword in keywords:
154
  try:
155
+ tok, score = process.extractOne(keyword, tokens, scorer=fuzz.ratio)
156
  except:
157
+ score = 0
158
+
159
+ if score > 80:
160
+ print(f"TOK: {tok}, SCORE: {score}")
161
+ nlu_response['data'] = keyword
162
+ nlu_response['confidence'] = score
163
 
164
  return nlu_response
165