简体   繁体   English

是否有任何用于 Python 的自动更正/自动完成库?

[英]Are there any auto-correct/auto-complete libraries for Python?

I'm making a chatbot and would like users to spell correctly to make everything on the back end easier.我正在制作一个聊天机器人,并希望用户正确拼写以使后端的一切变得更容易。 Are there any autocorrect and/or autocomplete libraries out there?是否有任何自动更正和/或自动完成库?

I'm guessing you are making an integration for something like Slack.我猜您正在为 Slack 之类的东西进行集成。 Using an autocorrect feature might be very dangerous as you could "correct" something from a neutral state to a destructive state.使用自动更正功能可能非常危险,因为您可以将某些内容从中立状态“更正”到破坏性状态。 Styling your inputs to be simple and but descriptive would be a lot safer.将您的输入样式设置为简单但具有描述性会更安全。 You could also implement a "did you mean" feature with some simple character counting that would let the user see that they messed up, and offer then the option to correctly input the right key phrase.您还可以通过一些简单的字符计数来实现“您的意思是”功能,让用户看到他们搞砸了,然后提供正确输入正确关键短语的选项。

Input:输入:

derete file1.jpg

check possible keyword position 0 with existing keyword set...add/remove/delete使用现有关键字集检查可能的关键字位置 0...添加/删除/删除

0/6 match for add 0/6 匹配添加

2/6 match for remove 2/6 匹配删除

5/6 match for known keyword 'delete', picking 'delete' as suggestion 5/6 匹配已知关键字“删除”,选择“删除”作为建议

Output:输出:

Did you mean delete file1.jpg ?你的意思是delete file1.jpg吗?

I think that would be safer and not too painful to code.我认为这会更安全,而且编写代码不会太痛苦。 Just have a function that iterates through each character and increments a counter if the character matches.只需有一个函数来遍历每个字符并在字符匹配时增加一个计数器。 It's FAR from perfect but would be a step in the right direction if you wanted to make it manually.它离完美还很远,但如果您想手动制作它,这将是朝着正确方向迈出的一步。

What gman said in the other answer is a good idea, sometimes trying to forcefully auto-correct might "correct" right words that fit in the context but are not in the auto-correct database and substitute them for right words that don't make sense in the context. gman 在另一个答案中所说的是一个好主意,有时尝试强制自动更正可能会“更正”适合上下文但不在自动更正数据库中的正确单词,并将它们替换为不正确的单词语境中的意义。

A python auto-complete lib: https://github.com/phatpiglet/autocorrect一个 python 自动完成库: https : //github.com/phatpiglet/autocorrect

See also: https://github.com/mattalcock/blog/blob/master/2012/12/5/python-spell-checker.rst另见: https : //github.com/mattalcock/blog/blob/master/2012/12/5/python-spell-checker.rst

Try jamspell - it works pretty well for automatic spelling correction (it consider context):尝试jamspell - 它适用于自动拼写校正(考虑上下文):

import jamspell

corrector = jamspell.TSpellCorrector()
corrector.LoadLangModel('en.bin')

corrector.FixFragment('Some sentnec with error')
# u'Some sentence with error'

However it can sometimes break correct words (less than 1% cases, according to their documentation)然而,它有时会破坏正确的单词(根据他们的文档,不到 1% 的情况)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM