1

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

asked Sep 25, 2017 at 14:53
2
  • 1
    corpus is a local variable name in pretreatment. Local variables are not shared; the name corpus in the global namespace is independent. You need to assign the returned value from the function to a global name. Commented Sep 25, 2017 at 14:55
  • 1
    Read about how variables work and variable scope. Commented Sep 25, 2017 at 14:55

2 Answers 2

2

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
answered Sep 25, 2017 at 14:56
Sign up to request clarification or add additional context in comments.

Comments

1

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")))
answered Sep 25, 2017 at 15:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.