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.
46 lines
1.0 KiB
46 lines
1.0 KiB
9 months ago
|
import string
|
||
|
from collections import Counter
|
||
|
from cppy.cp_util import *
|
||
|
|
||
8 months ago
|
################################
|
||
9 months ago
|
# data
|
||
8 months ago
|
################################
|
||
|
data = ''
|
||
9 months ago
|
words = []
|
||
|
word_freqs = []
|
||
|
|
||
|
################################
|
||
|
# procedures
|
||
|
################################
|
||
|
def read_file(path_to_file):
|
||
|
global data
|
||
|
with open(path_to_file,encoding='utf-8') as f:
|
||
8 months ago
|
data = f.read()
|
||
9 months ago
|
|
||
8 months ago
|
def extractwords():
|
||
9 months ago
|
global data
|
||
8 months ago
|
global words
|
||
|
words = data.lower().split()
|
||
9 months ago
|
with open(stopwordfilepath) as f:
|
||
|
stop_words = set(f.read().split(','))
|
||
|
stop_words.update(string.ascii_lowercase)
|
||
|
words = [word for word in words if word not in stop_words]
|
||
|
|
||
|
def frequencies():
|
||
|
global words
|
||
|
global word_freqs
|
||
|
word_freqs.extend([(word, 1) for word in words])
|
||
|
|
||
|
def sort():
|
||
|
global word_freqs
|
||
|
word_freqs = Counter(words).most_common()
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
read_file( testfilepath )
|
||
8 months ago
|
extractwords()
|
||
9 months ago
|
frequencies()
|
||
|
sort()
|
||
|
|
||
|
for tf in word_freqs[:10]:
|
||
|
print(tf[0], '-', tf[1])
|