PageRenderTime 44ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/python/engine/PinYin/SpecialPhrase.py

http://scim-python.googlecode.com/
Python | 76 lines | 46 code | 5 blank | 25 comment | 10 complexity | 5f9f7fe44bdcca0f8f5766f4bb9954be MD5 | raw file
  1. # vim: set noet ts=4:
  2. # -*- coding: utf-8 -*-
  3. #
  4. # scim-python
  5. #
  6. # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@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 os.path
  27. import time
  28. class SpecialPhrase:
  29. _dict = None
  30. def __init__ (self):
  31. if SpecialPhrase._dict == None:
  32. self._load_table ()
  33. SpecialPhrase._dict = self._dict
  34. else:
  35. self._dict = SpecialPhrase._dict
  36. def _load_table (self):
  37. self._dict = {}
  38. name = os.path.join (os.path.dirname (__file__), "special_phrase")
  39. for l in file (name):
  40. l = l.strip ()
  41. if l == "" or l[0] == "#":
  42. continue
  43. py, phrase = l.split ("\t")
  44. phrase = unicode (phrase, "utf8")
  45. if py not in self._dict:
  46. self._dict[py] = []
  47. self._dict[py].append (phrase)
  48. def lookup (self, py):
  49. result = []
  50. now = time.localtime ()
  51. weeks1 = (u"一", u"二", u"三", u"四", u"五", u"六", u"日")
  52. weeks2 = (u"一", u"二", u"三", u"四", u"五", u"六", u"天")
  53. values = {
  54. "year" : now[0],
  55. "month" : now[1],
  56. "day" : now[2],
  57. "hour_24" : now[3],
  58. "minute" : now[4],
  59. "second" : now[5],
  60. "week1" : weeks1[now[6]],
  61. "week2" : weeks2[now[6]],
  62. }
  63. if py in self._dict:
  64. for phrase in self._dict[py]:
  65. if phrase[0] == "#":
  66. phrase = phrase[1:] % values
  67. if phrase not in result:
  68. result.append (phrase)
  69. return result
  70. if __name__ == "__main__":
  71. SpecialPhrase ()._load_table ()