Getting error in 2nd function but not in 1st UnboundLocalError: local variable 'data_list' referenced before assignment

Viewed 11

I am not getting error here:

def sentiment_analysis(data):

for c in ['?','●','.',',','|','>','<','(',')']:
    if c in data:
        data=data.replace(c,'')
        data_list = data.split()

# printing the data
print(data_list,'\n')

path = 'C:\\Users\\p12m9\\Documents\\Python Coding\\Data Science Projects\\Job Assignment\\Job 2 (Web Crawling)\\StopWords'
os.chdir(path)
for files in os.listdir():
    if files.endswith(".txt"):
        file_path = f"{path}\{files}"
        with open(file_path, 'r') as f:
            stop_words = f.read()
            c = ['?','●','.',',','|','>','<','(',')']
            stop_word_list = [i for i in stop_words if i not in c]
            data_list = [x.upper() for x in data_list]
            data_list = [i for i in data_list if i not in stop_words]
            data_list = [x.lower() for x in data_list]
            data_list = [i for i in data_list if i not in stop_words]
# printing the data
print(data_list,'\n')

with open('C:\\Users\\p12m9\\Documents\\Python Coding\\Data Science Projects\\Job Assignment\\Job 2 (Web Crawling)\\MasterDictionary\\positive-words.txt')as f:
    plist = f.read().splitlines()

with open('C:\\Users\\p12m9\\Documents\\Python Coding\\Data Science Projects\\Job Assignment\\Job 2 (Web Crawling)\\MasterDictionary\\negative-words.txt')as f:
    nlist = f.read().splitlines()

pscore = 0
nscore = 0
for w in data_list:
    if w in plist:
        pscore = pscore + 1
    elif w in nlist:
        nscore = nscore + 1
    else:
        pass
polarity_score = (pscore - nscore) / ((pscore + nscore) + 0.000001)
s_score = (pscore + nscore)/ ((len(data_list)) + 0.000001)
print('Positive Score is',pscore,'\n','Negative Score is',nscore,'\n','Polarity Score is',polarity_score,'\n','Subjectivity Score is', s_score,'\n')

But I am getting error here def words_list(data):

for c in ['?','●','.',',','|','>','<','(',')']:
    if c in data:
        data=data.replace(c,'')
        data_list = data.split()
path = 'C:\\Users\\p12m9\\Documents\\Python Coding\\Data Science Projects\\Job Assignment\\Job 2 (Web Crawling)\\StopWords'
os.chdir(path)
for files in os.listdir():
    if files.endswith(".txt"):
        file_path = f"{path}\\{files}"
        with open(file_path, 'r') as f:
            stop_words = f.read()
            c = ['?','●','.',',','|','>','<','(',')']
            
            stop_word_list = [i for i in stop_words if i not in c]
            data_list = [x.upper() for x in data_list]
            data_list = [i for i in data_list if i not in stop_words]
            data_list = [x.lower() for x in data_list]
            data_list = [i for i in data_list if i not in stop_words]
return data_list

This is the error

Traceback (most recent call last): File "C:\Users\p12m9\Documents\Python Coding\Data Science Projects\Job Assignment\Job 2 (Web Crawling)\Code with Files\code3.py", line 159, in word = words_list(data) File "C:\Users\p12m9\Documents\Python Coding\Data Science Projects\Job Assignment\Job 2 (Web Crawling)\Code with Files\code3.py", line 27, in words_list data_list = [x.upper() for x in data_list] UnboundLocalError: local variable 'data_list' referenced before assignment

1 Answers

You only initialize data_list if you match this condition

if c in data:
        data=data.replace(c,'')
        data_list = data.split()

Which mean that if for some reason you never match this condition, ( aka there's no ['?','●','.',',','|','>','<','(',')']: in data ), data_list will never be initialized, ence why you get this error

To avoid that you could initialize data_list earlier in the code as an empty list for instance.

Related