社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

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

Blueberry Tech • 6 年前 • 2025 次点击  

我不确定为什么自动更正不起作用,但每次我尝试使用 Speller() 函数i得到此错误:

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/Django开发社区
本文地址:http://www.python88.com/topic/52224
文章 [ 2 ]  |  最新文章 6 年前
Simon Crane
Reply   •   1 楼
Simon Crane    6 年前

您正在尝试将要检查的代码传递给 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 在声明之前使用-我认为您希望有这个的全局实例,并在对主函数的所有调用中使用它。

Chris Doyle
Reply   •   2 楼
Chris Doyle    6 年前

您可能想查看 autocorrect 模块化 speller 类inits签名是 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."