跳转到主要内容

用于Python的hunspell包装器

项目描述

Build Status PyPI version shields.io PyPI pyversions License: MIT

CyHunspell

Hunspell词典的Cython包装器

描述

此存储库提供Hunspell包装器,可在Python中本地使用。该模块使用Cython在C++和Python代码之间建立链接,并具有一些附加功能。由于所有繁重的工作都在模块接口的C++侧完成,因此几乎没有Python开销,从而提供了最佳性能。

hunspell库将缓存任何更正,您可以通过向Hunspell构造函数添加`use_disk_cache`参数来使用持久缓存。否则,它使用内存缓存。

安装

对于最简单的安装,只需运行

pip install cyhunspell

这将为您所在的平台安装hunspell 1.7.0 C++绑定。

依赖项

cacheman -- 用于(可选异步)持久缓存

非Python依赖项

hunspell

库安装了hunspell版本1.7.0。随着hunspell新版本的可用,此库将提供匹配的新版本。

功能

拼写检查 & 拼写建议

如何使用

以下是一些使用存储库的简单示例。

创建Hunspell对象

from hunspell import Hunspell
h = Hunspell()

现在您有一个可用的hunspell对象,可以为您执行基本查询。

h.spell('test') # True

拼写

询问特定单词是否在字典中是一个简单任务。

h.spell('correct') # True
h.spell('incorect') # False

此函数只返回True或False,不会提供错误原因的建议。它还取决于你选择的词典。

建议

如果你想从Hunspell获取建议,它可以在给定的basestring输入后提供修正的标签。

h.suggest('incorect') # ('incorrect', 'correction', corrector', 'correct', 'injector')

建议按顺序排列,索引越低,越接近输入字符串。

后缀匹配

h.suffix_suggest('do') # ('doing', 'doth', 'doer', 'doings', 'doers', 'doest')

词干提取

该模块还可以提取词干,提供复数和其他变形的词干。

h.stem('testers') # ('tester', 'test')
h.stem('saves') # ('save',)

分析

类似于词干提取,但返回输入的形态分析。

h.analyze('permanently') # (' st:permanent fl:Y',)

生成

由于1.7.0构建版本对任何输入(包括文档中描述的输入)都没有产生任何结果,因此目前没有提供生成方法。如果这个问题得到解决或有人确定了调用模式中的问题,这个功能将在未来添加到库中。

批量请求

您还可以请求对Hunspell进行批量操作。这将触发一个线程请求(没有全局解释器锁)以执行请求的操作。目前,只有'suggest'和'stem'可以进行批量请求。

h.bulk_suggest(['correct', 'incorect'])
# {'incorect': ('incorrect', 'correction', 'corrector', 'correct', 'injector'), 'correct': ('correct',)}
h.bulk_suffix_suggest(['cat', 'do'])
# {'do': ('doing', 'doth', 'doer', 'doings', 'doers', 'doest'), 'cat': ('cater', 'cats', "cat's", 'caters')}
h.bulk_stem(['stems', 'currencies'])
# {'currencies': ('currency',), 'stems': ('stem',)}
h.bulk_analyze(['dog', 'permanently'])
# {'permanently': (' st:permanent fl:Y',), 'dog': (' st:dog',)}

默认情况下,它会启动与CPU数量相同的线程来执行操作。您也可以覆盖并发性。

h.set_concurrency(4) # Four threads will now be used for bulk requests

词典

您还可以指定希望使用的语言或词典。

h = Hunspell('en_CA') # Canadian English

默认情况下,您有以下词典可用

  • en_AU
  • en_CA
  • en_GB
  • en_NZ
  • en_US
  • en_ZA

但是,您可以下载自己的词典并将Hunspell指向您的自定义词典。

h = Hunspell('en_GB-large', hunspell_data_dir='/custom/dicts/dir')

添加词典

您还可以通过调用add_dic方法在运行时添加新的词典。

h.add_dic(os.path.join(PATH_TO, 'special.dic'))

添加单词

您可以在运行时向词典中添加单个单词。

h.add('sillly')

此外,您可以通过提供第二个参数将词缀附加到单词上

h.add('silllies', "is:plural")

删除单词

与添加类似,您也可以删除单词。

h.remove(word)

异步缓存

如果您希望Hunspell缓存建议和词干,您可以传递一个目录来存放此类缓存。

h = Hunspell(disk_cache_dir='/tmp/hunspell/cache/dir')

这将定期和在后台保存所有建议和词干请求。在特定时间范围内的新请求超过一定数量后,缓存将分叉并保存缓存内容,而程序的其他部分继续运行。您无需显式地将缓存保存到磁盘,但如果需要,可以这样做。

h.save_cache()

