Instantly share code, notes, and snippets.

@funktor /min_max_age.py Secret
Last active Oct 15, 2018

Embed
What would you like to do?
def get_min_max_age(sentence):
min_age, max_age = -1.0, -1.0
#get_tokens tokenizes the sentence into words separated by spaces.
#it also replaces any special characters specified in to_replace param with spaces.
#lower_cases the sentence.
#each word is lemmatized using word-net lemmatization.
tokens = get_tokens(sentence, to_replace='[^\w\-\+\&\.]+')
sent = ' '.join(tokens)
numbers = [float(x) for x in re.findall('\d+\.\d+|\d+', sent)]
word_numbers = re.findall(r'\b(one|two|three|four|five|six|seven|eight|nine|ten)\b', sent)
for wd_num in word_numbers:
if wd_num == 'one':
numbers.append(1.0)
elif wd_num == 'two':
numbers.append(2.0)
elif wd_num == 'three':
numbers.append(3.0)
elif wd_num == 'four':
numbers.append(4.0)
elif wd_num == 'five':
numbers.append(5.0)
elif wd_num == 'six':
numbers.append(6.0)
elif wd_num == 'seven':
numbers.append(7.0)
elif wd_num == 'eight':
numbers.append(8.0)
elif wd_num == 'nine':
numbers.append(9.0)
else:
numbers.append(10.0)
periods = re.findall('(year|yr|month)s?', sent)
if len(numbers) == 1:
if len(periods) > 0 and periods[0][:5] == 'month':
min_age = numbers[0]/12.0
max_age = 100.0
else:
min_age = numbers[0]
max_age = 100.0
elif len(numbers) == 2:
if len(periods) > 1:
if periods[0][:5] == 'month' and periods[1][:4] == 'year':
age1, age2 = numbers[0]/12.0, numbers[1]
elif periods[0][:4] == 'year' and periods[1][:5] == 'month':
age1, age2 = numbers[0], numbers[1]/12.0
min_age, max_age = min(age1, age2), max(age1, age2)
else:
min_age, max_age = min(numbers), max(numbers)
if len(periods) > 0 and periods[0][:5] == 'month':
min_age /= 12.0
max_age /= 12.0
if (min_age, max_age) == (-1.0, -1.0) and bool(re.search('all age', sentence, re.IGNORECASE)):
max_age = 100.0
return min_age, max_age
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment