def time(sentence): time_regex = re.compile('\d{1,2}:\d\d') match_indexes = [] for index, word in enumerate(sentence): if time_regex.match(word): match_indexes += [index] for index in match_indexes: hours, minutes = sentence[index].split(':') sentence[index] = number_string(hours) + ' ' + number_string(minutes) return sentence
def ratio(sentence): year_regex = re.compile('^\d+:\d+$') new_sentence = [] for word in sentence: if year_regex.match(word): first, second = word.split(':') new_sentence += [number_string(first) + ' to ' + number_string(second)] else: new_sentence += [word] return new_sentence
def money(sentence): money_regex = re.compile('[$]') match_indexes = [] for index, word in enumerate(sentence): if money_regex.match(word): match_indexes += [index] sentence = Sentence(sentence) if match_indexes: index = match_indexes[0] first = sentence.adjacent_word(index, offset=1) second = sentence.adjacent_word(index, offset=2) if 'illion' in second: sentence.insert(index, offset=2, word='Dollars') sentence.remove(index) elif '\/' in second: sentence.insert(index, offset=3, word='Dollar') sentence.remove(index) else: sentence.remove(index) sentence[index] = number_string(first, money=True) if len(match_indexes) > 1: return money(str(sentence).split()) return str(sentence).split()
def ordinal(sentence): sentence_str = ' '.join(sentence) ordinal_regex = re.compile('(\d+)(st|nd|rd|th)', flags=re.IGNORECASE) for number, ordinal_ending in ordinal_regex.findall(sentence_str): sentence_str = sentence_str.replace(number + ordinal_ending, number_string(number, ordinal=True)) return sentence_str.split()
def number(sentence): number_regex = re.compile('^([0-9]+[.]*[0-9]*)$') match_indexes = [] for index, word in enumerate(sentence): if number_regex.match(word): match_indexes += [index] for index in match_indexes: sentence[index] = number_string(sentence[index]) return sentence
def get_date_string(day=None, month=None, year=None): month_dict = {1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December'} if day and month and year: day = number_string(str(day), ordinal=True) year = year_string(year) return "{month} {day}, {year}".format(day=day, month=month_dict[month], year=year) elif day and month: day = number_string(str(day), ordinal=True) return "{month} {day}".format(day=day, month=month_dict[month]) elif month and year: year = year_string(year) return "{month} {year}".format(year=year, month=month_dict[month])
def distance(sentence): year_regex = re.compile('(\d+)\'(\d+)\"') new_sentence = [] for word in sentence: if year_regex.match(word): feet, inches = year_regex.search(word).groups() if int(feet) == 1: new_sentence += ['One foot'] else: new_sentence += [number_string(feet) + ' feet'] new_sentence += ['and'] if int(inches) == 1: new_sentence += ['One inch'] else: new_sentence += [number_string(inches) + ' inches '] else: new_sentence += [word] return new_sentence
def year_string(numerical_year): year_str = str(numerical_year) if len(year_str) == 2: if int(year_str) <= 16: year_str = '20' + year_str else: year_str = '19' + year_str start = year_str[:2] ending = year_str[2:] if re.match('2000', year_str): return 'Two Thousand' elif re.match('\d\d00', year_str): return number_string(start) + ' ' + 'hundred' elif re.match('200[1-9]', year_str): return number_string(year_str[:3] + '0') + ' and ' + number_string(year_str[3]) elif re.match('\d\d0[1-9]', year_str): return number_string(start) + ' oh ' + number_string(ending) else: return number_string(start) + ' ' + number_string(ending)