forked from p46318075/CodePattern
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
779 B
29 lines
779 B
9 months ago
|
import re
|
||
|
from cppy.cp_util import *
|
||
|
|
||
|
|
||
9 months ago
|
def extractwords(str_data):
|
||
9 months ago
|
pattern = re.compile('[\W_]+')
|
||
|
word_list = pattern.sub(' ', str_data).lower().split()
|
||
|
stop_words = get_stopwords()
|
||
|
return [w for w in word_list if not w in stop_words]
|
||
|
|
||
|
def frequencies(word_list):
|
||
|
word_freqs = {}
|
||
|
for word in word_list:
|
||
|
word_freqs[word] = word_freqs.get(word, 0) + 1
|
||
|
return word_freqs
|
||
|
|
||
|
def sort(word_freq):
|
||
|
return sorted( word_freq.items(), key=lambda x: x[1], reverse=True )
|
||
|
|
||
9 months ago
|
def printall(word_freqs, n = 10 ):
|
||
9 months ago
|
for word, freq in word_freqs[ :n ]:
|
||
|
print(word, '-', freq)
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
9 months ago
|
printall(sort(frequencies(
|
||
|
extractwords(
|
||
9 months ago
|
read_file( testfilepath ))))
|
||
|
)
|