[英]Autocorrect python library throwing error instead of correcting the word
I'm not really sure why this autocorrect isnt working, but every time i try to use the Speller()
function i get this error:我不确定为什么这个自动更正不起作用,但每次我尝试使用
Speller()
函数时,我都会收到此错误:
TypeError: '>' not supported between instances of 'str' and 'int'
and here is my code:这是我的代码:
import time
from autocorrect import Speller
def main(consoleMode):
if consoleMode:
# beg fur input :D
inputVar = input("Console Mode input: ")
if Speller(inputVar.lower()) == "hi" or Speller(inputVar.lower()) == "hello" or Speller(inputVar.lower()) == "wassup" or Speller(inputVar.lower()) == "sup":
if name == None:
name = int(input("Hello!\nI'd like to get to know you.\nWhat's your name?\n> "))
if ("not" in Speller(name) and "tell" in Speller(name)) or ("not" in Speller(name) and "say" in Speller(name)):
print("Alright, I'll just call you Bob for now :)")
name = "Bob"
else:
print("Hey " + name + "!")
while True:
main(True)
edit: I also tried doing int(disisastringvariable)
but it just doesnt even work as it also throws an error编辑:我也试过做
int(disisastringvariable)
但它甚至不起作用,因为它也会引发错误
you might want to check out the documentation for the autocorrect
module the speller
class inits signature is def __init__(self, threshold=0, lang='en'):
So when creating an instance of the class and passing in a single argument it will assume you are passing in a threshold.你可能要检查出的文档
autocorrect
模块的speller
类inits签名def __init__(self, threshold=0, lang='en'):
所以,创建类的实例,并通过在一个单一的参数时,它会假设您正在通过一个阈值。
So calling Speller("something")
will pass a string in that will be stored as the threshold.因此,调用
Speller("something")
将传递一个字符串,该字符串将被存储为阈值。 then on line 27 of the init method.然后在 init 方法的第 27 行。
if threshold > 0
this string will be compared to an int. if threshold > 0
此字符串将与 int 进行比较。 Hence the error.因此错误。 Since you cannot do > between a string and an int.
因为你不能在字符串和整数之间做 > 。
I would suggest first read any documentation for this module.我建议首先阅读此模块的任何文档。 The example from the documenation suggest to use it like
文档中的示例建议使用它
>>> spell = Speller(lang='en')
>>> spell("I'm not sleapy and tehre is no place I'm giong to.")
"I'm not sleepy and there is no place I'm going to."
You are trying to pass the code you want to check to the Speller
constructor.您正在尝试将要检查的代码传递给
Speller
构造函数。
Instead, you should create the object once, and then it is callable and checks input.相反,您应该创建一次对象,然后它是可调用的并检查输入。 Read the examples here: https://github.com/fsondej/autocorrect
在此处阅读示例: https : //github.com/fsondej/autocorrect
import time
from autocorrect import Speller
my_speller = Speller(lang='en')
name = None
def main(consoleMode):
global name
if consoleMode:
# beg fur input :D
inputVar = input("Console Mode input: ")
if my_speller(inputVar.lower()) == "hi" or my_speller(inputVar.lower()) == "hello" or my_speller(inputVar.lower()) == "wassup" or my_speller(inputVar.lower()) == "sup":
if name == None:
name = input("Hello!\nI'd like to get to know you.\nWhat's your name?\n> ")
if ("not" in my_speller(name) and "tell" in my_speller(name)) or ("not" in my_speller(name) and "say" in my_speller(name)):
print("Alright, I'll just call you Bob for now :)")
name = "Bob"
else:
print("Hey " + name + "!")
while True:
main(True)
You also had a problem with the variable name
being used before declaration - I think you want to have a global instance of this and use it in all calls to your main function.您在声明之前使用的变量
name
也有问题 - 我认为您希望拥有一个全局实例并在对主函数的所有调用中使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.