def test_unicode(self): for numstr, result in [(u'125', 125), (u'124.5', 124.5), (u'five sixths', Fraction(5, 6)), (u'one million', 1000000)]: guess = str2num(numstr) self.assertEqual(guess, result) self.assertEqual(type(guess), type(result)) for numstr in [u'şimal', u'汉语漢語', u'תירִבְעִ']: self.assertRaises(ValueError, lambda: str2num(numstr))
def test_comma_ints(self): for numstr, result in [('1,234', 1234), ('12,345', 12345), ('123,456', 123456), ('1,234,567', 1234567), ('12,345,678', 12345678), ('123,456,789', 123456789), ('1,234,567,890', 1234567890)]: guess = str2num(numstr) self.assertEqual(guess, result) self.assertEqual(type(guess), type(result)) for numstr, result in [('-1,234', -1234), ('-12,345', -12345), ('-123,456', -123456), ('-1,234,567', -1234567), ('-12,345,678', -12345678), ('-123,456,789', -123456789), ('-1,234,567,890', -1234567890)]: guess = str2num(numstr) self.assertEqual(guess, result) self.assertEqual(type(guess), type(result))
def test_newspaper_ints(self): for numstr, result in [('1 thousand', 10 ** 3), ('1 million', 10 ** 6), ('1 billion', 10 ** 9), ('1 trillion', 10 ** 12), ('1 quadrillion', 10 ** 15), ('1 quintillion', 10 ** 18), ('1 sextillion', 10 ** 21), ('1 septillion', 10 ** 24), ('1 octillion', 10 ** 27), ('1 nonillion', 10 ** 30)]: guess = str2num(numstr) self.assertEqual(guess, result) self.assertEqual(type(guess), type(result)) for numstr, result in [('1.2 million', 1200000), ('12.3 million', 12300000), ('12.34 million', 12340000), ('123.456789 million', 123456789)]: guess = str2num(numstr) self.assertEqual(guess, result) self.assertEqual(type(guess), type(result))
def test_ints(self): for numstr, result in [('0', 0), ('-1', -1), ('1', 1), ('123456789', 123456789), ('-123456789', -123456789)]: guess = str2num(numstr) self.assertEqual(guess, result) self.assertEqual(type(guess), type(result))
def test_comma_floats(self): for numstr, result in [('123,456,789.0', 123456789.0), ('-123,456,789.0', -123456789.0), ('1,000.0', 1000.0)]: guess = str2num(numstr) self.assertEqual(guess, result) self.assertEqual(type(guess), type(result))
def test_cap_and_space(self): for numstr, result in [('TWENTY FIVE', 25), ('Thirty six', 36), ('ONE hundred and Five', 105), (' One Hundred SIX ', 106)]: guess = str2num(numstr) self.assertEqual(guess, result) self.assertEqual(type(guess), type(result))
def test_dashes(self): for numstr, result in [('twenty-six', 26), ('one-hundred nine', 109), ('twenty-five', 25), ('five-sixths', Fraction(5, 6))]: guess = str2num(numstr) self.assertEqual(guess, result) self.assertEqual(type(guess), type(result))
def test_common_expressions(self): for numstr, result in [('four score and seven', 87), ('three dozen', 36), ('a dozen', 12), ('five scores', 100), ('fifty three gross', 7632), ('dozen', 12), ('a dozen dozen', 144)]: guess = str2num(numstr) self.assertEqual(guess, result) self.assertEqual(type(guess), type(result))
def test_floats(self): for numstr, result in [('0.0', 0.0), ('-1.0', -1.0), ('1.0', 1.0), ('123456789.0', 123456789.0), ('-123456789.0', -123456789.0), ('4.32E10', 4.32E10), ('-2.3e-23', -2.3e-23), ('0.00043', 0.00043), ('-0.23', -0.23), ('1000.0', 1000.0)]: guess = str2num(numstr) self.assertEqual(guess, result) self.assertEqual(type(guess), type(result))
def test_fractions(self): for denomstr, denom in [('half', 2), ('third', 3), ('fourth', 4), ('fifth', 5), ('sixth', 6), ('seventh', 7), ('eighth', 8), ('ninth', 9), ('tenth', 10), ('eleventh', 11), ('twelfth', 12), ('thirteenth', 13), ('fourteenth', 14), ('fifteenth', 15), ('sixteenth', 16), ('seventeenth', 17), ('eighteenth', 18), ('nineteenth', 19), ('twentieth', 20), ('thirtieth', 30), ('fortieth', 40), ('fiftieth', 50), ('sixtieth', 60), ('seventieth', 70), ('eightieth', 80), ('ninetieth', 90)]: guess = str2num('one ' + denomstr) self.assertEqual(guess, Fraction(1, denom)) self.assertEqual(type(guess), type(Fraction(1, denom))) guess = str2num('3 ' + denomstr + 's') self.assertEqual(guess, Fraction(3, denom)) self.assertEqual(type(guess), type(Fraction(3, denom)))
def test_word_ints(self): for numstr, result in [('twenty six', 26), ('five hundred twelve', 512), ('one thousand three hundred fifty two', 1352), ('fifteen million and thirty eight', 15000038), ('twelve hundred', 1200)]: guess = str2num(numstr) self.assertEqual(guess, result) self.assertEqual(type(guess), type(result))
def test_ordinary_fractions(self): for numstr, result in[('1/2', Fraction(1, 2)), ('1 / 2', Fraction(1, 2)), ('1/3', Fraction(1, 3)), ('6/5', Fraction(6, 5)), ('6/ 6', Fraction(6, 6)), (' 6,343 /5 ', Fraction(6343, 5)), (' 6/ 6 ', Fraction(6, 6)), ('0/5', Fraction(0, 5)), ('-6/7', Fraction(-6, 7))]: guess = str2num(numstr) self.assertEqual(guess, result) self.assertEqual(type(guess), type(result))
def test_newspaper_ints(self): for numstr, result in [('1 thousand', 10**3), ('1 million', 10**6), ('1 billion', 10**9), ('1 trillion', 10**12), ('1 quadrillion', 10**15), ('1 quintillion', 10**18), ('1 sextillion', 10**21), ('1 septillion', 10**24), ('1 octillion', 10**27), ('1 nonillion', 10**30)]: guess = str2num(numstr) self.assertEqual(guess, result) self.assertEqual(type(guess), type(result)) for numstr, result in [('1.2 million', 1200000), ('12.3 million', 12300000), ('12.34 million', 12340000), ('123.456789 million', 123456789)]: guess = str2num(numstr) self.assertEqual(guess, result) self.assertEqual(type(guess), type(result))
def test_bigfractions(self): for denomstr, denom in [('half', 2), ('third', 3), ('fourth', 4), ('fifth', 5), ('sixth', 6), ('seventh', 7), ('eighth', 8), ('ninth', 9), ('tenth', 10), ('eleventh', 11), ('twelfth', 12), ('thirteenth', 13), ('fourteenth', 14), ('fifteenth', 15), ('sixteenth', 16), ('seventeenth', 17), ('eighteenth', 18), ('nineteenth', 19), ('twentieth', 20), ('thirtieth', 30), ('fortieth', 40), ('fiftieth', 50), ('sixtieth', 60), ('seventieth', 70), ('eightieth', 80), ('ninetieth', 90)]: guess = str2num('twelve million, three hundred forty five' ' thousand, six hundred seventy eight ' + denomstr + 's') self.assertEqual(guess, Fraction(12345678, denom)) self.assertEqual(type(guess), type(Fraction(12345678, denom))) guess = str2num('twelve million, three hundred forty five' ' thousand, six hundred seventy eight and one ' + denomstr) self.assertEqual(guess, 12345678 + Fraction(1, denom)) self.assertEqual(type(guess), type(12345678 + Fraction(1, denom)))
def test_unit_words(self): for numstr, result in [('zero', 0), ('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5), ('six', 6), ('seven', 7), ('eight', 8), ('nine', 9), ('ten', 10), ('eleven', 11), ('twelve', 12), ('thirteen', 13), ('fourteen', 14), ('fifteen', 15), ('sixteen', 16), ('seventeen', 17), ('eighteen', 18), ('nineteen', 19), ('twenty', 20), ('thirty', 30), ('forty', 40), ('fifty', 50), ('sixty', 60), ('seventy', 70), ('eighty', 80), ('ninety', 90)]: guess = str2num(numstr) self.assertEqual(guess, result) self.assertEqual(type(guess), type(result))
def test_ordinary_fractions(self): for numstr, result in [('1/2', Fraction(1, 2)), ('1 / 2', Fraction(1, 2)), ('1/3', Fraction(1, 3)), ('6/5', Fraction(6, 5)), ('6/ 6', Fraction(6, 6)), (' 6,343 /5 ', Fraction(6343, 5)), (' 6/ 6 ', Fraction(6, 6)), ('0/5', Fraction(0, 5)), ('-6/7', Fraction(-6, 7))]: guess = str2num(numstr) self.assertEqual(guess, result) self.assertEqual(type(guess), type(result))
def test_nonints(self): for numstr in [ 'jim', 'zerox', 'bone', 'twos', 'threek', 'fourk', 'and', 'the', 's', 'a', '-', '' ]: self.assertRaises(ValueError, lambda: str2num(numstr))
def test_nonints(self): for numstr in ['jim', 'zerox', 'bone', 'twos', 'threek', 'fourk', 'and', 'the', 's', 'a', '-', '']: self.assertRaises(ValueError, lambda: str2num(numstr))
def test_word_num_mix(self): for numstr, result in [('twenty 6', 26), ('one-hundred 9', 109), ('20 five', 25), ('5 sixths', Fraction(5, 6))]: guess = str2num(numstr) self.assertEqual(guess, result) self.assertEqual(type(guess), type(result))