|
1 |
| -#coding: utf8 |
| 1 | +# coding: utf8 |
| 2 | + |
2 | 3 | from abc import ABCMeta, abstractmethod
|
3 | 4 |
|
4 |
| -context = {"number": {}, "unit": {}} |
5 |
| -context["number"][u"零"] = 0 |
6 |
| -context["number"][u"一"] = 1 |
7 |
| -context["number"][u"二"] = 2 |
8 |
| -context["number"][u"三"] = 3 |
9 |
| -context["number"][u"四"] = 4 |
10 |
| -context["number"][u"五"] = 5 |
11 |
| -context["number"][u"六"] = 6 |
12 |
| -context["number"][u"七"] = 7 |
13 |
| -context["number"][u"八"] = 8 |
14 |
| -context["number"][u"九"] = 9 |
15 |
| - |
16 |
| -context["unit"][u"十"] = 10 |
17 |
| -context["unit"][u"百"] = 100 |
18 |
| -context["unit"][u"千"] = 1000 |
19 |
| -context["unit"][u"万"] = 10000 |
20 |
| -context["unit"][u"亿"] = 100000000 |
21 |
| - |
22 |
| -class Expression: |
| 5 | + |
| 6 | +class Expression(object): |
23 | 7 | __metaclass__ = ABCMeta
|
24 | 8 |
|
25 | 9 | @abstractmethod
|
26 |
| - def interpreter(self, context): |
| 10 | + def interpret(self): |
27 | 11 | pass
|
28 | 12 |
|
29 | 13 | @abstractmethod
|
30 |
| - def get_priority(self, context): |
| 14 | + def get_priority(self): |
31 | 15 | pass
|
32 | 16 |
|
| 17 | + |
33 | 18 | class NumberExpression(Expression):
|
| 19 | + cn_to_number = {} |
| 20 | + cn_to_number[u"零"] = 0 |
| 21 | + cn_to_number[u"一"] = 1 |
| 22 | + cn_to_number[u"二"] = 2 |
| 23 | + cn_to_number[u"三"] = 3 |
| 24 | + cn_to_number[u"四"] = 4 |
| 25 | + cn_to_number[u"五"] = 5 |
| 26 | + cn_to_number[u"六"] = 6 |
| 27 | + cn_to_number[u"七"] = 7 |
| 28 | + cn_to_number[u"八"] = 8 |
| 29 | + cn_to_number[u"九"] = 9 |
| 30 | + |
34 | 31 | def __init__(self, chinese_number):
|
35 |
| - self._cn = chinese_number |
| 32 | + self._cn = chinese_number |
36 | 33 |
|
37 |
| - def interpreter(self, context): |
38 |
| - return context["number"][self._cn] |
| 34 | + def interpret(self): |
| 35 | + return self.cn_to_number[self._cn] |
39 | 36 |
|
40 |
| - def get_priority(self, context): |
| 37 | + def get_priority(self): |
41 | 38 | return 1
|
42 | 39 |
|
| 40 | + |
43 | 41 | class UnitExpression(Expression):
|
| 42 | + unit_to_number = {} |
| 43 | + unit_to_number[u"十"] = 10 ** 1 |
| 44 | + unit_to_number[u"百"] = 10 ** 2 |
| 45 | + unit_to_number[u"千"] = 10 ** 3 |
| 46 | + unit_to_number[u"万"] = 10 ** 4 |
| 47 | + unit_to_number[u"亿"] = 10 ** 8 |
| 48 | + |
44 | 49 | def __init__(self, unit, lefts):
|
45 | 50 | self._unit = unit
|
46 | 51 | self._lefts = lefts
|
47 | 52 |
|
48 |
| - def interpreter(self, context): |
49 |
| - base = sum([left.interpreter(context) for |
50 |
| - left in self._lefts]) |
51 |
| - return base * context["unit"][self._unit] |
| 53 | + def interpret(self): |
| 54 | + base = sum([left.interpret() for left in self._lefts]) |
| 55 | + return base * self.unit_to_number[self._unit] |
52 | 56 |
|
53 |
| - def get_priority(self, context): |
54 |
| - return context["unit"][self._unit] |
| 57 | + def get_priority(self): |
| 58 | + return self.unit_to_number[self._unit] |
55 | 59 |
|
56 |
| -class Converter: |
57 |
| - def __init__(self): |
58 |
| - pass |
59 | 60 |
|
60 |
| - def convert(self, chinese_number): |
61 |
| - global context |
62 |
| - cn = chinese_number |
| 61 | +class Context(object): |
| 62 | + def __init__(self, chinese_number): |
| 63 | + self._cn = chinese_number |
| 64 | + |
| 65 | + def convert(self): |
63 | 66 | stack = []
|
64 | 67 |
|
65 |
| - for word in cn: |
66 |
| - if word == u"零": |
| 68 | + for char in self._cn: |
| 69 | + if char == u"零": |
67 | 70 | continue
|
68 |
| - if word in context["number"]: |
69 |
| - stack.append(NumberExpression(word)) |
| 71 | + if char in NumberExpression.cn_to_number: |
| 72 | + stack.append(NumberExpression(char)) |
70 | 73 | continue
|
71 |
| - if word in context["unit"]: |
| 74 | + if char in UnitExpression.unit_to_number: |
72 | 75 | lefts = []
|
73 |
| - for ind in range(len(stack)-1, -1, -1): |
74 |
| - if stack[ind].get_priority(context) > \ |
75 |
| - context["unit"][word]: |
| 76 | + for ind in range(len(stack) - 1, -1, -1): |
| 77 | + if stack[ind].get_priority() > UnitExpression.unit_to_number[char]: |
76 | 78 | break
|
77 | 79 | lefts.insert(0, stack.pop(ind))
|
78 |
| - stack.append(UnitExpression(word, lefts)) |
79 |
| - return sum(item.interpreter(context) for item in stack) |
| 80 | + stack.append(UnitExpression(char, lefts)) |
| 81 | + continue |
| 82 | + raise ValueError("unknown character") |
| 83 | + return sum(item.interpret() for item in stack) |
80 | 84 |
|
81 |
| -if __name__ == "__main__": |
82 |
| - input_string = raw_input(u"请输入汉字表示:".encode("utf8")) |
83 |
| - print Converter().convert(unicode(input_string, "utf8")) |
84 | 85 |
|
| 86 | +if __name__ == "__main__": |
| 87 | + print(Context(u"三千二百五十亿四千三百万八千一百二十九").convert()) |
0 commit comments