summary refs log tree commit diff
path: root/numberparser.py
blob: f02bb6e39fd1832ce47e4aff9d4ebf6b3945327a (plain)
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
43
44
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
# numberparser.py - Translate words to decimal

# This is part of Kaylee
# -- this code is licensed GPLv3
# Copyright 2015 Clayton G. Hobbs
# Portions Copyright 2013 Jezra
import re

# Define the mappings from words to numbers
class NumberParser:
    zero = {
        'zero': 0
    }

    ones = {
        'one': 1,
        'two': 2,
        'three': 3,
        'four': 4,
        'five': 5,
        'six': 6,
        'seven': 7,
        'eight': 8,
        'nine': 9
    }

    special_ones = {
        'ten': 10,
        'eleven': 11,
        'twelve': 12,
        'thirteen': 13,
        'fourteen': 14,
        'fifteen': 15,
        'sixteen': 16,
        'seventeen': 17,
        'eighteen': 18,
        'ninteen': 19
    }

    tens = {
        'twenty': 20,
        'thirty': 30,
        'fourty': 40,
        'fifty': 50,
        'sixty': 60,
        'seventy': 70,
        'eighty': 80,
        'ninty': 90
    }

    hundred = {
        'hundred': 100
    }

    exp = {
        'thousand': 1000,
        'million': 1000000,
        'billion': 1000000000
    }

    allowed = [
        'and'
    ]

    def __init__(self):
        self.number_words = []
        for word in sorted(self.zero.keys()):
            self.number_words.append(word)
        for word in sorted(self.ones.keys()):
            self.number_words.append(word)
        for word in sorted(self.special_ones.keys()):
            self.number_words.append(word)
        for word in sorted(self.tens.keys()):
            self.number_words.append(word)
        for word in sorted(self.hundred.keys()):
            self.number_words.append(word)
        for word in sorted(self.exp.keys()):
            self.number_words.append(word)
        self.mandatory_number_words = self.number_words.copy()
        for word in sorted(self.allowed):
            self.number_words.append(word)

    def parse_number(self, text_line):
        """
        Parse numbers from natural language into ints

        TODO: Throw more exceptions when invalid numbers are detected.  Only
        allow certian valueless words within numbers.  Support zero.
        """
        value = 0
        partial_value = 0
        last_list = None

        # Split text_line by commas, whitespace, and hyphens
        text_line = text_line.strip()
        text_words = re.split(r'[,\s-]+', text_line)
        # Parse the number
        for word in text_words:
            if word in self.zero:
                if last_list is not None:
                    raise ValueError('Invalid number')
                value = 0
                last_list = self.zero
            elif word in self.ones:
                if last_list in (self.zero, self.ones, self.special_ones):
                    raise ValueError('Invalid number')
                value += self.ones[word]
                last_list = self.ones
            elif word in self.special_ones:
                if last_list in (self.zero, self.ones, self.special_ones, self.tens):
                    raise ValueError('Invalid number')
                value += self.special_ones[word]
                last_list = self.special_ones
            elif word in self.tens:
                if last_list in (self.zero, self.ones, self.special_ones, self.tens):
                    raise ValueError('Invalid number')
                value += self.tens[word]
                last_list = self.tens
            elif word in self.hundred:
                if last_list not in (self.ones, self.special_ones, self.tens):
                    raise ValueError('Invalid number')
                value *= self.hundred[word]
                last_list = self.hundred
            elif word in self.exp:
                if last_list in (self.zero, self.exp):
                    raise ValueError('Invalid number')
                partial_value += value * self.exp[word]
                value = 0
                last_list = self.exp
            elif word not in self.allowed:
                raise ValueError('Invalid number')
            # Debugging information
            #print(word, value, partial_value)
        # Finish parsing the number
        value += partial_value
        return value

    def parse_all_numbers(self, text_line):
        nums = []
        t_numless = ''

        # Split text_line by commas, whitespace, and hyphens
        text_words = re.split(r'[,\s-]+', text_line.strip())
        # Get a string of word classes
        tw_classes = ''
        for word in text_words:
            if word in self.mandatory_number_words:
                tw_classes += 'm'
            elif word in self.allowed:
                tw_classes += 'a'
            else:
                tw_classes += 'w'

        # For each string of number words:
        last_end = 0
        for m in re.finditer('m[am]*m|m', tw_classes):
            # Get the number words
            num_words = ' '.join(text_words[m.start():m.end()])
            # Parse the number and store the value
            try:
                nums.append(self.parse_number(num_words))
            except ValueError:
                nums.append(-1)
            # Add words to t_numless
            t_numless += ' '.join(text_words[last_end:m.start()]) + ' %d '
            last_end = m.end()
        t_numless += ' '.join(text_words[last_end:])

        return (t_numless.strip(), nums)

if __name__ == '__main__':
    np = NumberParser()
    # Get the words to translate
    text_line = input('Enter a string: ')
    # Parse it to an integer
    value = np.parse_all_numbers(text_line)
    # Print the result
    print(value)