I have a string, dictionary in the form:
('The puppy likes flowers',
{'laughter': (8.5, 0.9313),
'flowers': (7.88, 1.1718),
'the': (4.98, 0.9145),
'puppy': (7.58, 1.4581),
'died': (1.56, 1.198),
'laugh': (9.5, 0.1),
'flow': (2.3, 0.51),
'likes':(5.9, 0.032),
'like':(6.5, 0.021)
}
)
Each parentheses is a tuple which corresponds to (score, standard deviation). I’m taking the average of just the first integer in each tuple. I’ve tried this:
def score(string, d):
if len(string) == 0:
return 0
string = string.lower()
included = [d[word][0]for word in d if word in string]
return sum(included) / len(included)
When I run:
print score ('The puppy likes flower', {'laughter': (8.5, 0.9313), 'flower':
(7.88, 1.1718), 'the':(4.98, 0.9145), 'puppy':(7.58, 1.4581),
'died':(1.56, 1.198),'laugh': (9.5, 0.1),'flow': (2.3, 0.51)})
I should get the average of only 'the', 'puppy', 'likes' and 'flowers': 4.98 + 7.88 + 5.9 + 7.58 / 4 but this running function also includes 'like' and 'flow' : 4.98 + 7.88 + 5.9 + + 7.58 + 6.5 + 2.3 / 6.
First off using the variable string is not a great idea … but its OK here … you have a flaw in the logic … the following works
This will add a 0 for pieces of the string
sthat are not ind… if you wanted to ignore them use the following instead