Reputation: 602
I don't understand why I have the error
NameError: name 'corpus' is not defined
def pretreatment(fileName):
with open(fileName, 'r') as file:
global corpus
text = file.readlines()
corpus = []
for occurrence in text:
occurrence = occurrence.replace('.',' ')
occurrence = occurrence.replace(',',' ')
occurrence = occurrence.replace(';',' ')
occurrence = occurrence.replace('(',' ')
occurrence = occurrence.replace(')',' ')
occurrence = occurrence.replace('?',' ')
occurrence = occurrence.replace('!',' ')
occurrence = occurrence.replace(':',' ')
corpus.append(occurrence)
return corpus
def lexical_analysis(corpus):
global corpus
lexical_corpus = pretreatment(corpus)
tokens = nltk.word_tokenize(lexical_corpus)
return tokens
print(pretreatment("blabla.txt"))
print(lexical_analysis(corpus))
I have called the pretreatment function in the lexical_analysis function, but I still have an undefined variable error.
I would like to take advantage of and ask if there is a way to use the replace function in a more elegant way.
EDIT: Thank you for all the explanations. I just managed to declare the variables as global, I understood what the problem was and everything worked perfectly
global variableName
Upvotes: 1
Views: 20422
Reputation: 8
corpus is not defined in global scope, so you are getting error.
you can use like this,
corpus = pretreatment("blabla.txt")
print(lexical_analysis(corpus))
or
print(lexical_analysis(pretreatment("blabla.txt")))
Upvotes: 1
Reputation: 16743
corpus
is a local variable inside the function 'pretreatment`. Just because you have called the function doesn't mean it would (or should) begin to exist in the global scope. Try reading about scopes in Python.
print(lexical_analysis(pretreatment('blabla.txt')) # should work
Upvotes: 2