水里的一个tt人
幼苗
共回答了25个问题采纳率:92% 举报
不清楚clean_up函数做了什么,整个函数都是新写的,你参考一下吧.
def hapax_legomena_ratio(text):
at_least_once = []
at_least_twice = []
total = 0
for s in text:
for word in s.strip().split():
word = word.strip('.,;')
total += 1
if word not in at_least_once:
at_least_once.append(word)
elif word not in at_least_twice:
at_least_twice.append(word)
return (1.0 * (len(at_least_once)-len(at_least_twice)))/total
代码很简单,前面是为了把字符串分割成单词,然后统计单词总数 (total += 1).
后面是核心部分,统计单词是出现了最少一次,还是最少两次.
if word not in at_least_once:
at_least_once.append(word)
elif word not in at_least_twice:
at_least_twice.append(word)
最后的 return 语句用来计算只出现一次的单词个数 (至少出现一次个数 - 至少出现两次个数 = 只出现一次个数)和总单词数的比率.
1年前
追问
9
zhangjiafu
举报
谢谢!特别感谢!后面核心的部分懂了 前面单词分割的部分也懂了 可是split的方法还没学过 不可以用的话 要怎么吧字符串分割成单词呢