2015-10-31 5 views
0

Как подсчитать количество раз, когда каждое пятизначное слово появляется в текстовом файле, а затем печатать пять наиболее часто встречающихся и наименее часто встречающихся слов с пятью буквами?Txt File-Dictionary-Frequency

До сих пор это то, что я написал, и передал некоторые из приведенных мне ответов. Я не могу заставить его произнести слова с пятью буквами и напечатать самые частые и наименее частые слова.

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])) 
+0

'counter [word] + = 1' –

+0

Возможный дубликат [количество частот элементов в python] (http://stackoverflow.com/questions/893417/item-frequency-count-in-python) – TessellatingHeckler

+0

' counter [word] + = 1' не должно быть отступом в выражении if. – ytpillai

ответ

0
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])) 
1

Есть TRY о collections.Counter A:

>>> 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] 
0

Проверить это

>>> 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]) 

Demo 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)] 

Demo 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().

Смежные вопросы