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
corpusis a local variable name inpretreatment. Local variables are not shared; the namecorpusin the global namespace is independent. You need to assign the returned value from the function to a global name.