diff options
author | Huang Peng <shawn.p.huang@gmail.com> | 2008-09-03 14:12:40 +0800 |
---|---|---|
committer | Huang Peng <shawn.p.huang@gmail.com> | 2008-09-03 14:12:40 +0800 |
commit | bb4a93cf72378b125124d05d18a4f5eac3b49482 (patch) | |
tree | efd23ad44e56cb2928d59e7995701a726c01847d | |
parent | d70c6f0d300195e10b568990b8bc82acb88efcf1 (diff) | |
download | ibus-anthy-bb4a93cf72378b125124d05d18a4f5eac3b49482.tar.gz |
Support Kana typing mode.
-rw-r--r-- | engine/Makefile.am | 3 | ||||
-rw-r--r-- | engine/engine.py | 93 | ||||
-rw-r--r-- | engine/jastring.py | 17 | ||||
-rw-r--r-- | engine/kana.py | 69 | ||||
-rw-r--r-- | engine/segment.py | 4 |
5 files changed, 153 insertions, 33 deletions
diff --git a/engine/Makefile.am b/engine/Makefile.am index d60b21d..cca358d 100644 --- a/engine/Makefile.am +++ b/engine/Makefile.am @@ -24,8 +24,9 @@ engine_anthy_PYTHON = \ main.py \ tables.py \ jastring.py \ - romaji.py \ segment.py \ + romaji.py \ + kana.py \ $(NULL) engine_anthydir = $(pkgdatadir)/engine diff --git a/engine/engine.py b/engine/engine.py index 7428274..4fbb51b 100644 --- a/engine/engine.py +++ b/engine/engine.py @@ -37,10 +37,6 @@ 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, \ @@ -63,7 +59,7 @@ class Engine(ibus.EngineBase): # init state self.__input_mode = INPUT_MODE_HIRAGANA - self.__typing_method = TYPING_MODE_ROMAJI + self.__typing_mode = jastring.TYPING_MODE_ROMAJI self.__prop_dict = {} self.__lookup_table = ibus.LookupTable(page_size=9) @@ -74,7 +70,7 @@ class Engine(ibus.EngineBase): # reset values of engine def __reset(self): - self.__preedit_ja_string = jastring.JaString() + self.__preedit_ja_string = jastring.JaString(self.__typing_mode) self.__convert_chars = u"" self.__cursor_pos = 0 self.__need_update = False @@ -84,41 +80,66 @@ class Engine(ibus.EngineBase): self.__lookup_table_visible = False def __init_props(self): - props = ibus.PropList() + anthy_props = ibus.PropList() # init input mode properties - mode_prop = ibus.Property(name = u"InputMode", + input_mode_prop = ibus.Property(name = u"InputMode", type = ibus.PROP_TYPE_MENU, label = u"あ", tooltip = _(u"Switch input mode")) - self.__prop_dict[u"InputMode"] = mode_prop + self.__prop_dict[u"InputMode"] = input_mode_prop - mode_props = ibus.PropList() - mode_props.append(ibus.Property(name = u"InputMode.Hiragana", + props = ibus.PropList() + props.append(ibus.Property(name = u"InputMode.Hiragana", type = ibus.PROP_TYPE_RADIO, label = _(u"Hiragana"))) - mode_props.append(ibus.Property(name = u"InputMode.Katakana", - type = ibus.PROP_TYPE_RADIO, - label = _(u"Katakana"))) - mode_props.append(ibus.Property(name = u"InputMode.HalfWidthKatakana", - type = ibus.PROP_TYPE_RADIO, - label = _(u"Half width katakana"))) - mode_props.append(ibus.Property(name = u"InputMode.Latin", - type = ibus.PROP_TYPE_RADIO, - label = _(u"Latin"))) - mode_props.append(ibus.Property(name = u"InputMode.WideLatin", - type = ibus.PROP_TYPE_RADIO, - label = _(u"Wide Latin"))) + props.append(ibus.Property(name = u"InputMode.Katakana", + type = ibus.PROP_TYPE_RADIO, + label = _(u"Katakana"))) + props.append(ibus.Property(name = u"InputMode.HalfWidthKatakana", + type = ibus.PROP_TYPE_RADIO, + label = _(u"Half width katakana"))) + props.append(ibus.Property(name = u"InputMode.Latin", + type = ibus.PROP_TYPE_RADIO, + label = _(u"Latin"))) + props.append(ibus.Property(name = u"InputMode.WideLatin", + type = ibus.PROP_TYPE_RADIO, + label = _(u"Wide Latin"))) + + props[self.__input_mode].set_state(ibus.PROP_STATE_CHECKED) + + for prop in props: + self.__prop_dict[prop.name] = prop + + input_mode_prop.set_sub_props(props) + anthy_props.append(input_mode_prop) - mode_props[self.__input_mode].set_state(ibus.PROP_STATE_CHECKED) + # typing input mode properties + typing_mode_prop = ibus.Property(name = u"TypingMode", + type = ibus.PROP_TYPE_MENU, + label = u"R", + tooltip = _(u"Switch typing mode")) + self.__prop_dict[u"TypingMode"] = typing_mode_prop - for prop in mode_props: + props = ibus.PropList() + props.append(ibus.Property(name = u"TypingMode.Romaji", + type = ibus.PROP_TYPE_RADIO, + label = _(u"Romaji"))) + props.append(ibus.Property(name = u"TypingMode.Kana", + type = ibus.PROP_TYPE_RADIO, + label = _(u"Katakana"))) + # props.append(ibus.Property(name = u"TypingMode.ThumbShift", + # type = ibus.PROP_TYPE_RADIO, + # label = _(u"Thumb shift"))) + props[self.__typing_mode].set_state(ibus.PROP_STATE_CHECKED) + + for prop in props: self.__prop_dict[prop.name] = prop - mode_prop.set_sub_props(mode_props) - props.append(mode_prop) + typing_mode_prop.set_sub_props(props) + anthy_props.append(typing_mode_prop) - return props + return anthy_props def page_up(self): # only process cursor down in convert mode @@ -295,6 +316,22 @@ class Engine(ibus.EngineBase): self.update_property(prop) self.__reset() self.__invalidate() + elif prop_name == u"TypingMode.Romaji": + prop = self.__prop_dict[u"TypingMode"] + prop.label = u"R" + self.__typing_mode = jastring.TYPING_MODE_ROMAJI + self.update_property(prop) + self.__reset() + self.__invalidate() + elif prop_name == u"TypingMode.Kana": + prop = self.__prop_dict[u"TypingMode"] + prop.label = u"か" + self.__typing_mode = jastring.TYPING_MODE_KANA + self.update_property(prop) + self.__reset() + self.__invalidate() + else: + pass def focus_in(self): self.register_properties(self.__prop_list) diff --git a/engine/jastring.py b/engine/jastring.py index 9eef391..db067ef 100644 --- a/engine/jastring.py +++ b/engine/jastring.py @@ -20,15 +20,25 @@ # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. import romaji +import kana + +TYPING_MODE_ROMAJI, \ +TYPING_MODE_KANA, \ +TYPING_MODE_THUMB_SHIFT = range(3) class JaString: - def __init__(self): + def __init__(self, mode=TYPING_MODE_ROMAJI): + self.__mode = mode self.reset() def reset(self): self.__cursor = 0 self.__segments = list() + def set_mode(self, mode): + self.__mode = mode + self.reset() + def insert(self, c): segment_before = None segment_after = None @@ -44,7 +54,10 @@ class JaString: new_segments = segment_after.prepend(c) else: if c != u"\0" and c != u"": - new_segments = [romaji.RomajiSegment(c)] + if self.__mode == TYPING_MODE_ROMAJI: + new_segments = [romaji.RomajiSegment(c)] + elif self.__mode == TYPING_MODE_KANA: + new_segments = [kana.KanaSegment(c)] if new_segments: self.__segments[self.__cursor:self.__cursor] = new_segments self.__cursor += len(new_segments) diff --git a/engine/kana.py b/engine/kana.py new file mode 100644 index 0000000..5510742 --- /dev/null +++ b/engine/kana.py @@ -0,0 +1,69 @@ +# 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 * +import segment + +UNFINISHED_SET = set(u"かきくけこさしすせそたちつてとはひふへほ") + +class KanaSegment(segment.Segment): + def __init__(self, enchars=u"", jachars=u""): + if not jachars: + jachars = kana_typing_rule.get(enchars, u"") + super(KanaSegment, self).__init__(enchars, jachars) + + def is_finished(self): + return self._jachars not in UNFINISHED_SET + + def append(self, enchar): + if enchar == u"\0" or enchar == u"": + return [] + if self._jachars: + text = self._jachars + enchar + jachars = kana_voiced_consonant_rule.get(text, None) + if jachars: + self._enchars = self._enchars + enchar + self._jachars = jachars + return [] + return [KanaSegment(enchar)] + self._enchars = self._enchars + enchar + self._jachars = kana_typing_rule.get(self._enchars, u"") + return [] + + def prepend(self, enchar): + if enchar == u"\0" or enchar == u"": + return [] + if self._enchars == u"": + self._enchars = enchar + self._jachars = kana_typing_rule.get(self._enchars, u"") + return [] + return [KanaSegment(enchar)] + + def pop(self, index=-1): + if index == -1: + index = len(self._enchars) - 1 + if index < 0 or index >= len(self._enchars): + raise IndexError("Out of bound") + enchars = list(self._enchars) + del enchars[index] + self._enchars = u"".join(enchars) + self._jachars = kana_typing_rule.get(self._enchars, u"") diff --git a/engine/segment.py b/engine/segment.py index dcdb6be..7d968fe 100644 --- a/engine/segment.py +++ b/engine/segment.py @@ -58,12 +58,12 @@ class Segment(object): def to_katakana(self): if self._jachars: - return u"".join(map(lambda c: hiragana_katakana_table[c][0], self._jachars)) + return u"".join(map(lambda c: hiragana_katakana_table.get(c, (c, c, 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 u"".join(map(lambda c: hiragana_katakana_table.get(c, (c, c, c))[1], self._jachars)) return self._enchars def to_latin(self): |