自动更正python库引发错误,而不是更正单词

问题描述 投票:0回答:2

我不确定这个自动更正为什么不起作用,但是每次我尝试使用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),但它甚至不起作用,因为它还会引发错误

python python-3.x autocorrect
2个回答
3
投票

您可能要查看autocorrect模块的文档,speller类的初始签名为def __init__(self, threshold=0, lang='en'):,因此,当创建该类的实例并传递单个参数时,它将假定您正在传递阈值。

因此,调用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."

0
投票

您正在尝试将要检查的代码传递给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时也遇到了问题-我想您希望对此有一个全局实例,并在对主函数的所有调用中使用它。

© www.soinside.com 2019 - 2024. All rights reserved.