def test_invalid_scale(self): try: actual = TextToNumEng.convert('hundred four') self.assertIsNone(actual) except Exception as e: #print type(e), str(e) self.assertTrue(isinstance(e, Exception))
def test_convert_999(self): try: expected = 999 actual = TextToNumEng.convert('nine hundred ninety nine') self.assertEqual(actual, expected) except Exception: self.assertTrue(False, traceback.format_exc())
def test_string_empty(self): try: actual = TextToNumEng.convert('') self.fail() self.assertIsNone(actual) except TypeError: pass
def test_year_invalid(self): try: actual = TextToNumEng.convertTryYear('nineteen hundred four') self.assertIsNone(actual) except Exception as e: #print type(e), str(e) self.assertTrue(isinstance(e, Exception))
def test_number(self): expected = 128 actual = "" try: actual = TextToNumEng.convert(expected) self.assertEqual(expected, actual) except TypeError: raise
def test_not_number(self): try: actual = TextToNumEng.convert('hi there') self.assertIsNone(actual) except Exception as e: self.assertTrue(isinstance(e, Exception))
def test_convert_94(self): expected = 94 actual = TextToNumEng.convert('ninety four') self.assertEqual(actual, expected)
def test_convert_tens_ones(self): expected = 92 actual = TextToNumEng.convert('ninety two') self.assertEqual(actual, expected)
def test_list_string(self): words = 'two million fifty thousand one hundred and forty nine'.split() expected = 2050149 actual = TextToNumEng.convert(words) self.assertEqual(actual, expected)
def test_year_2015(self): expected = 2015 actual = TextToNumEng.convertTryYear('twenty fifteen') self.assertEqual(actual, expected)
def test_convert_ones(self): expected = 4 actual = TextToNumEng.convert('four') self.assertEqual(actual, expected)
def test_year_1992(self): expected = 1992 actual = TextToNumEng.convertTryYear('nineteen ninety two') self.assertEqual(actual, expected)
def test_year_984(self): expected = 984 actual = TextToNumEng.convertTryYear("nine eighty four") self.assertEqual(actual, expected)
def test_year_911(self): expected = 911 actual = TextToNumEng.convertTryYear("nine 11") self.assertEqual(actual, expected)
def test_year_1996(self): expected = 1996 actual = TextToNumEng.convertTryYear("nineteen ninety six") self.assertEqual(actual, expected)
def test_convert_hundreds_and_tens_ones(self): expected = 249 actual = TextToNumEng.convert('two hundred and forty nine') self.assertEqual(actual, expected)
def test_convert_millions_thousands_hundreds_and_tens_ones(self): expected = 2050149 actual = TextToNumEng.convert( 'two million fifty thousand one hundred and forty nine') self.assertEqual(actual, expected)
def test_convert_teens(self): expected = 19 actual = TextToNumEng.convert('nineteen') self.assertEqual(actual, expected)
def test_hyphen(self): expected = 41 actual = TextToNumEng.convert('forty-one') self.assertEqual(actual, expected)
def test_convert_tens(self): expected = 20 actual = TextToNumEng.convert('twenty') self.assertEqual(actual, expected)
def test_convert_scales(self): expected = 500 actual = TextToNumEng.convert("five hundred") self.assertEqual(actual, expected)
def getNormOptions(word_string, extended=True): # Returns possible ways to normalize the string options_dict = defaultdict(set) # Number conversion if word_string.isdigit(): word_number = int(word_string) # Convert digits to words try: num_text = NumToTextEng.convert(word_number) options_dict['textnum'].add(num_text) except: pass # Convert digits to year try: num_text = NumToTextEng.convertTryYear(word_number) options_dict['textyear'].add(num_text) except: pass else: if extended and len(word_string.split()) > 1: ext = set() # Hyphenation ext.add('-'.join(word_string.split())) # All one single word ext.add(''.join(word_string.split())) # Strip punctuation (both sides) ext.add(word_string.strip(string.punctuation)) # Strip left punct only ext.add(word_string.lstrip(string.punctuation)) # Strip right punct only ext.add(word_string.rstrip(string.punctuation)) options_dict['extended'].update(ext) wstr = word_string # Deyphenation if '-' in word_string: wstr = word_string.replace('-', ' ') options_dict['text'].add(wstr) # Convert words to digits try: num = TextToNumEng.convertTryYear(wstr) options_dict['num'].add(str(num)) except: pass # Contractions if "'" in word_string: # Expand contractions cont_options = ContractionsEng.expandOptions(word_string) else: # Apply contractions cont_options = ContractionsEng.contractOptions(word_string) if cont_options: options_dict['text'].update(cont_options) return options_dict