第三次作业

学号2017****1079  

姓名卜赫扬

码云仓库https://gitee.com/buheyang/piaopiaopiao/tree/SE1079

代码

from string import punctuation

def process_file(dst):   
    try:    
        f = open(dst, 'r')
    except IOError as s:
        print(s)
        return None
    try:   
        bvffer = f.read()
    except:
        print("Read File Error!")
        return None
    f.close()
    return bvffer

def process_buffer(bvffer):
    if bvffer:
        word_freq = {}
        bvffer = bvffer.lower()
        for ch in '“‘!;,.?”':
            bvffer = bvffer.lower().replace(ch, " ") 
        words = bvffer.strip().split()       
        for word in words:
            word_freq[word] = word_freq.get(word, 0)+1 
        return word_freq

def output_result(word_freq):
    if word_freq:
        sorted_word_freq = sorted(word_freq.items(), key=lambda v: v[1], reverse=True)
        for item in sorted_word_freq[:10]:
            print(item[0], item[1])
if __name__ == "__main__":
    dst ="C:/Users/buheyang/Desktop/wind.txt"
    bvffer = process_file(dst)
    word_freq = process_buffer(bvffer)
    output_result(word_freq)
运行结果: 第三次作业_第1张图片

你可能感兴趣的:(第三次作业)