1

このコードが機能しないのはなぜですか?

def hapax_legomana_ratio(text):
''' Return the hapax_legomana ratio for this text.
This ratio is the number of words that occur exactly once divided
by the total number of words.
text is a list of strings each ending in \n.
At least one line in text contains a word.'''

uniquewords=dict()
words=0
for line in text:
    line=line.strip().split()
    for word in line:
        words+=1
        if word in words:
            uniquewords[word]-=1
        else:
            uniquewords[word]=1
HLR=len(uniquewords)/words

print (HLR)

テストすると、間違った答えが返ってきます。たとえば、9 個の文字列に 3 つの一意の単語がある場合、.33333 ではなく 0.20454545454545456 が返されます。

4

2 に答える 2