軟工作業 4:結對專案之詞頻統計——基本功能
一、基本資訊
1、本次作業的地址:https://edu.cnblogs.com/campus/ntu/Embedded_Application/homework/2088
2、專案Git地址:https://gitee.com/ntucs/PairProg/tree/SE015_014
3.開發環境:Pycharm2017、Python3.6
4.結對成員:1613072015候玉權 、1613072014盧傑
二、專案分析
1.程式執行模組(方法、函式)介紹
1.1Task1:
(1)讀檔案到緩衝區,統計檔案的有效行數.
def process_file(dst): # 讀檔案到緩衝區 try: # 開啟檔案 doc = open(dst, "r") except IOError as e: print(e) return None try: # 讀檔案到緩衝區 bvffer = doc.read() except: print('Read File Error!') return None doc.close() return bvffer def count_line(dst): #計算文章的行數 lines = len(open(dst, 'r').readlines()) # 藉助readlines可以獲得文字的行數 print("line:",lines)
(2) 統計檔案的單詞總數.
def process_word(words): # 通過正則表示式證明是否符合單詞標準,計算單詞個數且返回單詞字典 if words: T = [] words_select = '[a-z]{4}(\w)*' for i in range(len(words)): word = re.match(words_select, words[i]) # 如果不匹配,返回NULL型別 if word: T.append(word.group()) print("words:{:}".format(len(T))) f = open('result.txt', 'a') print("words:{:}".format(len(T)),file=f) f.close() return T
(3) 統計檔案中各單詞的出現次數,最終只輸出頻率最高的10個。頻率相同的單詞,優先輸出字典序靠前的單詞。
def process_stopword(words, stopwords): word_freq = {} for word in words: #將轉換好的words中踢除stopwords.txt中的單詞 if word in stopwords: continue else: word_freq[word] = word_freq.get(word, 0) + 1 # 對單詞進行計數統計 return word_freq
(4)輸出到檔案result.txt
def save_result(lines, words, items): # 儲存結果到檔案(result.txt) result = open("result.txt", "w") # 以寫模式開啟,並清空檔案 result.write("lines:" + lines + "\n") # 寫入檔案result.txt result.write("words:" + words + "\n") for item in items: item = '<' + str(item[0]) + '>:' + str(item[1]) + '\n' result.write(item) result.close()
(5)主函式
def main(): dst = 'Gone_with_the_wind.txt' txt = 'stopwords.txt' b1 = process_file(dst) # 讀目標檔案 b2 = process_file(txt) # 讀stopwords.txt檔案 F = process_tr(b1) # 將目標檔案中的文章文體及字元轉換 S = process_tr(b2) # 將stopwords.txt檔案中的文章文體及字元轉換 count_line(dst) # 統計行數 T = process_word(F) # 計算單詞的個數 word_freq = process_stopword(T, S) # 輸出轉換好的words中踢除stopwords.txt中的前10個單詞 output_result(word_freq) print("高頻短語:") word_group(dst,word_freq) items = output_result(word_freq) lines = str(lines) words = str(words) save_result(lines, words, items)
1.2Task2:
停詞表模組(將停詞模組與統計高頻短語合二為一)
def word_group(dst,word_freq): #輸出頻率高的短語 str= open(dst, 'r').read() useless_Phrase={'would','that'} wordGroup = str.lower() tokens = nltk.wordpunct_tokenize(wordGroup) # 二字短語 finder = nltk.collocations.BigramCollocationFinder.from_words(tokens) # 刪除掉符合條件fn的片語 finder.apply_word_filter(lambda x: x in [',', '.', '’', '“', '”', '\'', '"', ',"', ',”']) print("頻率高的二字短語") # 這裡的key是排序依據,就是說先按t[1](詞頻)排序,-表示從大到小;再按照片語(t[0])排序,預設從a-z. print(sorted(finder.ngram_fd.items(), key=lambda t: (-t[1], t[0]))[:5]) # 三字短語 finder = nltk.collocations.TrigramCollocationFinder.from_words(tokens) # 刪除掉符合條件fn的片語 finder.apply_word_filter(lambda x: x in [',', '.', '’', '“', '”', '\'', '"', ',"', ',”']) print("頻率高的三字短語") print(sorted(finder.ngram_fd.items(), key=lambda t: (-t[1], t[0]))[:5]) for word in wordGroup: if word in useless_Phrase: continue else: word_freq[word] = word_freq.get(word, 0) + 1 # 將片語進行計數統計 return word_freq
三.程式演算法的時間、空間複雜度分析
以下面程式碼為例:
def count_line(dst): #計算文章的行數 lines = len(open(dst, 'r').readlines()) # 藉助readlines可以獲得文字的行數 print("line:",lines)
該程式只有一個函式,其時間複雜度為O(n),所以該程式時間複雜度為O(n)。同理空間複雜度也為O(n)。
四、程式執行截圖
任務一:
任務二:
五、效能分析 效能分析圖表六、結對程式設計開銷時間及照片
(1)結對程式設計開銷時間花費了一週時間左右。
(2)結對程式設計照片
七、事後分析與總結
1.簡述關於停詞表部分的決策過程 一開始準備使用nltk中的停詞表,但是考慮到時間開銷以及停詞效果顯示方面的原因,我們選擇建立stopwords.txt,自定義停詞內容,然後通過程式碼讀取txt文件中停用詞,在輸出詞頻前十的過程中進行剔除停用詞。
2.評價 (1)盧傑評價侯玉權:侯玉權同學學習能力強、對於不熟悉的方法上手很快。而且他做事認真且有條理、但是嚴謹性需加強。提出 使用nltk中的方法,思考角度為“不重複造輪子”,並且該工具包已經發展成熟,
在使用過程中並不會產生程式問題。美中不足的是對編寫程式背後的邏輯方面,不夠嚴謹,希望以後能夠多加思考。 (2)侯玉權評價盧傑:盧傑同學思維活躍、常常想出好的方法。不過他的基礎程式碼能力欠缺,需要繼續努力。使用正則表示式,針對2個詞彙的短語,與3個詞彙的短語編寫正則表示式,從文字中找出
符合要求的短語集合之後,進行短語統計(類似詞頻統計)。在完成任務的同時,積極幫助我解答疑問,受益匪淺,期待下一次的合作。
3.關於結對過程的建議 (1)可以以自由結對程式設計的形式開展,同學自主尋找的結對夥伴彼此比較熟悉能夠更有效率地開展結對程式設計工作;當然,不太熟悉的人結對程式設計也有一定好處,如可以鍛鍊個人的與他人交流溝通、相處之道。
4.其他 “三人行必有我師”,相互學習對方的技能,可以提升自己水平。工作及時得到同伴的肯定,自信心和成就感會增強,覺得工作很愉快,很願意很partner一起工作,這樣就會提高生產率。在程式設計中,相互討論,可能更快更有效地解決問題。