否则,Hunspell对象将仅在内存中本地缓存此类请求,而不持久化内存。

语言首选项

  • 谷歌风格指南
  • 面向对象(有一些例外)

已知解决方案

  • 在Windows上,非常长的文件路径或以不同于系统编码的方式保存的路径需要Hunspell特殊处理以加载词典文件。要绕过Windows配置中的此问题,可以在Hunspell构造函数中将system_encoding设置为'UTF-8',或将环境变量HUNSPELL_PATH_ENCODING设置为UTF-8。然后,您必须通过传递该参数名称到Hunspell构造函数或设置环境变量HUNSPELL_DATA来以UTF-8重新编码hunspell_data_dir。这是Hunspell / Windows操作的限制。

作者

作者:Tim Rodriguez和Matthew Seal

许可证

MIT

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪一个,请了解有关安装包的更多信息。

源代码分发

此版本没有可用的源代码分发文件。请参阅生成分发归档的教程。

构建分发

cyhunspell-2.0.2-cp39-cp39-win_amd64.whl (2.1 MB 查看哈希值)

上传时间: CPython 3.9 Windows x86-64

cyhunspell-2.0.2-cp39-cp39-win32.whl (2.1 MB 查看哈希值)

上传时间: CPython 3.9 Windows x86

cyhunspell-2.0.2-cp39-cp39-manylinux2010_x86_64.whl (3.8 MB 查看哈希值)

上传时间: CPython 3.9 manylinux: glibc 2.12+ x86-64

cyhunspell-2.0.2-cp39-cp39-manylinux2010_i686.whl (3.7 MB 查看哈希值)

上传时间: CPython 3.9 manylinux: glibc 2.12+ i686

cyhunspell-2.0.2-cp39-cp39-macosx_10_9_x86_64.whl (2.1 MB 查看哈希值)

上传时间: CPython 3.9 macOS 10.9+ x86-64

cyhunspell-2.0.2-cp38-cp38-win_amd64.whl (2.1 MB 查看哈希值)

上传时间: CPython 3.8 Windows x86-64

cyhunspell-2.0.2-cp38-cp38-win32.whl (2.1 MB 查看哈希值)

上传时间: CPython 3.8 Windows x86

cyhunspell-2.0.2-cp38-cp38-manylinux2010_x86_64.whl (3.8 MB 查看哈希值)

上传时间: CPython 3.8 manylinux: glibc 2.12+ x86-64

cyhunspell-2.0.2-cp38-cp38-manylinux2010_i686.whl (3.7 MB 查看哈希值)

上传时间: CPython 3.8 manylinux: glibc 2.12+ i686

cyhunspell-2.0.2-cp38-cp38-macosx_10_9_x86_64.whl (2.1 MB 查看哈希值)

上传于 CPython 3.8 macOS 10.9+ x86-64

cyhunspell-2.0.2-cp37-cp37m-win_amd64.whl (2.1 MB 查看哈希值)

上传于 CPython 3.7m Windows x86-64

cyhunspell-2.0.2-cp37-cp37m-win32.whl (2.1 MB 查看哈希值)

上传于 CPython 3.7m Windows x86

cyhunspell-2.0.2-cp37-cp37m-manylinux2010_x86_64.whl (3.8 MB 查看哈希值)

上传于 CPython 3.7m manylinux: glibc 2.12+ x86-64

cyhunspell-2.0.2-cp37-cp37m-manylinux2010_i686.whl (3.7 MB 查看哈希值)

上传于 CPython 3.7m manylinux: glibc 2.12+ i686

cyhunspell-2.0.2-cp37-cp37m-macosx_10_9_x86_64.whl (2.1 MB 查看哈希值)

上传于 CPython 3.7m macOS 10.9+ x86-64

cyhunspell-2.0.2-cp36-cp36m-win_amd64.whl (2.1 MB 查看哈希值)

上传于 CPython 3.6m Windows x86-64

cyhunspell-2.0.2-cp36-cp36m-win32.whl (2.1 MB 查看哈希值)

上传于 CPython 3.6m Windows x86

cyhunspell-2.0.2-cp36-cp36m-manylinux2010_x86_64.whl (3.8 MB 查看哈希值)

上传于 CPython 3.6m manylinux: glibc 2.12+ x86-64

cyhunspell-2.0.2-cp36-cp36m-manylinux2010_i686.whl (3.7 MB 查看哈希值)

上传于 CPython 3.6m manylinux: glibc 2.12+ i686

cyhunspell-2.0.2-cp36-cp36m-macosx_10_9_x86_64.whl (2.1 MB 查看哈希值)

上传于 CPython 3.6m macOS 10.9+ x86-64

支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面