我试图在一组行上实现Markov属性。我需要所有的独特的词沿着下面的词相应的频率。
输入
文件名:示例.txt
I Love you
I Miss you
Miss you Baby
You are the best
I Miss you
代码段
from collections import Counter
import pprint
class TextAnalyzer:
text_file = 'example.txt'
def __init__(self):
self.raw_data = ''
self.word_map = dict()
self.prepare_data()
self.analyze()
pprint.pprint(self.word_map)
def prepare_data(self):
with open(self.text_file, 'r') as example:
self.raw_data=example.read().replace('\n', ' ')
example.close()
def analyze(self):
words = self.raw_data.split()
word_pairs = [[words[i],words[i+1]] for i in range(len(words)-1)]
self.word_map = dict()
for word in list(set(words)):
for pair in word_pairs:
if word == pair[0]:
self.word_map.setdefault(word, []).append(pair[1])
self.word_map[word] = Counter(self.word_map[word]).most_common(11)
TextAnalyzer()
实际产量
{'Baby': ['You'],
'I': ['Love', 'Miss', 'Miss'],
'Love': ['you'],
'Miss': ['you', 'you', 'you'],
'You': ['are'],
'are': ['the'],
'best': ['I'],
'the': ['best'],
'you': [('I', 1), ('Miss', 1), ('Baby', 1)]}
预期输出:
{'Miss': [('you',3)],
'I': [('Love',1), ('Miss',2)],
'Love': ['you',1],
'Baby': ['You',1],
'You': ['are',1],
'are': ['the',1],
'best': ['I',1],
'the': ['best'],
'you': [('I', 1), ('Miss', 1), ('Baby', 1)]}
我希望输出按最大频率排序。如何改进代码以实现该输出。
为了更接近预期结果,您可以编辑
analize
方法:这张照片:
这是你想要的,但没有分类。你需要写一个定制的打印方法来为你排序。你知道吗
例如,向类中添加以下方法:
用
self.printfreq()
替换pprint.pprint(self.word_map)
行将导致打印:长排序键允许按列表中的最大频率对字典键进行排序。你知道吗
编辑
我给
max
添加了一个默认参数。这允许避免在输入中有一个或多个不重复的单词时可能出现的ValueError: max() arg is an empty sequence
。你知道吗相关问题 更多 >
编程相关推荐