英语分词。

wordsegmentation的Python项目详细描述


分词是Apache2许可的英语单词模块 分词,用纯python编写,基于万亿字的语料库。

灵感来自格兰特·詹克斯https://pypi.python.org/pypi/wordsegment。 基于peter norvig“自然语言语料库数据”一章中的词权重算法 摘自《美丽的数据》(Segaran和Hammerbacher,2009)。

如前所述,数据文件来自google web万亿单词语料库 作者:Thorsten Brants和Alex Franz,由语言数据联盟发布。

功能

  • 纯Python
  • 采用分而治之的分割算法,使输入文本不存在最大长度限制。
  • 分段ALGOTROTHM采用动态规划实现多项式时间复杂度。
  • 使用google万亿语料库进行分词评分。
  • 在Python2.7上开发
  • 在CPython 2.6,2.7,3.4上测试。

快速启动

使用pip

安装wordsegment很简单
$ pip install wordsegmentation

需要依赖关系networkx

$ pip install networkx

教程

在您自己的python程序中,您通常希望使用segment将短语分成一个部分列表:

>>> from wordsegmentation import WordSegment
>>> ws = WordSegment()

>>> ws.segment('universityofwashington')
['university', 'of', 'washington']
>>> ws.segment('thisisatest')
['this', 'is', 'a', 'test']
>>> ws.segment('thisisanURLcontaining123345and-&**^&butitstillworks')
['this', 'is', 'an', 'url', 'containing', '123345', 'and', '-&**^&', 'but', 'it', 'still', 'works']
>>> ws.segment('NoMatterHowLongThisTextThisTextThisTextThisTextThisTextThisTextThisTextThisTextThisTextThisTextThisTextThisTextThisTextThisTextThisTextMightBe')
['no', 'matter', 'how', 'long', 'this', 'text', 'this', 'text', 'this', 'text', 'this', 'text', 'this', 'text', 'this', 'text', 'this', 'text', 'this', 'text', 'this', 'text', 'this', 'text', 'this', 'text', 'this', 'text', 'this', 'text', 'this', 'text', 'this', 'text', 'might', 'be']

错误报告

Weihan@Github

gmail.com上的weihan.github

技术细节

在代码中,分割算法由以下步骤组成,

  1. 分而治之–将输入字符串安全地分成子字符串。这样我们就解决了长度限制,这将大大降低性能。例如,“facebook123helloworld”将被视为3个子问题-“facebook”、“123”和“helloworld”。
  2. 对于每个子字符串。我用动态规划的方法计算并得到最优词。
  3. 合并子问题,并返回原始字符串的结果。
在该模块中使用的分割算法,实现了O(n ^ 2)的时间复杂度。 通过与现有分割算法的比较,该模块在以下方面优于:
  1. 可以处理很长的输入。没有为输入字符串设置任意最大长度限制。
  2. 通过动态规划在多项式时间内完成分割。
  3. 默认情况下,该算法使用过滤后的google语料库,其中只包含字典中可以找到的英语单词。
一个极端的例子是求解经典的English Scriptio_continua segmentation problem,如下所示:
>;>;你喜欢你的新生活。Esseditistheblightmanwasbornforitismargaretyoumownfor')

我们的算法在多项式时间内解决了这个问题,结果是:

[‘margaret', 'are', 'you', 'grieving', 'over', 'golden', 'grove', 'un', 'leaving', 'leaves', 'like', 'the', 'things', 'of', 'man', 'you', 'with', 'your', 'fresh', 'thoughts', 'care', 'for', 'can', 'you', 'a', 'has', 'the', 'he', 'art', 'grows', 'older', 'it', 'will', 'come', 'to', 'such', 'sights', 'colder', 'by', 'and', 'by', 'nor', 'spa', 're', 'a', 'sigh', 'though', 'worlds', 'of', 'wan', 'wood', 'leaf', 'me', 'allie', 'and', 'yet', 'you', 'will', 'weep', 'and', 'know', 'why', 'now', 'no', 'matter', 'child', 'the', 'name', 'sorrows', 'springs', 'are', 'the', 'same', 'nor', 'mouth', 'had', 'non', 'or', 'mind', 'expressed', 'what', 'he', 'art', 'heard', 'of', 'ghost', 'guessed', 'it', 'is', 'the', 'blight', 'man', 'was', 'born', 'for', 'it', 'is', 'margaret', 'you', 'mourn', 'for']

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Jetty中的java Set AccessControlAllowOrigin   java我已将我的apk上传到google play,但没有显示任何设备兼容   在内存中创建mdb副本时发生java错误   是否有类似jQuery的选择器用于JavaXML解析?   C++不能使用JNI使用外部java类   java如何创建jpql查询以获取没有关联父级的实体   maven如何重新部署java web app cargo   java如何使用Gson将嵌套的json对象添加到文件中?   用于inputfield的java Sencha GXT自定义货币格式   java在容器上设置游标,而不更改子组件的游标   java JDBI使用fluent接口选择(非对象查询)   安卓 Firebase runTransaction不工作MutableData为空   java如何修复快速排序代码   java正则表达式验证密码,密码只接受固定长度之间的值   javascript拆分值并以格式化方式显示   java Asynctask:doInBackground中Asynctask和process的两个值