修复属性错误:模块“this”没有属性“stemmedWords”; Python

发布于 2025-01-11 22:18:40 字数 2222 浏览 1 评论 0原文

我有以下两个文件,ProcessText.py 和 test.py,但是当我运行 test.py 时,出现上述错误。我检查了所有代码,属性设置没有任何问题(我认为)。我是 python 新手,但不是编程,所以如果我做了一些愚蠢的事情,请告诉我:)。从我在网上收集到的信息来看,这与导入有关,但我不太明白导入会干扰什么。

from ProcessText import ProcessText

class test:
    input = "input string goes here"
    ProcessText(input)
    tfDict = ProcessText.setTFIDF(input)
    for k, v in tfDict:
        print(k," : ",v )
import math
import string
import this

from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize

class ProcessText:
    tfDict = dict()
    stemmedWords = []
    lemmatizedWords = ""
    stemmedWordsRem = []
    sentences = []
    def __init__(self, input):
        lemmatizer = WordNetLemmatizer()
        text = word_tokenize(input)
        ps = PorterStemmer()
        this.stopWordsRem = [word for word in text if not word in stopwords.words()]  # removes stop words from input
        for each in this.stopWordsRem:  # stems input words
            this.stemmedWords.append(ps.stem(each))
        this.lemmatizedWords = [lemmatizer.lemmatize(w) for w in stemmedWords]  # lemmatizes each input word
        this.lemmatizedWords = ''.join(this.lemmatizedWords)
        this.emPunctuation = this.lemmatizedWords.translate(
            str.maketrans('', '', string.punctuation))  # strips punctuation from string
        this.sentences = this.lemmatizedWords.split(".")

    def setTFIDF(input):
        for word in this.remPunctuation:  # Finds the TF value of each word
            termsCount = 0
            if not (word in this.tfDict):
                for words in this.lemmatizedWords:
                    if (words == word):
                        termsCount += 1
                this.tfDict[word] = termsCount

        for k, v in this.tfDict.items():  # Finds the TF-IDF value of each word in the input text MIGHT need to add log to this
            documentsWithWord = 0
            for sentence in this.sentences:
                if sentence.find(k):
                    documentsWithWord += 1
            this.tfDict[k] = math.log((len(sentence) / documentsWithWord) * v)
        return this.tfDict

I have the following two files, ProcessText.py and test.py, yet when I run test.py I get the error above. I have checked all of the code and there is nothing wrong with the attribute settings(I think). I am new to python but not programming, so if I'm doing something stupid please let me know :). From what I've gathered online it's something to do with the importing, but I don't quite understand what import is messing with what.

from ProcessText import ProcessText

class test:
    input = "input string goes here"
    ProcessText(input)
    tfDict = ProcessText.setTFIDF(input)
    for k, v in tfDict:
        print(k," : ",v )
import math
import string
import this

from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize

class ProcessText:
    tfDict = dict()
    stemmedWords = []
    lemmatizedWords = ""
    stemmedWordsRem = []
    sentences = []
    def __init__(self, input):
        lemmatizer = WordNetLemmatizer()
        text = word_tokenize(input)
        ps = PorterStemmer()
        this.stopWordsRem = [word for word in text if not word in stopwords.words()]  # removes stop words from input
        for each in this.stopWordsRem:  # stems input words
            this.stemmedWords.append(ps.stem(each))
        this.lemmatizedWords = [lemmatizer.lemmatize(w) for w in stemmedWords]  # lemmatizes each input word
        this.lemmatizedWords = ''.join(this.lemmatizedWords)
        this.emPunctuation = this.lemmatizedWords.translate(
            str.maketrans('', '', string.punctuation))  # strips punctuation from string
        this.sentences = this.lemmatizedWords.split(".")

    def setTFIDF(input):
        for word in this.remPunctuation:  # Finds the TF value of each word
            termsCount = 0
            if not (word in this.tfDict):
                for words in this.lemmatizedWords:
                    if (words == word):
                        termsCount += 1
                this.tfDict[word] = termsCount

        for k, v in this.tfDict.items():  # Finds the TF-IDF value of each word in the input text MIGHT need to add log to this
            documentsWithWord = 0
            for sentence in this.sentences:
                if sentence.find(k):
                    documentsWithWord += 1
            this.tfDict[k] = math.log((len(sentence) / documentsWithWord) * v)
        return this.tfDict

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乄_柒ぐ汐 2025-01-18 22:18:40

在代码中:

from ProcessText import ProcessText

尝试为您的模块和类使用不同的名称

来自 PEP- 8:包和模块名称:

包和模块名称:应该是短的、全小写的名称。如果可以提高可读性,可以在模块名称中使用下划线。 Python 包也应该有短的、全小写的名称,尽管不鼓励使用下划线。

类名称:类名称通常应使用 CapWords 约定。在接口被记录并主要用作可调用的情况下,可以使用函数的命名约定。

更正确的说法是:

from myclass import MyClass

你有一个叫做这个的模块吗?为什么要导入它?

import this

从我在 ProcessText 类中看到的情况来看,您正在尝试定义实例变量(我知道这一点是因为您执行此操作的方式与 java 类似):

class ProcessText:
    tfDict = dict()
    stemmedWords = []
    lemmatizedWords = ""
    stemmedWordsRem = []
    sentences = []

但是要声明实例变量并初始化它们,在 python 中,它是以不同的方式完成的方式,:

class ProcessText:
    def __init__(self, input):
        self.tfDict = dict()
        self.stemmedWords = []
        self.lemmatizedWords = ""
        self.stemmedWordsRem = []
        self.sentences = []

当您尝试使用 this 关键字引用这些实例变量(尝试不导入 this)而不是使用 self 时,会出现修复 AttributeError 错误 关键字。

In the code:

from ProcessText import ProcessText

Try to use a different name for your module and your class

From PEP-8: Package and Module Names:

Package and Module Names: should have short, all-lowercase names. Underscores can be used in the module name if it improves readability. Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.

Class Names: Class names should normally use the CapWords convention. The naming convention for functions may be used instead in cases where the interface is documented and used primarily as a callable.

Something more correct would be:

from myclass import MyClass

Do you have a module called this?, why are you importing it?

import this

From what I see in the ProcessText class, you are trying to define instance variables (I know this because the way you are doing it is similar to java):

class ProcessText:
    tfDict = dict()
    stemmedWords = []
    lemmatizedWords = ""
    stemmedWordsRem = []
    sentences = []

But to declare instance variables and initialize them, in python it is done in a different way,:

class ProcessText:
    def __init__(self, input):
        self.tfDict = dict()
        self.stemmedWords = []
        self.lemmatizedWords = ""
        self.stemmedWordsRem = []
        self.sentences = []

The Fix AttributeError error occurs when you are trying to use the this keyword to reference these instance variables (try to not import this) instead of using self keyword.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文