summaryrefslogtreecommitdiff
path: root/engine
diff options
context:
space:
mode:
authorHuang Peng <shawn.p.huang@gmail.com>2008-09-03 10:39:03 +0800
committerHuang Peng <shawn.p.huang@gmail.com>2008-09-03 10:39:03 +0800
commit51d1686671b5597e29672a2026ccff7640cf4f3a (patch)
treeabe034a38a2d39032d13cc8b0613a6a27833787e /engine
parent63faa83cc3086f8f57e6425d82cf38b31e53d9c4 (diff)
downloadibus-anthy-51d1686671b5597e29672a2026ccff7640cf4f3a.tar.gz
Refactory Segment
Diffstat (limited to 'engine')
-rw-r--r--engine/Makefile.am1
-rw-r--r--engine/engine.py10
-rw-r--r--engine/romaji.py99
-rw-r--r--engine/segment.py72
-rw-r--r--engine/tables.py2
5 files changed, 110 insertions, 74 deletions
diff --git a/engine/Makefile.am b/engine/Makefile.am
index d9ca635..d60b21d 100644
--- a/engine/Makefile.am
+++ b/engine/Makefile.am
@@ -25,6 +25,7 @@ engine_anthy_PYTHON = \
tables.py \
jastring.py \
romaji.py \
+ segment.py \
$(NULL)
engine_anthydir = $(pkgdatadir)/engine
diff --git a/engine/engine.py b/engine/engine.py
index c134af4..7428274 100644
--- a/engine/engine.py
+++ b/engine/engine.py
@@ -37,6 +37,10 @@ INPUT_MODE_HALF_WIDTH_KATAKANA, \
INPUT_MODE_LATIN, \
INPUT_MODE_WIDE_LATIN = range(5)
+TYPING_MODE_ROMAJI, \
+TYPING_MODE_KANA, \
+TYPING_MODE_THUMB_SHIFT = range(3)
+
CONV_MODE_OFF, \
CONV_MODE_ANTHY, \
CONV_MODE_HIRAGANA, \
@@ -47,8 +51,7 @@ CONV_MODE_LATIN_2, \
CONV_MODE_LATIN_3, \
CONV_MODE_WIDE_LATIN_1, \
CONV_MODE_WIDE_LATIN_2, \
-CONV_MODE_WIDE_LATIN_3, \
-= range(11)
+CONV_MODE_WIDE_LATIN_3 = range(11)
class Engine(ibus.EngineBase):
def __init__(self, bus, object_path):
@@ -60,6 +63,7 @@ class Engine(ibus.EngineBase):
# init state
self.__input_mode = INPUT_MODE_HIRAGANA
+ self.__typing_method = TYPING_MODE_ROMAJI
self.__prop_dict = {}
self.__lookup_table = ibus.LookupTable(page_size=9)
@@ -650,7 +654,7 @@ class Engine(ibus.EngineBase):
elif self.__input_mode == INPUT_MODE_WIDE_LATIN:
# Input Wide Latin chars
char = unichr(keyval)
- wide_char = half_symbol_rule.get(char, None)
+ wide_char = symbol_rule.get(char, None)
if wide_char == None:
wide_char = ibus.unichar_half_to_full(char)
self.__commit_string(wide_char)
diff --git a/engine/romaji.py b/engine/romaji.py
index 91b30ea..e63667f 100644
--- a/engine/romaji.py
+++ b/engine/romaji.py
@@ -21,20 +21,18 @@
from ibus import unichar_half_to_full
from tables import *
+import segment
-class RomajiSegment:
+class RomajiSegment(segment.Segment):
def __init__(self, enchars = u"", jachars = u""):
- self.__enchars = enchars
- if jachars:
- self.__jachars = jachars
- else:
+ if not jachars:
jachars = romaji_typing_rule.get(enchars, None)
if jachars == None:
- jachars = half_symbol_rule.get(enchars, u"")
- self.__jachars = jachars
+ jachars = symbol_rule.get(enchars, u"")
+ super(RomajiSegment, self).__init__(enchars, jachars)
def is_finished(self):
- return self.__jachars != u""
+ return self._jachars != u""
def append(self, enchar):
if self.is_finished():
@@ -42,20 +40,20 @@ class RomajiSegment:
return []
return [RomajiSegment(enchar)]
- text = self.__enchars + enchar
+ text = self._enchars + enchar
jachars = romaji_typing_rule.get(text, None)
if jachars == None:
- jachars = half_symbol_rule.get(text, None)
+ jachars = symbol_rule.get(text, None)
if jachars:
- self.__enchars = text
- self.__jachars = jachars
+ self._enchars = text
+ self._jachars = jachars
return []
jachars, c = romaji_double_consonat_typing_rule.get(text, (None, None))
if jachars:
- self.__enchars = text[0]
- self.__jachars = jachars
+ self._enchars = text[0]
+ self._jachars = jachars
return [RomajiSegment(c)]
for i in range(-min(4, len(text)), 0):
@@ -63,16 +61,16 @@ class RomajiSegment:
jachars = romaji_typing_rule.get(enchars, None)
if jachars == None:
- jachars = half_symbol_rule.get(enchars, None)
+ jachars = symbol_rule.get(enchars, None)
if jachars:
jasegment = RomajiSegment(enchars, jachars)
- self.__enchars = text[:i]
+ self._enchars = text[:i]
return [jasegment]
jachars, c = romaji_double_consonat_typing_rule.get(enchars, (None, None))
if jachars:
jasegment = RomajiSegment(enchars[:-len(c)], jachars)
- self.__enchars = text[:i]
+ self._enchars = text[:i]
if c:
return [jasegment, RomajiSegment(c)]
return [jasegment]
@@ -80,14 +78,12 @@ class RomajiSegment:
jachars, c = romaji_correction_rule.get(enchars, (None, None))
if jachars:
jasegment = RomajiSegment(enchars[:-len(c)], jachars)
- self.__enchars = text[:i]
+ self._enchars = text[:i]
if c:
return [jasegment, RomajiSegment(c)]
return [jasegment]
-
-
- self.__enchars = text
+ self._enchars = text
return []
def prepend(self, enchar):
@@ -97,79 +93,42 @@ class RomajiSegment:
if self.is_finished():
return [RomajiSegment(enchar)]
- text = enchar + self.__enchars
+ text = enchar + self._enchars
jachars = romaji_typing_rule.get(text, None)
- if not jachars:
- jachars = half_symbol_rule.get(text, None)
+ if jachars == None:
+ jachars = symbol_rule.get(text, None)
if jachars:
- self.__enchars = text
- self.__jachars = jachars
+ self._enchars = text
+ self._jachars = jachars
return []
jachars, c = romaji_double_consonat_typing_rule.get(text, (None, None))
if jachars:
- self.__enchars = c
+ self._enchars = c
return [RomajiSegment(text[0], jachars)]
for i in range(min(4, len(text)), 0, -1):
enchars = text[:i]
jachars = romaji_typing_rule.get(enchars, None)
- if not jachars:
- jachars = half_symbol_rule.get(enchars, None)
+ if jachars == None:
+ jachars = symbol_rule.get(enchars, None)
if jachars:
jasegment = RomajiSegment(enchars, jachars)
- self.__enchars = text[i:]
+ self._enchars = text[i:]
return [jasegment]
jachars, c = romaji_double_consonat_typing_rule.get(enchars, (None, None))
if jachars:
- self.__enchars = c + text[i:]
+ self._enchars = c + text[i:]
return [RomajiSegment(enchars[:-len(c)], jachars)]
jachars, c = romaji_correction_rule.get(enchars, (None, None))
if jachars:
- self.__enchars = c + text[i:]
+ self._enchars = c + text[i:]
return [RomajiSegment(enchars[:-len(c)], jachars)]
- self.__enchars = text
+ self._enchars = text
return []
- def set_enchars(self, enchars):
- self.__enchars = enchars
-
- def get_enchars(self):
- return self.__enchars
-
- def set_jachars(self, jachars):
- self.__jachars = jachars
-
- def get_jachars(self):
- return self.__jachars
-
- def to_hiragana(self):
- if self.__jachars:
- return self.__jachars
- return self.__enchars
-
- def to_katakana(self):
- if self.__jachars:
- return u"".join(map(lambda c: hiragana_katakana_table[c][0], self.__jachars))
- return self.__enchars
-
- def to_half_width_katakana(self):
- if self.__jachars:
- return u"".join(map(lambda c: hiragana_katakana_table[c][1], self.__jachars))
- return self.__enchars
-
- def to_latin(self):
- return self.__enchars
-
- def to_wide_latin(self):
- return u"".join(map(unichar_half_to_full, self.__enchars))
-
- def is_empty(self):
- if self.__enchars or self.__jachars:
- return False
- return True
diff --git a/engine/segment.py b/engine/segment.py
new file mode 100644
index 0000000..8e8e547
--- /dev/null
+++ b/engine/segment.py
@@ -0,0 +1,72 @@
+# vim:set et sts=4 sw=4:
+# -*- coding: utf-8 -*-
+#
+# ibus-anthy - The Anthy engine for IBus
+#
+# Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+from ibus import unichar_half_to_full
+from tables import *
+
+class Segment(object):
+ def __init__(self, enchars, jachars):
+ self._enchars = enchars
+ self._jachars = jachars
+
+ def append(self, enchar):
+ raise NotImplementedError("append is not implemented")
+
+ def prepend(self, enchar):
+ raise NotImplementedError("prepend is not implemented")
+
+ def set_enchars(self, enchars):
+ self.enchars = enchars
+
+ def get_enchars(self):
+ return self._enchars
+
+ def set_jachars(self, jachars):
+ self._jachars = jachars
+
+ def get_jachars(self):
+ return self._jachars
+
+ def to_hiragana(self):
+ if self._jachars:
+ return self._jachars
+ return self._enchars
+
+ def to_katakana(self):
+ if self._jachars:
+ return u"".join(map(lambda c: hiragana_katakana_table[c][0], self._jachars))
+ return self._enchars
+
+ def to_half_width_katakana(self):
+ if self._jachars:
+ return u"".join(map(lambda c: hiragana_katakana_table[c][1], self._jachars))
+ return self._enchars
+
+ def to_latin(self):
+ return self._enchars
+
+ def to_wide_latin(self):
+ return u"".join(map(unichar_half_to_full, self._enchars))
+
+ def is_empty(self):
+ if self._enchars or self._jachars:
+ return False
+ return True
diff --git a/engine/tables.py b/engine/tables.py
index 0cc3e23..733f040 100644
--- a/engine/tables.py
+++ b/engine/tables.py
@@ -226,7 +226,7 @@ romaji_typing_rule = {
u"wye" : u"ゑ",
}
-half_symbol_rule = {
+symbol_rule = {
# symbols
u"," : u"、",
u"." : u"。",