diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..c58420d --- /dev/null +++ b/MANIFEST @@ -0,0 +1,4 @@ +# file GENERATED by distutils, do NOT edit +setup.py +pinyin/pinyin.py +pinyin/data/word.data diff --git a/pinyin/__init__.py b/pinyin/__init__.py new file mode 100644 index 0000000..235ca67 --- /dev/null +++ b/pinyin/__init__.py @@ -0,0 +1 @@ +from .pinyin import PinYin diff --git a/word.data b/pinyin/data/word.data similarity index 100% rename from word.data rename to pinyin/data/word.data diff --git a/pinyin.py b/pinyin/pinyin.py similarity index 64% rename from pinyin.py rename to pinyin/pinyin.py index 938b727..7941475 100644 --- a/pinyin.py +++ b/pinyin/pinyin.py @@ -4,46 +4,35 @@ """ Author:cleverdeng E-mail:clverdeng@gmail.com + Modified by anakin.yan@gmail.com """ -__version__ = '0.9' __all__ = ["PinYin"] -import os.path +import pkgutil class PinYin(object): - def __init__(self, dict_file='word.data'): + def __init__(self): + __package__ = 'pinyin' self.word_dict = {} - self.dict_file = dict_file - + self.data = pkgutil.get_data(__package__, 'data/word.data') def load_word(self): - if not os.path.exists(self.dict_file): - raise IOError("NotFoundFile") - - with file(self.dict_file) as f_obj: - for f_line in f_obj.readlines(): - try: - line = f_line.split(' ') - self.word_dict[line[0]] = line[1] - except: - line = f_line.split(' ') - self.word_dict[line[0]] = line[1] - + for line in self.data.split('\n'): + self.word_dict[line[:5].strip()] = line[8:] def hanzi2pinyin(self, string=""): result = [] if not isinstance(string, unicode): string = string.decode("utf-8") - + for char in string: key = '%X' % ord(char) result.append(self.word_dict.get(key, char).split()[0][:-1].lower()) return result - def hanzi2pinyin_split(self, string="", split=""): result = self.hanzi2pinyin(string=string) if split == "": diff --git a/setup.py b/setup.py index 40d5de1..5a17da3 100644 --- a/setup.py +++ b/setup.py @@ -4,28 +4,28 @@ """ Author:cleverdeng E-mail:clverdeng@gmail.com + Modified by anakin.yan@gmail.com """ from distutils.core import setup -from pinyin import __version__ as version setup( - name='pinyin', - version=version, - description='hanzi -> pinyin,With Python', - author='cleverdeng', - author_email='cleverdeng@gmail.com', - url='http://github.com/cleverdeng/pinyin.py', - py_modules=['pinyin'], - license='MIT License', - platforms=['any'], - classifiers=[ - 'Development Status :: 4 - Beta', - 'Intended Audience :: Developers', - 'License :: OSI Approved :: MIT License', - 'Programming Language :: Python', - 'Topic :: Software Development', - 'Topic :: Software Development :: Libraries', - 'Topic :: Software Development :: Libraries :: Python Modules' - ] - - ) + name='pinyin', + version='0.9.1', + description='hanzi -> pinyin,With Python', + author='cleverdeng', + author_email='cleverdeng@gmail.com', + url='http://github.com/cleverdeng/pinyin.py', + packages=['pinyin'], + package_data={'pinyin': ['data/word.data']}, + license='MIT License', + platforms=['any'], + classifiers=[ + 'Development Status :: 4 - Beta', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: MIT License', + 'Programming Language :: Python', + 'Topic :: Software Development', + 'Topic :: Software Development :: Libraries', + 'Topic :: Software Development :: Libraries :: Python Modules' + ] +)