~ubuntu-branches/ubuntu/oneiric/mozc/oneiric

« back to all changes in this revision

Viewing changes to converter/gen_pos_matcher_code.py

  • Committer: Bazaar Package Importer
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2010-07-14 03:26:47 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100714032647-13qjisj6m8cm8jdx
Tags: 0.12.410.102-1
* New upstream release (Closes: #588971).
  - Add mozc-server, mozc-utils-gui and scim-mozc packages.
* Update debian/rules.
  Add --gypdir option to build_mozc.py.
* Update debian/control.
  - Bumped standards-version to 3.9.0.
  - Update description.
* Add mozc icon (Closes: #588972).
* Add patch which revises issue 18.
  ibus_mozc_issue18.patch
* kFreeBSD build support.
  support_kfreebsd.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# Copyright 2010, Google Inc.
 
3
# All rights reserved.
 
4
#
 
5
# Redistribution and use in source and binary forms, with or without
 
6
# modification, are permitted provided that the following conditions are
 
7
# met:
 
8
#
 
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
 
14
# distribution.
 
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.
 
18
#
 
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.
 
30
 
 
31
"""
 
32
A tool to generate POS matcher
 
33
"""
 
34
 
 
35
__author__ = "taku"
 
36
 
 
37
import sys
 
38
import re
 
39
 
 
40
def ReadPOSID(file):
 
41
  pos = {}
 
42
  try:
 
43
    for line in open(file, "r"):
 
44
      fields = line.split()
 
45
      pos[fields[1]] = fields[0]
 
46
    return pos
 
47
  except:
 
48
    print "cannot open %s" % (file)
 
49
    sys.exit(1)
 
50
 
 
51
def PatternToRegexp(pattern):
 
52
  return pattern.replace("*", "[^,]+")
 
53
 
 
54
def GetRange(pos, pattern):
 
55
  if pattern == "*":
 
56
    return ""
 
57
 
 
58
  pat = re.compile(PatternToRegexp(pattern))
 
59
  min = -1
 
60
  max = -1
 
61
  keys = pos.keys()
 
62
  keys.sort()
 
63
 
 
64
  range = []
 
65
 
 
66
  for p in keys:
 
67
    id = pos[p]
 
68
    if pat.match(p):
 
69
      if min == -1:
 
70
        min = id
 
71
        max = id
 
72
      else:
 
73
        max = id
 
74
    else:
 
75
      if min != -1:
 
76
        range.append([min, max])
 
77
        min = -1
 
78
  if min != -1:
 
79
    range.append([min, max])
 
80
 
 
81
  tmp = []
 
82
  for r in range:
 
83
    if r[0] == r[1]:
 
84
      tmp.append("(id == %s)" % (r[0]))
 
85
    else:
 
86
      tmp.append("(id >= %s && id <= %s)" % (r[0], r[1]))
 
87
 
 
88
  if len(tmp) == 0:
 
89
    print "FATAL: No rule fiind %s" % (pattern)
 
90
    sys.exit(-1)
 
91
 
 
92
  return (range[0][0], " || ".join(tmp))
 
93
 
 
94
def main():
 
95
  pos = ReadPOSID(sys.argv[1])
 
96
  print "#include \"base/base.h\""
 
97
  print "namespace mozc {"
 
98
  print "namespace {"
 
99
  print "class POSMatcher {"
 
100
  print " public:"
 
101
 
 
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))
 
107
  print "  }"
 
108
  print "  static bool IsZipcode(uint16 id) {"
 
109
  print "    return (id == %s);" % (len(pos))
 
110
  print "  }"
 
111
 
 
112
  for line in open(sys.argv[2], "r"):
 
113
    if len(line) <= 1 or line[0] == '#':
 
114
      continue
 
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)
 
120
    print "  }"
 
121
    print "  static bool Is%s(uint16 id) {" % (func)
 
122
    print "    return (%s);" % (cond)
 
123
    print "  }"
 
124
 
 
125
  print " private:"
 
126
  print "  POSMatcher() {}"
 
127
  print "  ~POSMatcher() {}"
 
128
  print "};"
 
129
  print "}  // namespace"
 
130
  print "}  // mozc"
 
131
 
 
132
if __name__ == "__main__":
 
133
  main()