.txtドキュメント内の上位10個の最も頻繁な単語、10個の最も頻度の低い単語、および単語の総数を計算する小さなPythonスクリプトがあります。割り当てによると、単語は2文字以上として定義されます。最も頻度の高い10個の単語と最も頻度の低い10個の単語が正常に印刷されますが、ドキュメント内の単語の総数を印刷しようとすると、1文字の単語(「a」など)を含むすべての単語の総数が印刷されます。 )。単語の総数を取得して、2文字以上の単語のみを計算するにはどうすればよいですか?
これが私のスクリプトです:
from string import *
from collections import defaultdict
from operator import itemgetter
import re
number = 10
words = {}
total_words = 0
words_only = re.compile(r'^[a-z]{2,}$')
counter = defaultdict(int)
"""Define function to count the total number of words"""
def count_words(s):
unique_words = split(s)
return len(unique_words)
"""Define words as 2 letters or more -- no single letter words such as "a" """
for word in words:
if len(word) >= 2:
counter[word] += 1
"""Open text document, strip it, then filter it"""
txt_file = open('charactermask.txt', 'r')
for line in txt_file:
total_words = total_words + count_words(line)
for word in line.strip().split():
word = word.strip(punctuation).lower()
if words_only.match(word):
counter[word] += 1
# Most Frequent Words
top_words = sorted(counter.iteritems(),
key=lambda(word, count): (-count, word))[:number]
print "Most Frequent Words: "
for word, frequency in top_words:
print "%s: %d" % (word, frequency)
# Least Frequent Words:
least_words = sorted(counter.iteritems(),
key=lambda (word, count): (count, word))[:number]
print " "
print "Least Frequent Words: "
for word, frequency in least_words:
print "%s: %d" % (word, frequency)
# Total Unique Words:
print " "
print "Total Number of Words: %s" % total_words
私はPythonの専門家ではありません。これは、現在受講しているPythonクラス用です。私のコードのすっきりと適切なフォーマットは、この割り当てでは私に不利になります。可能であれば、このコードのフォーマットが「グッドプラクティス」と見なされるかどうかを誰かに教えてもらえますか?