0

I am working on a novel and I need to find all the adjectives associated to three different characters in order to compare their descriptions. Any idea on how I can do that? I am Python beginner :-)

I thought about creating three different dictionaries with the adjectives and their frequencies, but any suggestion will be appreciated.

Somebody suggested this:

# Tokenize the text into words
words = word_tokenize(verga_eros)

# Use NLTK's part-of-speech (POS) tagger to tag each word with its part of speech
tagged_words = pos_tag(words)

# Create a dictionary to store the adjectives associated with each character
adjectives = {
    "Marie": [],
    "Lucy": [],
    "Ella": []
}

# Loop through each tagged word and extract the adjectives associated with each character

for i in range(len(tagged_words)):
    word, pos = tagged_words[i]
    if pos == "JJ":  # If the word is an adjective
        if i > 0 and tagged_words[i-1][0] == "Marie":  # If the previous word is "Marie"
            adjectives["Marie"].append(word)
        elif i > 0 and tagged_words[i-1][0] == "Lucy":  # If the previous word is "Lucy"
            adjectives["Lucy"].append(word)
        elif i > 0 and tagged_words[i-1][0] == "Ella":  # If the previous word is "Ella"
            adjectives["Ella"].append(word)

# Print the adjectives associated with each character
print("Adjectives for Marie:", adjectives["Marie"])
print("Adjectives for Lucy:", adjectives["Lucy"])
print("Adjectives for Ella:", adjectives["Ella"])

But I need to use stanza instead of nltk!

30user08
  • 1
  • 2

1 Answers1

0

I'm not sure what kind of story or characters you want, but for example, you could create a dictionary with your characters with as many personality adjectives in a list as you'd like. Like this one:

    my_characters = {
  "Billy Bob": ["Aggressive","Depressed"],
  "Jim Carrey": ["Sus","Funny"],
  "Kevin Hart": ["Hilarious", "Short"]
}

print(f"Billy Bob is {' and '.join(my_characters['Billy Bob'])}")

You can index the key from your dictionary variable and display the values from there.

Ray953
  • 47
  • 5
  • Thank you! It is a nice solution but not quite what I needed. I have to use stanza and pos tagging. – 30user08 Mar 20 '23 at 11:01