File: ShuangPin.py

package info (click to toggle)
scim-python 0.1.13~rc1-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,436 kB
  • ctags: 2,794
  • sloc: sh: 9,774; python: 9,551; cpp: 3,420; makefile: 349; sed: 16
file content (204 lines) | stat: -rw-r--r-- 6,466 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# -*- coding: utf-8 -*-
# vim: set noet ts=4:
#
# scim-python
#
# Copyright (c) 2007-2008 Yu Fan <yufanyufan@gmail.com>
#
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA  02111-1307  USA
#
# $Id: $
#
from ZhengJu import *
import scim
import os
from scim import KeyCode
from scim import KeyMask
from scim import Property
import traceback
import sys
from PYDict import *
from gettext import dgettext

_ = lambda a : dgettext ("scim-python", a)
class ShuangPinEngine (Engine):

	def __init__ (self, factory, config, encoding, id):
		Engine.__init__(self, factory, config, encoding, id)
		
	def clear(self):
		self.extra_string = []
		Engine.clear(self)
	def get_extra_string(self):
		return "".join(self.extra_string)
	def reload_config(self,config):
		self.reset()
		self.schema = config.read ("/IMEngine/Python/ZhengJu/ShuangPinSchema", "MSPY")
		#~ print self.schema
		if not self.schema in SHUANGPIN_SCHEMAS.keys ():
			self.schima = "MSPY"
		(self.shengmu_schema, self.yunmu_schema) = SHUANGPIN_SCHEMAS[self.schema]
		Engine.reload_config(self,config)
		
	def translate(self,key):
		if(self._editor.current () == None or self._editor.current ().is_complete ()):
			if unichr (key.code) in self.shengmu_schema:
				self._editor.pinyinlist.append (PinYinWord(self.shengmu_schema[unichr (key.code)],""))
				if not self.progresivepromp:
					return
			else:
				raise InputException()
		else:
			if unichr(key.code) in self.yunmu_schema:
				yunmu = self.yunmu_schema[unichr(key.code)]
				p = None
				for i in yunmu:
					pinyin = self._editor.current ().get_shengmu () + i
					if pinyin in PINYIN_LIST:
						p = i
						break
				if p == None:
					raise InputException ()
				self._editor.current().set_yunmu (p)
			else:
				raise InputException
		self._editor.auto_convert ()
		


	def chinese_process_key_event (self, key):
		if (self.schema=="MSPY" or self.schema=="ZGPY") and key.mask == KeyMask.NullMask and (\
			(key.code >= KeyCode.KEY_a and key.code <= KeyCode.KEY_z) or \
			key.code == KeyCode.KEY_semicolon):
			self.translate(key)
			return True
		elif key.mask == KeyMask.NullMask \
			and (key.code >= KeyCode.KEY_a and key.code <= KeyCode.KEY_z):
			self.translate(key)
			return True
		elif self._editor.pinyinlist and key.code == KeyCode.KEY_BackSpace:
			if self._editor.pinyinlist[-1].is_complete ():
				self._editor.pinyinlist[-1].set_yunmu ("")
				if not self.progresivepromp:
					return True
			else:
				del self._editor.pinyinlist[-1]
			self._editor.update ()
			return True
		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):
			if self._editor.pinyinlist:
				p = self._editor.pinyinlist[-1].get_screen_pinyin()
				self.extra_string = [p] + self.extra_string
				del self._editor.pinyinlist[-1]
				self._editor.update ()
			return True
		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):
			if self.extra_string:
				self._editor.pinyinlist.append(PinYinWord(pinyin=self.extra_string[0]))
				del self.extra_string[0]
				self._editor.update ()
				return True
			else:
				raise InputException()
		elif (self._editor.pinyinlist or self.extra_string ) and key.code == KeyCode.KEY_Delete:
			if self.extra_string:
				del self.extra_string[0]
				return True
			else:
				raise InputException()
		elif self.extra_string and key.code in (KeyCode.KEY_KP_Space, KeyCode.KEY_space):
			while self.extra_string:
				self._editor.pinyinlist.append(PinYinWord(pinyin=self.extra_string[0]))
				del self.extra_string[0]
			self._editor.auto_convert ()
			return True
		elif Engine.chinese_process_key_event (self,key):
			return True;
		return False

class ShuangPinFactory (IMEngineFactory):
	def __init__ (self, config):
		IMEngineFactory.__init__ (self, config)
		self.name 		= _(u"ShuangPin")
		self.uuid 		= "5d2ceb47-7567-4d74-980f-26d5d300ea66"
		self.authors	= u"Yu Fan <yufanyufan@gmail.com>"
		self.icon_file 	= "/usr/share/scim/icons/scim-python.png"
		self.credits 	= u"GPL"
		self.help		= _(u"Help For ShuangPin")
		self.set_languages ("zh")
		self._config	= config
	def create_instance (self, encoding, id):
		engine =  ShuangPinEngine (self, self._config, encoding, id)
		return engine

	def reload_config (self, config):
		pass


def test_add_char(editor, char):
	if(editor.current () == None or editor.current ().is_complete ()):
		editor.pinyinlist.append(PinYinWord(MSPY_SHUANGPIN_SHENGMU_DICT[char],""))
	else:
		yunmu = MSPY_SHUANGPIN_YUNMU_DICT[char]
		p = None
		for i in yunmu:
			pinyin = editor.current ().get_shengmu() + i
			if pinyin in PINYIN_LIST:
				p = i
				break
		if p == None:
			raise Exception ()
		editor.current ().set_yunmu (p)
	editor.auto_convert ()

def test_case(pinyin,modify = None, sentence=None):
	editor = Editor ()
	editor.schema = "MSPY"
	editor.userword = True
	editor.userphrase = True
	editor.adjustfreq = True
	
	(editor.shengmu_schema, editor.yunmu_schema) = SHUANGPIN_SCHEMAS [editor.schema]
	for i in pinyin:
		test_add_char (editor,i)
	editor.convert_all ()
	if modify:
		for i in range (0,len(modify)):
			editor.wordlist[i].char=modify[i]
	result = editor.commit ()
	print result
	if sentence:
		assert editor.commit () == sentence

def test():
	test_case("fhdiijklfauhdedstmjqykllle")
	test_case("woxihr")
	test_case("hvbuhvybufme")
	test_case("zfmehvuioa")
	test_case("kklydkde")
	test_case("devild")
	test_case("veybvuyu")

if __name__ == "__main__":
	#~ import timeit
	#~ t = timeit.Timer("ShuangPin.test()","import ShuangPin")
	#~ print t.repeat(1,1)
	import cProfile
	cProfile.run("test()","fooprof")
	import pstats
	p = pstats.Stats("fooprof")
	p.strip_dirs().sort_stats("cumulative").print_stats()