長いテキスト文字列を解析し、Python で各単語が出現する回数を計算しています。私は機能する関数を持っていますが、それをより効率的にする方法があるかどうか (速度の点で) があるかどうか、およびこれを実行できる Python ライブラリ関数があるかどうかについてのアドバイスを探しているので、車輪の再発明はしていません。 ?
長い文字列 (通常、文字列内の 1000 語以上) に出現する最も一般的な単語を計算するためのより効率的な方法を提案できますか?
また、最初の要素が最も一般的な単語、2 番目の要素が 2 番目に一般的な単語などのリストに辞書を並べ替える最良の方法は何ですか?
test = """abc def-ghi jkl abc
abc"""
def calculate_word_frequency(s):
# Post: return a list of words ordered from the most
# frequent to the least frequent
words = s.split()
freq = {}
for word in words:
if freq.has_key(word):
freq[word] += 1
else:
freq[word] = 1
return sort(freq)
def sort(d):
# Post: sort dictionary d into list of words ordered
# from highest freq to lowest freq
# eg: For {"the": 3, "a": 9, "abc": 2} should be
# sorted into the following list ["a","the","abc"]
#I have never used lambda's so I'm not sure this is correct
return d.sort(cmp = lambda x,y: cmp(d[x],d[y]))
print calculate_word_frequency(test)