Spaces:
Sleeping
Sleeping
Commit ·
eb0f4d2
1
Parent(s): 902d4bf
Update helper.py
Browse files
helper.py
CHANGED
|
@@ -222,4 +222,89 @@ def numeric_number_dot_freetext(text):
|
|
| 222 |
return 0
|
| 223 |
|
| 224 |
except:
|
| 225 |
-
return 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
return 0
|
| 223 |
|
| 224 |
except:
|
| 225 |
+
return 0
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
def convert_into_numeric(num_list):
|
| 229 |
+
'''
|
| 230 |
+
This is a function to convert the identified numbers into a numeric form
|
| 231 |
+
'''
|
| 232 |
+
|
| 233 |
+
if num_list:
|
| 234 |
+
|
| 235 |
+
# at first we examine how many numbers were captured. Only one number should exist
|
| 236 |
+
if len(num_list) > 1:
|
| 237 |
+
return (0,'MAGNITUDE','more_magnitudes')
|
| 238 |
+
|
| 239 |
+
else:
|
| 240 |
+
target_num = num_list[0]
|
| 241 |
+
|
| 242 |
+
# case it is an integer or float, convert it, otherwise move to following cases
|
| 243 |
+
try:
|
| 244 |
+
target_num_float = float(target_num)
|
| 245 |
+
return {'Number' : target_num}
|
| 246 |
+
|
| 247 |
+
except:
|
| 248 |
+
# case that it belongs to one of the patterns of freetext number followed by numeric form etc (all the combinations)
|
| 249 |
+
if "$pattern" in target_num:
|
| 250 |
+
num, _ = target_num.split("$")
|
| 251 |
+
|
| 252 |
+
# Try with this function for all the rest of cases (6 point 5, 6 point five, six point 5)
|
| 253 |
+
num_conversion = numeric_number_dot_freetext(num)
|
| 254 |
+
|
| 255 |
+
if num_conversion:
|
| 256 |
+
return {'Number' : num_conversion}
|
| 257 |
+
|
| 258 |
+
# if none of the above has worked, then examine the case of freetext numbers without patterns (e.g. two, million, twenty three, etc)
|
| 259 |
+
else:
|
| 260 |
+
try:
|
| 261 |
+
num_conversion = w2n.word_to_num(target_num)
|
| 262 |
+
return {'Number' : num_conversion}
|
| 263 |
+
|
| 264 |
+
# if none of the above try to handle cases of "million and two" or "a million and two". In such cases, we delete any 'a' reference
|
| 265 |
+
# and we insert the word 'one' at the beginning. In that way the w2n library can handle them besides immediately throw an error
|
| 266 |
+
except:
|
| 267 |
+
|
| 268 |
+
try:
|
| 269 |
+
target_num = target_num.replace(" a ", " ")
|
| 270 |
+
new_target_num = "one " + target_num
|
| 271 |
+
num_conversion = w2n.word_to_num(new_target_num)
|
| 272 |
+
return {'Number' : num_conversion}
|
| 273 |
+
|
| 274 |
+
except:
|
| 275 |
+
return (0,'MAGNITUDE','unknown_error')
|
| 276 |
+
|
| 277 |
+
else:
|
| 278 |
+
return (0,'MAGNITUDE','no_magnitude')
|
| 279 |
+
|
| 280 |
+
|
| 281 |
+
def magnitude_binding(input_text):
|
| 282 |
+
'''
|
| 283 |
+
This is a function that binds together all the subcomponents of the magnitude number identification, while also controlling for multiple, or zero magnitude references
|
| 284 |
+
'''
|
| 285 |
+
|
| 286 |
+
try:
|
| 287 |
+
|
| 288 |
+
# capture the referred magnitudes
|
| 289 |
+
target_numbers = capture_numbers(input_text)
|
| 290 |
+
|
| 291 |
+
# we only accept for one magnitude reference
|
| 292 |
+
if len(target_numbers) == 1:
|
| 293 |
+
numeric_target_numbers = convert_into_numeric(target_numbers)
|
| 294 |
+
|
| 295 |
+
return numeric_target_numbers
|
| 296 |
+
|
| 297 |
+
# in case of zero references return the appropriate code (to aid returning the correct prompt)
|
| 298 |
+
elif len(target_numbers) == 0:
|
| 299 |
+
return (0,'MAGNITUDE','no_magnitude')
|
| 300 |
+
|
| 301 |
+
# in case of more than one references return the appropriate code (to aid returning the correct prompt)
|
| 302 |
+
elif len(target_numbers) > 1:
|
| 303 |
+
return (0,'MAGNITUDE','more_magnitudes')
|
| 304 |
+
|
| 305 |
+
# in case of unexpected error return the appropriate code (to aid returning the correct prompt)
|
| 306 |
+
else:
|
| 307 |
+
return (0,'MAGNITUDE','unknown_error')
|
| 308 |
+
|
| 309 |
+
except:
|
| 310 |
+
return (0,'MAGNITUDE','unknown_error')
|