Skip to content

Commit 437c974

Browse files
committed
misc refine.
1. simplify api 2. update license and readme
1 parent 02bab54 commit 437c974

File tree

3 files changed

+40
-31
lines changed

3 files changed

+40
-31
lines changed

LICENSE

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
Copyright (c) 2012, Lx Yu
1+
Copyright (c) 2013, Lx Yu
22

3-
Permission to use, copy, modify, and/or distribute this software for any
4-
purpose with or without fee is hereby granted, provided that the above
5-
copyright notice and this permission notice appear in all copies.
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of
4+
this software and associated documentation files (the "Software"), to deal in
5+
the Software without restriction, including without limitation the rights to
6+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7+
the Software, and to permit persons to whom the Software is furnished to do so,
8+
subject to the following conditions:
69

7-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8-
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9-
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10-
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11-
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12-
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13-
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.rst

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
Pinyin Translator
2-
=================
1+
Pinyin
2+
======
33

44
Translate chinese chars to pinyin based on Mandarin.dat
55

6+
Install
7+
-------
8+
9+
.. code:: bash
10+
11+
$ pip install pinyin
12+
613
Usage
714
-----
815

9-
Input unicode string
10-
11-
::
16+
.. code:: python
1217
1318
>>> import pinyin
14-
>>> pinyin.get_pinyin(u'你好')
19+
>>> pinyin.get(u'你好')
1520
'nihao'
1621
>>> pinyin.get_initial(u'你好')
1722
'n h'

pinyin/pinyin.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python
21
# -*- coding: utf-8 -*-
32

43
__all__ = ['get_pinyin', 'get_initial']
@@ -14,7 +13,7 @@
1413
pinyin_dict[k] = v.lower().split(" ")[0][:-1]
1514

1615

17-
def pinyin_generator(chars):
16+
def _pinyin_generator(chars):
1817
"""
1918
Generate pinyin for chars, if char is not chinese character,
2019
itself will be returned.
@@ -25,21 +24,21 @@ def pinyin_generator(chars):
2524
yield pinyin_dict.get(key, char)
2625

2726

28-
def get_pinyin(s):
29-
"""
30-
Return pinyin of string, the input string must be unicode
27+
def get(s):
28+
"""Return pinyin of string, the string must be unicode
3129
"""
32-
assert(type(s) is unicode)
30+
assert(isinstance(s, unicode))
31+
return ''.join(_pinyin_generator(s))
3332

34-
generator = pinyin_generator(s)
35-
return ''.join(generator)
33+
34+
def get_pinyin(s):
35+
"""This function is only for backward compatibility, use `get` instead.
36+
"""
37+
return get(s)
3638

3739

3840
def get_initial(s):
41+
"""Return the 1st char of pinyin of string, the string must be unicode
3942
"""
40-
Return the 1st char of pinyin of string, the input string must be unicode
41-
"""
42-
assert(type(s) is unicode)
43-
44-
generator = pinyin_generator(s)
45-
return ' '.join([p[0] for p in generator])
43+
assert(isinstance(s, unicode))
44+
return ' '.join([p[0] for p in _pinyin_generator(s)])

0 commit comments

Comments
 (0)