我有以下两个文件,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
在代码中:
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
关键字时,会出现Fix AttributeError错误。