Txt文件字典频率



如何计算每个五个字母的单词在文本文件中出现的次数,然后打印五个最频繁和最不频繁的五个字母单词?

到目前为止,这就是我所写的关于迄今为止向我展示的一些答案的内容。我无法把五个字母的单词给我,并把最频繁和最不频繁的单词打印出来。

counter = {}
in_file = open('tale_of_two_cities_ascii.txt', 'r')
content = in_file.read()

for line in in_file:
    for word in line.split():
        if len(word) != 5: continue
        if word not in counter:
            counter[word] = 0
            counter[word] += 1
words = sorted(counter, key=counter.get)
print("The five most frequent words:", ','.join(words[-5:]))
print("The five least frequent words:", ','.join(words[:5]))

试试collections.Counter:

>>> Counter('abracadabra').most_common(3)  # most common three items
[('a', 5), ('r', 2), ('b', 2)]
>>> Counter('abracadabra').most_common()[:-4:-1] # least common three items
[('d', 1), ('c', 1), ('b', 2)]

因此,解决方案可能是这样的:

import re
from collections import Counter
with open('your_text_file') as f:
    content = f.read()
    words = re.findall(r'w+', content)
    counter = Counter(words)
    most_common = [item[0] for item in counter.most_common() if len(item[0]) == 5][:5]
    least_common = [item[0] for item in counter.most_common() if len(item[0]) == 5][:-6:-1]
counter = {}
with open('tale_of_two_cities_ascii.txt') as infile:
    for line in infile:
        for word in line.strip():
            if len(word) != 5: continue
            if word not in counter: counter[word] = 0
            counter[word] += 1
words = sorted(counter, key=counter.__get__)
print("The five most common words are:", ','.join(words[-5:]))
print("The five least common words are:", ','.join(words[:5]))

查看

>>> import re
>>> from collections import Counter
>>> # 1st the text tokenizer
>>> TOKENS = lambda x: re.findall('[a-zA-Z]+', x)
>>> # 2nd counts the tokens with exactly 5 letters
>>> COUNTS = lambda txt: Counter([t for t in TOKENS(txt) if len(t) == 5])

演示1从文件读取文本

>>> # read some text file
>>> text = open('README.txt').read()
>>> # prints the most common 5 words in the counter
>>> print(COUNTS(text).most_common(5))
[('words', 3), ('Words', 3), ('model', 3), ('small', 2), ('Given', 1)]

演示2带有短文本

>>> demo = '''fives!! towes towes.. another fives cools, words NLP python fives'''
>>> print(COUNTS(demo).most_common(5))
[('fives', 3), ('towes', 2), ('words', 1), ('cools', 1)]

您也可以将TOKENS更改为您喜欢的模式,例如小写'[a-z]+', x.lower()

最新更新