/python/engine/XingMa/XMDict.py

http://scim-python.googlecode.com/ · Python · 101 lines · 63 code · 11 blank · 27 comment · 8 complexity · 1e708d5d2c04370b902aefb558e31e89 MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. # vim: set noet ts=4:
  3. #
  4. # scim-python
  5. #
  6. # Copyright (c) 2008-2008 Yu Yuwei <acevery@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. import sys
  27. reload (sys)
  28. sys.setdefaultencoding('utf-8')
  29. XingMa_Dict = {
  30. '0':0,
  31. 'a':1, 'b':2, 'c':3, 'd':4, 'e':5,
  32. 'f':6, 'g':7, 'h':8, 'i':9, 'j':10,
  33. 'k':11, 'l':12, 'm':13, 'n':14, 'o':15,
  34. 'p':16, 'q':17, 'r':18, 's':19, 't':20,
  35. 'u':21, 'v':22, 'w':23, 'x':24, 'y':25,
  36. 'z':26, "'":27, ';':28, '`':29, '~':30,
  37. '!':31, '@':32, '#':33, '$':34, '%':35,
  38. '^':36, '&':37, '*':38, '(':39, ')':40,
  39. '-':41, '_':42, '=':43, '+':44, '[':45,
  40. ']':46, '{':47, '}':48, '|':49, '/':50,
  41. ':':51, '"':52, '<':53, '>':54, ',':55,
  42. '.':56, '?':57, '\\':58, 'A':59, 'B':60,
  43. 'C':61, 'D':62, 'E':63, 'F':64, 'G':65,
  44. 'H':66, 'I':67, 'J':68, 'K':69, 'L':70,
  45. 'M':71, 'N':72, 'O':73, 'P':74, 'Q':75,
  46. 'R':76, 'S':77, 'T':78, 'U':79, 'V':80,
  47. 'W':81, 'X':82, 'Y':83, 'Z':84, '0':85,
  48. '1':86, '2':87, '3':88, '4':89, '5':90,
  49. '6':91, '7':92, '8':93, '9':94
  50. }
  51. XingMa_List = XingMa_Dict.keys()
  52. ID_XingMa_Dict = {}
  53. for key,id in XingMa_Dict.items():
  54. ID_XingMa_Dict[id] = key
  55. class XingMa_key:
  56. '''The class store'''
  57. def __init__(self, xm_key):
  58. try:
  59. if xm_key not in XingMa_List:
  60. error_m = u'%s is not in XingMa_Dict' % xm_key
  61. raise Exception ( error_m.encode('utf8') )
  62. except Exception, e:
  63. print e
  64. import traceback
  65. traceback.print_exc ()
  66. self._key = xm_key
  67. self._key_id = XingMa_Dict[xm_key]
  68. def get_key_id(self):
  69. return self._key_id
  70. def get_key(self):
  71. return self._key
  72. def __str__(self):
  73. return self._key
  74. def __int__(self):
  75. return self._key_id
  76. def Parse ( inputstr ):
  77. ids_input = []
  78. try:
  79. ids_input = map (XingMa_key,inputstr)
  80. except:
  81. pass
  82. return ids_input[:]
  83. def Deparse (id):
  84. '''deparse the id code of XingMa, id could be int or int in string form'''
  85. if id:
  86. id = int(id)
  87. if id in ID_XingMa_Dict:
  88. return ID_XingMa_Dict[id]
  89. else:
  90. return ''