天天看點

學習筆記CB001:NLTK庫、語料庫、詞機率、雙連詞、詞典

聊天機器人知識主要是自然語言處理。包括語言分析和了解、語言生成、機器學習、人機對話、資訊檢索、資訊傳輸與資訊存儲、文本分類、自動文摘、數學方法、語言資源、系統評測。

NLTK庫安裝,pip install nltk 。執行python。下載下傳書籍,import nltk,nltk.download(),選擇book,點Download。下載下傳完,加載書籍,from nltk.book import 。輸入text書籍節點,輸出書籍标題。搜尋文本,text1.concordance("former”) 。搜尋相關詞,text1.similar("ship") 。檢視詞在文章的位置,text4.dispersion_plot(["citizens", "democracy", "freedom", "duties", "America"]) ,可以按Ctr+Z退出。繼續嘗試其他函數需要重新執行python,重新加載書籍。詞統計,總字數 len(text1),文本所有詞集合 set(text1),文本總詞數 len(set(text4)),單詞出現總次數 text4.count("is") ,統計文章詞頻從大到小排序到清單 FreqDist(text1),統計詞頻輸出累計圖 fdist1 = FreqDist(text1);fdist1.plot(50, cumulative=True),隻出現一次的詞 fdist1.hapaxes(),頻繁雙聯詞 text4.collocations() 。

自然語言處理關鍵點,詞意了解、自動生成語言,機器翻譯、人機對話(圖靈測試,5分鐘内回答提出問題的30%)。基于規則,完全從文法句法出發,照語言規則分析、了解。基于統計,收集大量語料資料,統計學習了解語言,得益于硬體(GPU)、大資料、深度學習的發展。

NLTK語料庫,Gutenberg,nltk.corpus.gutenberg.fileids()。Gutenberg語料庫檔案辨別符,import nltk,nltk.corpus.gutenberg.fileids()。Gutenberg語料庫閱讀器 nltk.corpus.gutenberg。輸出文章原始内容 nltk.corpus.gutenberg.raw('chesterton-brown.txt') 。輸出文章單詞清單 nltk.corpus.gutenberg.words('chesterton-brown.txt') 。輸出文章句子清單 nltk.corpus.gutenberg.sents('chesterton-brown.txt') 。網絡文本語料庫,網絡和聊天文本,from nltk.corpus import webtext 。布朗語料庫,按照文本分類好500個不同來源文本,from nltk.corpus import brown 。路透社語料庫,1萬多個新聞文檔,from nltk.corpus import reuters 。就職演說語料庫,55個總統的演說,from nltk.corpus import inaugural 。

語料庫組織結構,散養式(孤立多篇文章)、分類式(按照類别組織,但沒有交集)、交叉式(文章屬多個類)、漸變式(文法随時間發生變化)。

語料庫通用接口,檔案 fileids(),分類 categories(),原始内容 raw(),詞彙 words(),句子 sents(),指定檔案磁盤位置 abspath(),檔案流 open()。

加載自定義語料庫,from nltk.corpus import PlaintextCorpusReader ,corpus_root = '/Users/libinggen/Documents/workspace/Python/robot/txt' ,wordlists = PlaintextCorpusReader(corpus_root, '.*') ,wordlists.fileids() 。

格式轉換GBK2UTF8,iconv -f GBK -t UTF-8 安娜·卡列尼娜.txt > 安娜·卡列尼娜utf8.txt 。

條件分布,在一定條件下事件機率頒上。條件頻率分布,指定條件下事件頻率分布。

輸出布朗語料庫每個類别條件每個詞機率:

利用條件頻率分布,按照最大條件機率生成雙連詞,生成随機文本:

詞典資源,詞或短語集合:

詞彙清單語料庫,所有英文單詞,識别文法錯誤 nltk.corpus.words.words 。

停用詞語料庫,識别最頻繁出現沒有意義詞 nltk.corpus.stopwords.words 。

發音詞典,輸出英文單詞發音 nltk.corpus.cmudict.dict 。比較詞表,多種語言核心200多個詞對照,語言翻譯基礎 nltk.corpus.swadesh 。同義詞集,面向語義英語詞典,同義詞集網絡 WordNet 。

參考資料:

<a href="http://www.shareditor.com/blogshow/?blogId=63">http://www.shareditor.com/blogshow/?blogId=63</a>

<a href="http://www.shareditor.com/blogshow?blogId=64">http://www.shareditor.com/blogshow?blogId=64</a>

<a href="http://www.shareditor.com/blogshow?blogId=65">http://www.shareditor.com/blogshow?blogId=65</a>

歡迎推薦上海機器學習工作機會,我的微信:qingxingfengzi