This article describes the Python implementation of the method of translating the numerals in English words into Arabic numerals. Share to everyone for your reference. The implementation methods are as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104-105 |
Import Re _known = {' zero ': 0, ' one ': 1, ' two ': 2, ' three ': 3, ' four ': 4, ' five ': 5, ' six ': 6, ' seven ': 7, ' eight ': 8 Nine ': 9, ' ten ': Ten, ' eleven ': One, ' twelve ': A, ' thirteen ': "Fourteen": "Fifteen": "Sixteen" : D, ' eighteen ': Nineteen, ' Twenty ': ' Thirty ': ' Forty ': ' Fifty ':--Sixty ': "E, ' Seventy ':" Ighty ': B, ' Ninety ':} def spoken_word_to_number (n): "" Assume n is a positive integer. Assert _positive_integer_number (' nine hundred ') = = 900 assert spoken_word_to_number (' one hundred ') = = Assert Spoken_w Ord_to_number (' eleven ') = = one assert Spoken_word_to_number (' twenty two ') = = Assert Spoken_word_to_number (' Thirty-two ') = = (' Forty two ') = = Spoken_word_to_number assert spoken_word_to_number (' two hundred thirty two ') = = 232 Assert Spoken_word_to_number (' two thirty two ') = = 232 assert spoken_word_to_number (' Nineteen Hundred Eighty Nine ') = = 1989 Assert Spoken_word_to_number (' Nineteen Eighty Nine ') = = 1989 Assert Spoken_word_to_number (' One thousand nine hundred and eighty Nine ') = = 1989 Assert Spoken_word_to_number (' nine Eig Hty ') = = 980 assert Spoken_word_to_number (' nine two ') = = wont to convert this one assert able R (' Nine thousand nine hundred ') = = 9900 Assert spoken_word_to_number (' One thousand nine hundred One ') = = 1901 "" "N = N.lo Wer (). Strip () if n in _known:return _known[n] Else:inputwordarr = Re.split (' [-] ', N) assert Len (Inputwordarr) > 1 #a ll single words are known #Check the pathological case where hundred are at the "end" or "Thousand is" at "End If inputwordarr[- 1] = = ' hundred ': Inputwordarr.append (' zero ') inputwordarr.append (' zero ') if inputwordarr[-1] = = ' thousand ': Inputwordarr.append (' zero ') inputwordarr.append (' zero ') inputwordarr.append (' zero ') if inputwordarr[0] = = ' hundred ': Inputwordarr.insert (0, ' one ') if inputwordarr[0] = = ' thousand ': Inputwordarr.insert (0, ' one ') Inputwordarr = [Word for WOR D in Inputwordarr if Word isn't in [' and ', ' mInus ', ' negative ']] currentposition = ' unit ' prevposition = None output = 0 for word in reversed (Inputwordarr): if current Position = = ' Unit ': number = _known[word] Output + = number If number > 9:currentposition = ' hundred ' Else:currentposi tion = ' Ten ' elif currentposition = = ' Ten ': If Word!= ' hundred ': Number = _known[word] If number < 10:output = Numbe R*10 Else:output + + #else: nothing special currentposition = ' hundred ' elif currentposition = ' hundred ': if Word Not in [' Hundred ', ' thousand ']: Number = _known[word] Output + = number*100 currentposition = ' thousand ' elif word = = ' tho Usand ': currentposition = ' thousand ' else:currentposition = ' hundred ' elif currentposition = ' thousand ': Assert word!= ' Hundred ' If Word!= ' thousand ': number = _known[word] Output = = number*1000 Else:assert "Can ' t be here" = None return (o Utput) |
I hope this article will help you with your Python programming.