我有一个文本文档,我正在使用正则表达式
和nltk
来查找该文档中最常见的前5
个单词。我必须打印出这些单词所属的句子,如何做到这一点?此外,我想将其扩展到在多个文档中查找常见单词,并返回它们的相应句子。
import nltk
import collections
from collections import Counter
import re
import string
frequency = {}
document_text = open('test.txt', 'r')
text_string = document_text.read().lower()
match_pattern = re.findall(r'\b[a-z]{3,15}\b', text_string) #return all the words with the number of characters in the range [3-15]
fdist = nltk.FreqDist(match_pattern) # creates a frequency distribution from a list
most_common = fdist.max() # returns a single element
top_five = fdist.most_common(5)# returns a list
list_5=[word for (word, freq) in fdist.most_common(5)]
print(top_five)
print(list_5)
输出:
[('you', 8), ('tuples', 8), ('the', 5), ('are', 5), ('pard', 5)]
['you', 'tuples', 'the', 'are', 'pard']
输出的是最常出现的单词,我需要打印这些单词所在的句子,该如何做呢?