jangjusung / jusung-python

0 stars 0 forks source link

단어 빈도수 #15

Open jangjusung opened 2 years ago

jangjusung commented 2 years ago

import string import sys

def count_unique_word(): words = {} st = string.whitespace + string.punctuation + string.digits + "\"'" with open("./dataset/ihaveadream.txt", encoding='UTF-8') as file:
for line in file: for word in line.lower().split(): word = word.strip(st) words[word] = words.setdefault(word, 0) + 1

return words

dict_words=count_unique_word() dict_words

def majorityCnt(words): a=sorted(words.items(),key=lambda x:x[1], reverse=True) return a[0]

majorityCnt(dict_words)

import pickle with open("result.txt","wb") as f: pickle.dump(dict_words,f)

with open("result.txt","rb") as fin: data1=pickle.load(fin)

data1