修复属性错误:模块“this”没有属性“stemmedWords”; Python
我有以下两个文件,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在代码中:
尝试为您的模块和类使用不同的名称
来自 PEP- 8:包和模块名称:
更正确的说法是:
你有一个叫做这个的模块吗?为什么要导入它?
从我在 ProcessText 类中看到的情况来看,您正在尝试定义实例变量(我知道这一点是因为您执行此操作的方式与 java 类似):
但是要声明实例变量并初始化它们,在 python 中,它是以不同的方式完成的方式,:
当您尝试使用
this
关键字引用这些实例变量(尝试不导入this
)而不是使用self 时,会出现修复 AttributeError 错误
关键字。In the code:
Try to use a different name for your module and your class
From PEP-8: Package and Module Names:
Something more correct would be:
Do you have a module called this?, why are you importing it?
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):
But to declare instance variables and initialize them, in python it is done in a different way,:
The Fix AttributeError error occurs when you are trying to use the
this
keyword to reference these instance variables (try to not importthis
) instead of usingself
keyword.