軟工作業 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一起工作,這樣就會提高生產率。在編程中,相互討論,可能更快更有效地解決問題。
軟工作業 4:結對項目之詞頻統計——基本功能