PageRenderTime 32ms CodeModel.GetById 2ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/python/engine/PinYin/ShuangPin.py

http://scim-python.googlecode.com/
Python | 204 lines | 176 code | 3 blank | 25 comment | 0 complexity | 99a9f80bd8f659db8365c5a80701d0dd MD5 | raw file
  1# -*- coding: utf-8 -*-
  2# vim: set noet ts=4:
  3#
  4# scim-python
  5#
  6# Copyright (c) 2007-2008 Yu Fan <yufanyufan@gmail.com>
  7#
  8#
  9# This library is free software; you can redistribute it and/or
 10# modify it under the terms of the GNU Lesser General Public
 11# License as published by the Free Software Foundation; either
 12# version 2 of the License, or (at your option) any later version.
 13#
 14# This library is distributed in the hope that it will be useful,
 15# but WITHOUT ANY WARRANTY; without even the implied warranty of
 16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17# GNU Lesser General Public License for more details.
 18#
 19# You should have received a copy of the GNU Lesser General Public
 20# License along with this program; if not, write to the
 21# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 22# Boston, MA  02111-1307  USA
 23#
 24# $Id: $
 25#
 26from ZhengJu import *
 27import scim
 28import os
 29from scim import KeyCode
 30from scim import KeyMask
 31from scim import Property
 32import traceback
 33import sys
 34from PYDict import *
 35from gettext import dgettext
 36
 37_ = lambda a : dgettext ("scim-python", a)
 38class ShuangPinEngine (Engine):
 39
 40	def __init__ (self, factory, config, encoding, id):
 41		Engine.__init__(self, factory, config, encoding, id)
 42		
 43	def clear(self):
 44		self.extra_string = []
 45		Engine.clear(self)
 46	def get_extra_string(self):
 47		return "".join(self.extra_string)
 48	def reload_config(self,config):
 49		self.reset()
 50		self.schema = config.read ("/IMEngine/Python/ZhengJu/ShuangPinSchema", "MSPY")
 51		#~ print self.schema
 52		if not self.schema in SHUANGPIN_SCHEMAS.keys ():
 53			self.schima = "MSPY"
 54		(self.shengmu_schema, self.yunmu_schema) = SHUANGPIN_SCHEMAS[self.schema]
 55		Engine.reload_config(self,config)
 56		
 57	def translate(self,key):
 58		if(self._editor.current () == None or self._editor.current ().is_complete ()):
 59			if unichr (key.code) in self.shengmu_schema:
 60				self._editor.pinyinlist.append (PinYinWord(self.shengmu_schema[unichr (key.code)],""))
 61				if not self.progresivepromp:
 62					return
 63			else:
 64				raise InputException()
 65		else:
 66			if unichr(key.code) in self.yunmu_schema:
 67				yunmu = self.yunmu_schema[unichr(key.code)]
 68				p = None
 69				for i in yunmu:
 70					pinyin = self._editor.current ().get_shengmu () + i
 71					if pinyin in PINYIN_LIST:
 72						p = i
 73						break
 74				if p == None:
 75					raise InputException ()
 76				self._editor.current().set_yunmu (p)
 77			else:
 78				raise InputException
 79		self._editor.auto_convert ()
 80		
 81
 82
 83	def chinese_process_key_event (self, key):
 84		if (self.schema=="MSPY" or self.schema=="ZGPY") and key.mask == KeyMask.NullMask and (\
 85			(key.code >= KeyCode.KEY_a and key.code <= KeyCode.KEY_z) or \
 86			key.code == KeyCode.KEY_semicolon):
 87			self.translate(key)
 88			return True
 89		elif key.mask == KeyMask.NullMask \
 90			and (key.code >= KeyCode.KEY_a and key.code <= KeyCode.KEY_z):
 91			self.translate(key)
 92			return True
 93		elif self._editor.pinyinlist and key.code == KeyCode.KEY_BackSpace:
 94			if self._editor.pinyinlist[-1].is_complete ():
 95				self._editor.pinyinlist[-1].set_yunmu ("")
 96				if not self.progresivepromp:
 97					return True
 98			else:
 99				del self._editor.pinyinlist[-1]
