cypunct是一个cython包,用于基于给定的unicode代码点frozenset拆分unicode字符串。
cypunct的Python项目详细描述
cypunct旨在解决快速拆分unicode的问题 基于一组字符的字符串。
cypunct设计用于python 2.6、2.7和3.3+。因为 cypunct是cython扩展,它(可能)只在cpython中工作 运行时。
对于python 2.6和2.7版本,cypunct只在以下情况下运行 cpython运行时使用标志编译 --enable-unicode=ucs4。cypunct将引发异常 如果python 2运行时不是用ucs-4编译的。
安装
pip最容易安装。只需运行
pip install cypunct
用法
cypunct接受一个unicode字符串和一个frozenset分隔符, 并基于该集合拆分字符串。每个分隔符 应该是单个Unicode代码点-^{TT3}$应该是1。
下面是一个简单的示例,其中我们提供了一个小的frozenset。
>>>fromcypunctimportsplit>>>split("James Mishra is the... best human ever, or so I think.",frozenset({' ','.',','}))['James','Mishra','is','the','best','human','ever','or','so','I','think','']
但是,如果只需要拆分空白字符,str.split()much 更好的表现。如果只需要拆分一个字符,str.split(char) 也会更快。
当你需要拆分许多可能的字符时,cypunct真的会发光, 例如整个Unicode character category。
下面的示例将拆分所有Unicode标点,而不拆分其他标点。
>>>fromcypunct.unicode_classesimportP>>>split("James Mishra is the... best human ever, or so I think.",P)['James Mishra is the',' best human ever',' or so I think','']
以下Unicode类作为集合提供:
Category | Description |
---|---|
C | Other |
Cc | Other, Format |
Cf | Other, Not Assigned |
Co | Other, Private Use |
Cs | Other, Surrogate |
L | Letter |
Ll | Letter, Lowercase |
Lm | Letter, Modifier |
Lo | Letter, Other |
Lt | Letter, Titlecase |
Lu | Letter, Uppercase |
M | Mark |
Mc | Mark, Space Combining |
Me | Mark, Enclosing |
Mn | Mark, Nonspacing |
N | Number |
Nd | Number, Decimal Digit |
Nl | Number, Letter |
No | Number, Other |
P | Punctuation |
Pc | Punctuation, Connector |
Pd | Punctuation, Dash |
Pe | Punctuation, Close |
Pf | Punctuation, Final Quote |
Pi | Punctuation, Initial Quote |
Po | Punctuation, Other |
Ps | Punctuation, Open |
S | Symbol |
Sc | Symbol, Currency |
Sk | Symbol, Modifier |
Sm | Symbol, Math |
So | Symbol, Other |
Z | Separator |
Zl | Separator, Line |
Zp | Separator, Paragraph |
Zs | Separator, Space |
cypunct.unicode_classes.COMMON_SEPARATORS是C、P、S和^{tt11}的并集。$ frozensets。我发现当把文本分成自然的 语言处理应用程序。
如果不为cypunct指定要使用的frozenset,则cypunct将 默认为COMMON_SEPARATORS。
更新Unicode数据
目前,cypunct.unicode_classes是从 UnicodeData.txt文件。自动生成脚本存在于 make_punctuation_file.py。
大多数cypunct用户不需要关心这个问题,但这很重要 了解您是否遇到Unicode错误或是否希望参与cypunct。
当前的UnicodeData.txt来自ftp://ftp.unicode.org/Public/10.0.0/ucd/UnicodeData.txt。
常见问题解答(FAQ)
q:我遇到一个安装错误,涉及 “pkg_resources.versionConflict(设置工具xx.xx.xx)”。 我该怎么解决?
你的setuptools版本很旧,我们无法 用它编译我们的cython扩展。跑步 pip install --upgrade setuptools然后再次尝试安装cypunct。
Q:如果用纯C编写,速度会不会更快?
是的,会的。我懒得手工编写c-cpython扩展,但它在我的todo列表中。 现在,cypunct是“足够快”,我可以在我的 日常生活。
但是,如果你想接受用c重写cypunct的挑战 与当前cython版本完全相同的功能,我将给您100美元。