1
# -*- coding: utf-8 -*-
2
# Copyright 2010, Google Inc.
5
# Redistribution and use in source and binary forms, with or without
6
# modification, are permitted provided that the following conditions are
9
# * Redistributions of source code must retain the above copyright
10
# notice, this list of conditions and the following disclaimer.
11
# * Redistributions in binary form must reproduce the above
12
# copyright notice, this list of conditions and the following disclaimer
13
# in the documentation and/or other materials provided with the
15
# * Neither the name of Google Inc. nor the names of its
16
# contributors may be used to endorse or promote products derived from
17
# this software without specific prior written permission.
19
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
A tool to generate POS matcher
43
for line in open(file, "r"):
45
pos[fields[1]] = fields[0]
48
print "cannot open %s" % (file)
51
def PatternToRegexp(pattern):
52
return pattern.replace("*", "[^,]+")
54
def GetRange(pos, pattern):
58
pat = re.compile(PatternToRegexp(pattern))
76
range.append([min, max])
79
range.append([min, max])
84
tmp.append("(id == %s)" % (r[0]))
86
tmp.append("(id >= %s && id <= %s)" % (r[0], r[1]))
89
print "FATAL: No rule fiind %s" % (pattern)
92
return (range[0][0], " || ".join(tmp))
95
pos = ReadPOSID(sys.argv[1])
96
print "#include \"base/base.h\""
97
print "namespace mozc {"
99
print "class POSMatcher {"
102
# Special rule for Zipcode:
103
# TODO(taku): remove this rule after introducuing
104
# a specail POS for handling Zipcode
105
print " static uint16 GetZipcodeId() {"
106
print " return %s;" % (len(pos))
108
print " static bool IsZipcode(uint16 id) {"
109
print " return (id == %s);" % (len(pos))
112
for line in open(sys.argv[2], "r"):
113
if len(line) <= 1 or line[0] == '#':
115
(func, pattern) = line.split()
116
(init_id, cond) = GetRange(pos, pattern);
117
print " // %s \"%s\"" % (func, pattern)
118
print " static uint16 Get%sId() {" % (func)
119
print " return %s;" % (init_id)
121
print " static bool Is%s(uint16 id) {" % (func)
122
print " return (%s);" % (cond)
126
print " POSMatcher() {}"
127
print " ~POSMatcher() {}"
129
print "} // namespace"
132
if __name__ == "__main__":