100			self._editor.update ()
101			return True
102		elif (self.extra_string or self._editor.pinyinlist) and (key.code == KeyCode.KEY_Left or key.code == KeyCode.KEY_f and key.mask & KeyMask.ControlMask):
103			if self._editor.pinyinlist:
104				p = self._editor.pinyinlist[-1].get_screen_pinyin()
105				self.extra_string = [p] + self.extra_string
106				del self._editor.pinyinlist[-1]
107				self._editor.update ()
108			return True
109		elif (self._editor.pinyinlist or self.extra_string ) and (key.code == KeyCode.KEY_Right or key.code == KeyCode.KEY_b and key.mask & KeyMask.ControlMask):
110			if self.extra_string:
111				self._editor.pinyinlist.append(PinYinWord(pinyin=self.extra_string[0]))
112				del self.extra_string[0]
113				self._editor.update ()
114				return True
115			else:
116				raise InputException()
117		elif (self._editor.pinyinlist or self.extra_string ) and key.code == KeyCode.KEY_Delete:
118			if self.extra_string:
119				del self.extra_string[0]
120				return True
121			else:
122				raise InputException()
123		elif self.extra_string and key.code in (KeyCode.KEY_KP_Space, KeyCode.KEY_space):
124			while self.extra_string:
125				self._editor.pinyinlist.append(PinYinWord(pinyin=self.extra_string[0]))
126				del self.extra_string[0]
127			self._editor.auto_convert ()
128			return True
129		elif Engine.chinese_process_key_event (self,key):
130			return True;
131		return False
132
133class ShuangPinFactory (IMEngineFactory):
134	def __init__ (self, config):
135		IMEngineFactory.__init__ (self, config)
136		self.name 		= _(u"ShuangPin")
137		self.uuid 		= "5d2ceb47-7567-4d74-980f-26d5d300ea66"
138		self.authors	= u"Yu Fan <yufanyufan@gmail.com>"
139		self.icon_file 	= "/usr/share/scim/icons/scim-python.png"
140		self.credits 	= u"GPL"
141		self.help		= _(u"Help For ShuangPin")
142		self.set_languages ("zh")
143		self._config	= config
144	def create_instance (self, encoding, id):
145		engine =  ShuangPinEngine (self, self._config, encoding, id)
146		return engine
147
148	def reload_config (self, config):
149		pass
150
151
152def test_add_char(editor, char):
153	if(editor.current () == None or editor.current ().is_complete ()):
154		editor.pinyinlist.append(PinYinWord(MSPY_SHUANGPIN_SHENGMU_DICT[char],""))
155	else:
156		yunmu = MSPY_SHUANGPIN_YUNMU_DICT[char]
157		p = None
158		for i in yunmu:
159			pinyin = editor.current ().get_shengmu() + i
160			if pinyin in PINYIN_LIST:
161				p = i
162				break
163		if p == None:
164			raise Exception ()
165		editor.current ().set_yunmu (p)
166	editor.auto_convert ()
167
168def test_case(pinyin,modify = None, sentence=None):
169	editor = Editor ()
170	editor.schema = "MSPY"
171	editor.userword = True
172	editor.userphrase = True
173	editor.adjustfreq = True
174	
175	(editor.shengmu_schema, editor.yunmu_schema) = SHUANGPIN_SCHEMAS [editor.schema]
176	for i in pinyin:
177		test_add_char (editor,i)
178	editor.convert_all ()
179	if modify:
180		for i in range (0,len(modify)):
181			editor.wordlist[i].char=modify[i]
182	result = editor.commit ()
183	print result
184	if sentence:
185		assert editor.commit () == sentence
186
187def test():
188	test_case("fhdiijklfauhdedstmjqykllle")
189	test_case("woxihr")
190	test_case("hvbuhvybufme")
191	test_case("zfmehvuioa")
192	test_case("kklydkde")
193	test_case("devild")
194	test_case("veybvuyu")
195
196if __name__ == "__main__":
197	#~ import timeit
198	#~ t = timeit.Timer("ShuangPin.test()","import ShuangPin")
199	#~ print t.repeat(1,1)
200	import cProfile
201	cProfile.run("test()","fooprof")
202	import pstats
203	p = pstats.Stats("fooprof")
204	p.strip_dirs().sort_stats("cumulative").print_stats()