Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

simplify code.

  • Loading branch information...
commit b588c863733256fb5a9fa08497afdb44b62137e6 1 parent 12b3e26
@lxyu authored
Showing with 16 additions and 16 deletions.
  1. +3 −2 pinyin/__init__.py
  2. +13 −14 pinyin/pinyin.py
View
5 pinyin/__init__.py
@@ -1,2 +1,3 @@
-from .pinyin import get_pinyin
-from .pinyin import get_pinyin_first_char
+# -*- coding: utf-8 -*-
+
+from .pinyin import *
View
27 pinyin/pinyin.py
@@ -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]
@@ -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])
Please sign in to comment.
Something went wrong with that request. Please try again.