我列出了十个最常见的单词——学术文章摘要。我想统计一下这些单词在我的数据集中出现了多少次。
排名前十的单词是:
top10 = ['model','language','models','task', 'data', 'paper', 'results', 'information', 'text','performance']
前3个观测值的一个例子是:
column[0:3] = ['The models are showing a great performance.',
'The information and therefor the data in the text are good enough to fulfill the task.',
'Data in this way results in the best information and thus performance'.]
所提供的代码应返回特定观察中所有单词的总出现次数列表。我尝试过以下代码,但它给出了错误:count((最多需要3个参数(给定10个(。
我的代码:
count = 0
for sentence in column:
for word in sentence.split():
count += word.lower().count('model','language','models','task', 'data', 'paper', 'results', 'information', 'text','performance')
我还想将所有单词小写并删除标点符号。所以输出应该是这样的:
output = (2, 4, 4)
第一次观察统计了前10名中的2个单词,即型号和性能
第二次观察统计前10个列表中的4个单词,即信息、数据、文本和任务
第三次观察计数数据、结果、数据、信息和性能的4个字
希望你能帮我!
您可以使用regex进行拆分,只需检查它是否在前10名中。
count =[]
for i,sentence in enumerate(column):
c = 0
for word in re.findall('w+',sentence):
c += int(word.lower() in top10)
count += [c]
计数=[2,4,4]