Skip to content

Commit

Permalink
simplify code.
Browse files Browse the repository at this point in the history
  • Loading branch information
lxyu committed Aug 29, 2012
1 parent 12b3e26 commit b588c86
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
5 changes: 3 additions & 2 deletions pinyin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .pinyin import get_pinyin
from .pinyin import get_pinyin_first_char
# -*- coding: utf-8 -*-

from .pinyin import *
27 changes: 13 additions & 14 deletions pinyin/pinyin.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#!/usr/bin/python
# -*- coding:utf-8 -*-
#!/usr/bin/env python
# -*- coding: utf-8 -*-

__all__ = ['get_pinyin', 'get_initial']

import os

# init pinyin dict
PINYIN_DATA = os.path.join(
os.path.dirname(os.path.abspath(__file__)), 'Mandarin.dat')
dat = os.path.join(os.path.dirname(__file__), "Mandarin.dat")
pinyin_dict = {}
with open(PINYIN_DATA) as f:
with open(dat) as f:
for line in f:
k, v = line.strip().split('\t')
pinyin_dict[k] = v.lower().split(" ")[0][:-1]
Expand All @@ -24,23 +25,21 @@ def pinyin_generator(chars):
yield pinyin_dict.get(key, char)


def get_pinyin(string):
def get_pinyin(s):
"""
Return pinyin of string, the input string must be unicode
"""
if type(string) is not unicode:
raise AttributeError('Input string is not unicode: %s' % string)
assert(type(s) is unicode)

generator = pinyin_generator(string)
generator = pinyin_generator(s)
return ''.join(generator)


def get_pinyin_first_char(string):
def get_initial(s):
"""
Return the 1st char of pinyin of string, the input string must be unicode
"""
if type(string) is not unicode:
raise AttributeError('Input string is not unicode: %s' % string)
assert(type(s) is unicode)

generator = pinyin_generator(string)
return ''.join([p[0] for p in generator])
generator = pinyin_generator(s)
return ' '.join([p[0] for p in generator])

0 comments on commit b588c86

Please sign in to comment.