python pyenchant 拼写检查模块使用教程
·
一、安装 pyenchant 模块
pip install pyenchant -i https://pypi.tuna.tsinghua.edu.cn/simple
二、关键方法
| 方法 | 功能描述 |
|---|---|
enchant.dict_exits(language) |
查看当前enchant模块是否支持某种语言 |
enchant.list_languages() |
查看当前enchant模块支持的所有语言 |
d = enchant.Dict(language) |
使用指定语言创建Dict对象 |
d.tag |
当前Dict使用的语言 |
d.check(word) |
检查word的拼写是否正确 |
d.suggest(word) |
对拼写错误的word提供几个正确拼写的单词 |
简单示例:
import enchant
# 创建英文拼写的 Dict 对象
d = enchant.Dict("en_US")
print(d.check("Hello"))
print(d.check("Helo"))
print(d.suggest("Helo"))
print(enchant.list_languages())
输出:
- True
- False
- [‘Help’, ‘Hel’, ‘Helot’, ‘Hero’, ‘He lo’, ‘He-lo’, ‘Hel o’, ‘Hole’, ‘Hello’, ‘Halo’, ‘Hell’, ‘Held’]
- [‘en_AG’, ‘en_AU’, ‘en_BS’, ‘en_BW’, ‘en_BZ’, ‘en_CA’, ‘en_DK’, ‘en_GB’, ‘en_GH’, ‘en_HK’, ‘en_IE’, ‘en_IN’, ‘en_JM’, ‘en_NA’, ‘en_NG’, ‘en_NZ’, ‘en_PH’, ‘en_SG’, ‘en_TT’, ‘en_US’, ‘en_ZA’, ‘en_ZW’]
三、检查一段文本的拼写
这里使用enchant.checker中的SpellChecker类来解决对一整段文本中的单词进行拼写检查
from enchant.checker import SpellChecker
chkr = SpellChecker("en_US")
chkr.set_text("This is sme sample txt with erors.")
for err in chkr:
print ("error: ", err.word)
输出:
- error: sme
- error: txt
- error: erors
四、英文分词器(Tokenization)
将英语文本进行分词,返回结果格式(word, pos),其中pos是word在整个文本中出现的位置
from enchant.tokenize import get_tokenizer
tknzr = get_tokenizer("en_US")
lis = [w for w in tknzr("this is some simple text.")]
print(lis)
输出:
- [(‘this’, 0), (‘is’, 5), (‘some’, 8), (‘simple’, 13), (‘text’, 20)]
参考
更多推荐



所有评论(0)