I'm trying to read through a text file word by word and then compare that word to a set of categorized arrays (joy, sadness, anger etc.) and if it is in that array, add the identifier for that array (a unique number assigned to them) to an empty list.
This is what I have:
anger = ['abandoned', 'abandonment', 'abhor', 'abhorrent', 'abolish',....]
joy = ['absolution', 'abundance', .....]
sadness = ['abandoned', ....]
trust = ['example word']
# Python program to read
# file word by word
DNA = []
# opening the text file
with open('ch1.txt','r', encoding="utf8") as file:
# reading each line
for line in file:
# reading each word
for word in line.split():
if word in joy:
DNA.append('6');
# reading each line
for line in file:
# reading each word
for word in line.split():
if word in anger:
DNA.append('2');
# reading each line
for line in file:
# reading each word
for word in line.split():
if word in fear:
DNA.append('5');
# reading each line
for line in file:
# reading each word
for word in line.split():
if word in sadness:
DNA.append('7');
# reading each line
for line in file:
# reading each word
for word in line.split():
if word in trust:
DNA.append('9');
# reading each line
for line in file:
# reading each word
for word in line.split():
if word in surprise:
DNA.append('8');
# reading each line
for line in file:
# reading each word
for word in line.split():
if word in disgust:
DNA.append('4');
print("Emotional DNA: " ,DNA)
What I want the code to do is something like this:
if word is in anger:
add anger to the list;
or if word is in sadness:
add sadness to the list;
The word can also appear in multiple categories and in that case both the category identifiers should be appended to the list.
This is the output I expect:
2, 2, 2, 4, 6, 4, 2, 3, 3, 4,8, 4, 5
In summary, all I want is to match the word being read in to see if it falls in one (or multiple) categories, and then add that category's identifier to the empty list. This is to map the progression of these emotion categories in the text file, one by one.
What is happening with the current code is that it is just reading through the first if statement and giving me this:
6, 6, 6, 6, 6, 6, 6
When I tried replacing the if statements after the first one with elif, only the first two statements were read and the others were ignored. I don't know how to make this if-else-if ladder work, any help would be appreciated.
Aucun commentaire:
Enregistrer un commentaire