PageRenderTime 17ms CodeModel.GetById 1ms 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. #
  26. from ZhengJu import *
  27. import scim
  28. import os
  29. from scim import KeyCode
  30. from scim import KeyMask
  31. from scim import Property
  32. import traceback
  33. import sys
  34. from PYDict import *
  35. from gettext import dgettext
  36. _ = lambda a : dgettext ("scim-python", a)
  37. class ShuangPinEngine (Engine):
  38. def __init__ (self, factory, config, encoding, id):
  39. Engine.__init__(self, factory, config, encoding, id)
  40. def clear(self):
  41. self.extra_string = []
  42. Engine.clear(self)
  43. def get_extra_string(self):
  44. return "".join(self.extra_string)
  45. def reload_config(self,config):
  46. self.reset()
  47. self.schema = config.read ("/IMEngine/Python/ZhengJu/ShuangPinSchema", "MSPY")
  48. #~ print self.schema
  49. if not self.schema in SHUANGPIN_SCHEMAS.keys ():
  50. self.schima = "MSPY"
  51. (self.shengmu_schema, self.yunmu_schema) = SHUANGPIN_SCHEMAS[self.schema]
  52. Engine.reload_config(self,config)
  53. def translate(self,key):
  54. if(self._editor.current () == None or self._editor.current ().is_complete ()):
  55. if unichr (key.code) in self.shengmu_schema:
  56. self._editor.pinyinlist.append (PinYinWord(self.shengmu_schema[unichr (key.code)],""))
  57. if not self.progresivepromp:
  58. return
  59. else:
  60. raise InputException()
  61. else:
  62. if unichr(key.code) in self.yunmu_schema:
  63. yunmu = self.yunmu_schema[unichr(key.code)]
  64. p = None
  65. for i in yunmu:
  66. pinyin = self._editor.current ().get_shengmu () + i
  67. if pinyin in PINYIN_LIST:
  68. p = i
  69. break
  70. if p == None:
  71. raise InputException ()
  72. self._editor.current().set_yunmu (p)
  73. else:
  74. raise InputException
  75. self._editor.auto_convert ()
  76. def chinese_process_key_event (self, key):
  77. if (self.schema=="MSPY" or self.schema=="ZGPY") and key.mask == KeyMask.NullMask and (\
  78. (key.code >= KeyCode.KEY_a and key.code <= KeyCode.KEY_z) or \
  79. key.code == KeyCode.KEY_semicolon):
  80. self.translate(key)
  81. return True
  82. elif key.mask == KeyMask.NullMask \
  83. and (key.code >= KeyCode.KEY_a and key.code <= KeyCode.KEY_z):
  84. self.translate(key)
  85. return True
  86. elif self._editor.pinyinlist and key.code == KeyCode.KEY_BackSpace:
  87. if self._editor.pinyinlist[-1].is_complete ():
  88. self._editor.pinyinlist[-1].set_yunmu ("")
  89. if not self.progresivepromp:
  90. return True
  91. else:
  92. del self._editor.pinyinlist[-1]
  93. self._editor.update ()
  94. return True
  95. 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):
  96. if self._editor.pinyinlist:
  97. p = self._editor.pinyinlist[-1].get_screen_pinyin()
  98. self.extra_string = [p] + self.extra_string
  99. del self._editor.pinyinlist[-1]
  100. self._editor.update ()
  101. return True
  102. 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):
  103. if self.extra_string:
  104. self._editor.pinyinlist.append(PinYinWord(pinyin=self.extra_string[0]))
  105. del self.extra_string[0]
  106. self._editor.update ()
  107. return True
  108. else:
  109. raise InputException()
  110. elif (self._editor.pinyinlist or self.extra_string ) and key.code == KeyCode.KEY_Delete:
  111. if self.extra_string:
  112. del self.extra_string[0]
  113. return True
  114. else:
  115. raise InputException()
  116. elif self.extra_string and key.code in (KeyCode.KEY_KP_Space, KeyCode.KEY_space):
  117. while self.extra_string:
  118. self._editor.pinyinlist.append(PinYinWord(pinyin=self.extra_string[0]))
  119. del self.extra_string[0]
  120. self._editor.auto_convert ()
  121. return True
  122. elif Engine.chinese_process_key_event (self,key):
  123. return True;
  124. return False
  125. class ShuangPinFactory (IMEngineFactory):
  126. def __init__ (self, config):
  127. IMEngineFactory.__init__ (self, config)
  128. self.name = _(u"ShuangPin")
  129. self.uuid = "5d2ceb47-7567-4d74-980f-26d5d300ea66"
  130. self.authors = u"Yu Fan <yufanyufan@gmail.com>"
  131. self.icon_file = "/usr/share/scim/icons/scim-python.png"
  132. self.credits = u"GPL"
  133. self.help = _(u"Help For ShuangPin")
  134. self.set_languages ("zh")
  135. self._config = config
  136. def create_instance (self, encoding, id):
  137. engine = ShuangPinEngine (self, self._config, encoding, id)
  138. return engine
  139. def reload_config (self, config):
  140. pass
  141. def test_add_char(editor, char):
  142. if(editor.current () == None or editor.current ().is_complete ()):
  143. editor.pinyinlist.append(PinYinWord(MSPY_SHUANGPIN_SHENGMU_DICT[char],""))
  144. else:
  145. yunmu = MSPY_SHUANGPIN_YUNMU_DICT[char]
  146. p = None
  147. for i in yunmu:
  148. pinyin = editor.current ().get_shengmu() + i
  149. if pinyin in PINYIN_LIST:
  150. p = i
  151. break
  152. if p == None:
  153. raise Exception ()
  154. editor.current ().set_yunmu (p)
  155. editor.auto_convert ()
  156. def test_case(pinyin,modify = None, sentence=None):
  157. editor = Editor ()
  158. editor.schema = "MSPY"
  159. editor.userword = True
  160. editor.userphrase = True
  161. editor.adjustfreq = True
  162. (editor.shengmu_schema, editor.yunmu_schema) = SHUANGPIN_SCHEMAS [editor.schema]
  163. for i in pinyin:
  164. test_add_char (editor,i)
  165. editor.convert_all ()
  166. if modify:
  167. for i in range (0,len(modify)):
  168. editor.wordlist[i].char=modify[i]
  169. result = editor.commit ()
  170. print result
  171. if sentence:
  172. assert editor.commit () == sentence
  173. def test():
  174. test_case("fhdiijklfauhdedstmjqykllle")
  175. test_case("woxihr")
  176. test_case("hvbuhvybufme")
  177. test_case("zfmehvuioa")
  178. test_case("kklydkde")
  179. test_case("devild")
  180. test_case("veybvuyu")
  181. if __name__ == "__main__":
  182. #~ import timeit
  183. #~ t = timeit.Timer("ShuangPin.test()","import ShuangPin")
  184. #~ print t.repeat(1,1)
  185. import cProfile
  186. cProfile.run("test()","fooprof")
  187. import pstats
  188. p = pstats.Stats("fooprof")
  189. p.strip_dirs().sort_stats("cumulative").print_stats()