【NLP】NLTK工具集使用
學(xué)習(xí)總結(jié)
文章目錄
學(xué)習(xí)總結(jié)
一、Natural Language Toolkit
二、常用語(yǔ)料庫(kù)和詞典
三、常用NLP工具集
3.1 分句
3.2 標(biāo)記解析
3.3 詞性標(biāo)注
Reference
一、Natural Language Toolkit
NLTK提供了多種語(yǔ)料庫(kù)(Corpora)和詞典(Lexicon)資源,如WordNet等,以及常用工具集,如分句、標(biāo)記解析(Tokenization)、詞干提?。⊿temming)、詞性標(biāo)注(POS Taggin)和句法分析(Syntactic Parsing)等,用于英文文本數(shù)據(jù)處理。
關(guān)于nltk的下載還是很多坑的,如果直接import nltk和nltk.download()下載失敗,可參考:
(1)nltk安裝失?。河捎谶B接方在一段時(shí)間后沒(méi)有正確答復(fù)或連接的主機(jī)沒(méi)有反應(yīng),連接嘗試失敗。
(2)直接下載github的nltk:https://github.com/nltk/nltk_data。我一開(kāi)始就是一直報(bào)錯(cuò)For more information see: https://www.nltk.org/data.html. Attempted to load tokenizers/punkt/english.pickle,然而nltk_data確實(shí)已經(jīng)解壓了還放在正確的路徑中了還不行,嘗試了幾個(gè)辦法后報(bào)錯(cuò)OSError: No such file or directory: 'D:\\anaconda1\\envs\\tensorflow\\lib\\nltk_data\\tokenizers\\punkt\\PY3\\english.pickle'發(fā)現(xiàn)木有PY3文件,加了個(gè)PY3文件夾后還是不行,最后直接去github上重新下載一個(gè)nltk的punkt包直接解壓就行了。。。
(3)如果還是不行,就絕對(duì)路徑吧sent_detector = nltk.data.load('D:\local\Anaconda3\Lib\site-packages//nltk-data//tokenizers/punkt/english.pickle'),狗頭滑稽。
注意:
nltk包放在的位置,可以通過(guò)如下代碼查看:
import nltk nltk.data.path
1
2
二、常用語(yǔ)料庫(kù)和詞典
常用語(yǔ)料庫(kù)(文本數(shù)據(jù)集),如圖書(shū)、電影評(píng)論和聊天記錄等,分為未標(biāo)注語(yǔ)料庫(kù)和人工標(biāo)注語(yǔ)料庫(kù)。
NLP任務(wù)中可以將一些停用詞(如冠詞a、the,介詞of、to等)刪除,提升計(jì)算速度,它們含義也不太重要。英文的常用停用詞:
from nltk.corpus import stopwords print(stopwords.words('english')) ['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers', 'herself', 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', 'should', 'now']
1
2
3
4
5
6
7
三、常用NLP工具集
3.1 分句
分句:將較長(zhǎng)的文檔切分為若干句子。
一個(gè)句子結(jié)尾一般有明顯標(biāo)志(如句號(hào)、問(wèn)好、感嘆號(hào)等)。
也有特殊情況,在英文中,句號(hào)不僅作為句尾標(biāo)志,還可以作為單詞的一部分,如Mr.
# 分句 from nltk.corpus import gutenberg from nltk.tokenize import sent_tokenize text = gutenberg.raw("austen-emma.txt") sentences = sent_tokenize(text) # 對(duì)Emma小說(shuō)全文分句 print(sentences[100]) # 顯示其中一個(gè)句子
1
2
3
4
5
6
其中一句的分句的結(jié)果為:
Mr. Knightley loves to find fault with me, you know-- in a joke--it is all a joke.
1
2
也可以自己寫(xiě)的句子試試,然后進(jìn)行分句:
from nltk.tokenize import sent_tokenize mytext = "Hello Adam, how are you? I hope everything is going well. Today is a good day, see you dude." print(sent_tokenize(mytext))
1
2
3
分句的結(jié)果為:
['Hello Adam, how are you?', 'I hope everything is going well.', 'Today is a good day, see you dude.']
1
3.2 標(biāo)記解析
NLP最基本的輸入單元:標(biāo)記Token,它可以是一個(gè)詞或標(biāo)點(diǎn)符號(hào)。
任務(wù)如,將句子結(jié)尾標(biāo)點(diǎn)符號(hào)和前面的單詞進(jìn)行拆分。
可以使用nltk.tokenize.word_tokenize。
這里接著上面的一個(gè)句子sentences[100]進(jìn)行標(biāo)記解析:
# 標(biāo)記解析 from nltk.tokenize import word_tokenize print(word_tokenize(sentences[100]))
1
2
3
得到的該句子的每個(gè)token標(biāo)記:
['Mr.', 'Knightley', 'loves', 'to', 'find', 'fault', 'with', 'me', ',', 'you', 'know', '--', 'in', 'a', 'joke', '--', 'it', 'is', 'all', 'a', 'joke', '.']
1
3.3 詞性標(biāo)注
根據(jù)詞語(yǔ)上下文,確定具體詞性。
如They sat by the fire和They fire a gun的fire意思不同,前者是名詞,后者是動(dòng)詞。
# 詞性標(biāo)記 from nltk import pos_tag # 對(duì)句子標(biāo)記解析后再進(jìn)行詞性標(biāo)注 In [3]:pos_tag(word_tokenize("They sat by the fire.")) Out[3]: [('They', 'PRP'), ('sat', 'VBP'), ('by', 'IN'), ('the', 'DT'), ('fire', 'NN'), ('.', '.')] In [4]:pos_tag(word_tokenize("They fire a gun.")) Out[4]: [('They', 'PRP'), ('fire', 'VBP'), ('a', 'DT'), ('gun', 'NN'), ('.', '.')]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
從上面詞性標(biāo)注的結(jié)果看出,前者句子的fire被標(biāo)注為名詞(NN),后者被標(biāo)注為動(dòng)詞(VBP),如果不知道詞性單詞的含義,可以help查詢:
nltk.help.upenn_tagset('NN')
1
Reference
(1)NLTK官網(wǎng):https://www.nltk.org/
(2)https://github.com/nltk/nltk_data
自然語(yǔ)言處理基礎(chǔ)
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。