简体   繁体   English

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

[英]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.

相关问题 Python 抛出错误 FileNotFound 而不是仅仅创建文件 - Python throwing error FileNotFound instead of just creating file 为什么Python会抛出此错误? - Why is Python throwing this error? Python BackSlash引发错误 - Throwing error with Python BackSlash 抛出 ZeroDivision 错误 Python - Throwing ZeroDivision Error Python 用Python修正打印的项目 - Correcting printed items in Python Python 模块 teradatasql 抛出“语法错误:预期在请求的开头和单词之间有一些东西” - Python module teradatasql throwing "Syntax error: expected something between the beginning of the request and the word" Python中的单词频率而不是列表 - Word frequencies in Python instead of listing RPI伺服帽python3库抛出错误:'board'没有属性'SCL' - RPI servo hat python3 library throwing error: 'board' has no attribute 'SCL' Python post 请求在请求库中抛出 400 'Bad Request' 错误,但适用于 cURL - Python post request throwing 400 'Bad Request' error with requests library but works with cURL Google Calendar API v3的Python客户端库插入事件引发“缺少结束时间”错误 - Python client library for Google Calendar API v3 insert event throwing “Missing end time” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM