Reputation: 11
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
Upvotes: 1
Views: 455
Reputation:
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.
Upvotes: 1