Greg Thompson commited on
Commit
b27dee1
2 Parent(s): da7d340 e7b3771

Merge branch 'staging' of https://huggingface.co/spaces/thompsgj/mathtext-fastapi into staging

Browse files
Files changed (1) hide show
  1. mathtext_fastapi/nlu.py +21 -10
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',
@@ -137,17 +140,25 @@ def run_intent_classification(message_text):
137
  'question',
138
  'easier',
139
  'easy',
140
- 'support'
 
 
141
  ]
142
-
143
- for command in commands:
 
 
 
 
 
144
  try:
145
- ratio = fuzz.ratio(command, message_text.lower())
146
  except:
147
- ratio = 0
148
- if ratio > 80:
149
- nlu_response['data'] = command
150
- nlu_response['confidence'] = ratio / 100
 
151
 
152
  return nlu_response
153
 
 
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',
 
140
  'question',
141
  'easier',
142
  'easy',
143
+ 'support',
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
+ nlu_response['data'] = keyword
161
+ nlu_response['confidence'] = score
162
 
163
  return nlu_response
164