我有一个字符串,字典形式为:
('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)
}
)
每个括号是对应于(分数,标准差)的元组。我只取每个元组中第一个整数的平均值。我试过这个:
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)
当我运行时:
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)})
我应该只得到'the'
,'puppy',
'likes'
和'flowers'
:的平均值,4.98 + 7.88 + 5.9 + 7.58 / 4
但是这个运行函数还包括'like'
和'flow'
: 4.98 + 7.88 + 5.9 + + 7.58 + 6.5 + 2.3 / 6
。