1. 程式人生 > 實用技巧 >詞頻統計 SPEC

詞頻統計 SPEC

此作業的要求參見https://edu.cnblogs.com/campus/nenu/2020Fall/homework/11206

功能1小檔案輸入。 為表明程式能跑,結果真實而不是迫害老五,請他親自鍵
盤在控制檯下輸入命令。

重點/難點 :

(1)如何將.py檔案打包成.exe檔案

詳情見:https://www.cnblogs.com/smart-zihan/p/11881172.html

(2)如何處理標點符號問題

字元預處理:

def openFile(filePath):
    with open(filePath, "r", encoding="utf-8") as file:
        filecontent 
= file.read() for replaceChar in '!"#$&()*+,-./:;<=>?@[\\]^_{|}·~“”‘’': filecontent = filecontent.replace(replaceChar, " ") return filecontent.lower().split()

執行效果圖:

功能2支援命令列輸入英文作品的檔名,請老五親自錄入。

重點/難點:對大量資料的處理

程式碼:

def sortAndprint(wordList):
    print("\n")
    wordDict 
= {} for word in wordList: wordDict[word] = wordDict.get(word, 0) + 1 wordDict_List=list(wordDict.items()) wordDict_List.sort(key=lambda x:x[1],reverse=True) print("{0:<10}{1}".format('total',len(wordDict_List))) if(len(wordDict_List) > 10): for i in range(10): word,count
=wordDict_List[i] print("{0:<10}{1}".format(word,count)) else: for i in range(len(wordDict_List)): word,count =wordDict_List[i] print("{0:<10}{1}".format(word,count)) return

截圖:

功能3支援命令列輸入儲存有英文作品檔案的目錄名,批量統計。

程式碼:

elif ((args.filePath != None) and (os.path.isdir(args.filePath) == True) and (args.s == None)):
    filePathList = os.listdir(args.filePath)
    for file in filePathList:
        print('File:' + file.split('.')[0])
        sortAndprint(openFile(args.filePath + '\\' + file))
    pass

截圖:

功能4從控制檯讀入英文單篇作品,這不是為了打臉老五,而是為了向你女朋
友炫酷,表明你能提供更適合嵌入指令碼中的作品(或者如她所說,不過是更靈活
的介面)。如果讀不懂需求,請教師兄師姐,或者 bing: linux 重定向,儘管
這個功能在windows下也有,搜尋關鍵詞中加入linux有利於迅速找到。

重點/難點:對重定向的理解

程式碼:

elif ((args.filePath != None) and(os.path.isfile(args.filePath) != True) and (args.s == None) and (os.path.isdir(args.filePath) != True)):
    #print('File:' + args.filePath)
    args.filePath=args.filePath+".txt"
    sortAndprint(openFile(args.filePath))
    pass    

截圖:

PSP表格

PSP階段 預計花費時間 實際花費時間 原因
功能1 126min 139min 對於python知識的不熟悉,不會處理標點符號
功能2 130min 148min 其他事情耽擱
功能3 104min 165min 學習python中讀取流知識
功能4 150min 196min 理解重定向概念

程式碼及版本控制
(5分。雖然只有5分,但此題如果做錯,因為教師得不到你的程式碼,所以會導致“功能實現”為負分。)
程式碼要求在 coding.net 做版本控制。要求push&pull時使用git客戶端,不允許使用web頁面。

答:

程式碼地址:

https://a123098.coding.net/p/word/d/jia/git