为什么这段代码不起作用?
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 个唯一单词时,它给我 0.20454545454545456 而不是 .33333。