I'm trying to count the number of words spoken by the characters "Michael"
and "Jim"
in the following dialogue and store them in a dictionary that looks like like {"Michael:":15, "Jim:":10}
.
string = "Michael: All right Jim. Your quarterlies look very good. How are things at the library? Jim: Oh, I told you. I couldn’t close it. So… Michael: So you’ve come to the master for guidance? Is this what you’re saying, grasshopper? Jim: Actually, you called me in here, but yeah. Michael: All right. Well, let me show you how it’s done."
I thought of creating an empty dictionary containing the character names as keys, splitting the string by " "
and then counting the number of resulting list elements between the the character names by using the keys as a reference and then storing the count of words as values. This is the code I've used so far:
dict = {"Michael:" : 0,
"Jim:" : 0}
list = string.split(" ")
indices = [i for i, x in enumerate(list) if x in dict.keys()]
nums = []
for i in range(1,len(indices)):
nums.append(indices[i] - indices[i-1])
print(nums)
The result is a list that prints as [15, 10, 15, 9]
I think I need help with the following:
- A better approach if possible
- A way to count the number of words spoken by a character when that line is the last line of the dialogue
- A way to update the dict with an automatic count of words spoken by the character
The last point is crucial because I'm trying to replicate this process for an episode's worth of quotes.
Thank you in advance!