自动更正 python 库抛出错误而不是更正单词

Autocorrect python library throwing error instead of correcting the word

我不太确定为什么这个自动更正不起作用,但每次我尝试使用 Speller() 功能时,我都会收到此错误:

TypeError: '>' not supported between instances of 'str' and 'int'

这是我的代码:

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)

编辑:我也试过 int(disisastringvariable) 但它甚至不起作用,因为它也会抛出错误

你可能想查看 autocorrect 模块的文档 speller class 初始化签名是 def __init__(self, threshold=0, lang='en'): 所以在创建 [=22 的实例时=] 并传入一个参数,它将假定您传入的是阈值。

因此调用 Speller("something") 将传递一个字符串,该字符串将被存储为阈值。然后在 init 方法的第 27 行。 if threshold > 0 该字符串将与 int 进行比较。因此错误。因为你不能在字符串和 int 之间做 >。

我建议先阅读此模块的所有文档。文档中的示例建议使用它

>>> 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."

您正在尝试将要检查的代码传递给 Speller 构造函数。
相反,您应该创建一次对象,然后它就可以调用并检查输入。在此处阅读示例: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)

您还遇到了在声明之前使用变量 name 的问题 - 我认为您希望拥有它的全局实例并在对主函数的所有调用中使用它。