PageRenderTime 37ms CodeModel.GetById 23ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/python/engine/PinYin/SpecialPhrase.py

http://scim-python.googlecode.com/
Python | 76 lines | 46 code | 5 blank | 25 comment | 16 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#
26import os.path
27import time
28
29class SpecialPhrase:
30	_dict = None
31	def __init__ (self):
32		if SpecialPhrase._dict == None:
33			self._load_table ()
34			SpecialPhrase._dict = self._dict
35		else:
36			self._dict = SpecialPhrase._dict	
37
38	def _load_table (self):
39		self._dict = {}
40		name = os.path.join (os.path.dirname (__file__), "special_phrase")
41		for l in file (name):
42			l = l.strip ()
43			if l == "" or l[0] == "#":
44				continue
45			py, phrase = l.split ("\t")
46			phrase = unicode (phrase, "utf8")
47			if py not in self._dict:
48				self._dict[py] = []
49			self._dict[py].append (phrase)
50
51	def lookup (self, py):
52		result = []
53		now = time.localtime ()
54		weeks1 = (u"", u"", u"", u"", u"", u"", u"")
55		weeks2 = (u"", u"", u"", u"", u"", u"", u"")
56		values = {
57			"year" : now[0],
58			"month" : now[1],
59			"day" : now[2],
60			"hour_24" : now[3],
61			"minute" : now[4],
62			"second" : now[5],
63			"week1" : weeks1[now[6]],
64			"week2" : weeks2[now[6]],
65		}
66		if py in self._dict:
67			for phrase in self._dict[py]:
68				if phrase[0] == "#":
69					phrase = phrase[1:] % values
70				if phrase not in result:
71					result.append (phrase)
72		return result
73
74if __name__ == "__main__":
75	SpecialPhrase ()._load_table ()
76