次の形式の文字列、辞書があります。
('(Laughter 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)
}
)
各括弧は (スコア、標準偏差) に対応するタプルです。各タプルの最初の整数だけの平均をとっています。私はこれを試しました:
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 ('(Laughter 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)})
'laughter'
and 'flower'
:のみの平均を取得する必要があります8.5 + 7.88 / 2
が、この実行中の関数には'laugh'
and 'flow'
:も含まれています8.5 + 7.88 + 9.5 + 2.3 /4
。