安裝
參考:https://www.cnblogs.com/zrmw/p/10869325.html
分詞:注意先分句再分詞,這些對象均來自nltk.tokenize庫
- word_tokenize
導入nltk的tokenize庫后,tokens = nltk.word_tokenize(sentence)語句進行分詞操作,sentence為待處理的字符串。返回一個列表。該方法要求被處理的字符串本身各個詞語之間有空格,能處理如don't, they'll等縮寫詞的情況。 - TweetTokenizer
Twitter-aware,按空格進行分詞,同時針對推文一些特性,去除@用戶名,保留表情等一些特殊符號。
分兩種:
(1)不帶參數token=TweetTokenizer().tokenize(sentence)處理。
輸入"This is a coooool #dummysmiley: 😃 😛 ❤️ and some arrows < > -> <--"
輸出['This', 'is', 'a', 'cooool', '#dummysmiley', ':', '😃', '😛', '❤️', 'and', 'some', 'arrows', '<', '>', '->', '<--']能夠拆分無效用的標點符號。
(2)帶參數token = TweetTokenizer(strip_handles=True, reduce_len = True).
輸入@remy: This is waaaaayyyy too much for you!!!!!!
輸出[':', 'This', 'is', 'waaayyy', 'too', 'much', 'for', 'you', '!', '!', '!']
當一個詞中相同字符連續出現3次以上,就只保留3個。設置strip_handles = True會刪去@xxx。 - MWETokenizer
tokenizer = MWETokenizer([('a', 'little'), ('a', 'little', 'bit'), ('a', 'lot')])
輸入tokenizer.tokenize('In a litte or a litte bit or a lot in spite of'.split()); tokenizer.add_mwe(('in', 'spite', 'of'))
輸出['In', 'a_little', 'or', 'a_little_bit', 'or', 'a_lot', 'in_spite_of']
該方法可對已經先保留的一些短語,或者組合,進行重組(對一些專有詞可以先進行保留,如F-16,最后重組已保留-)。 - RegexpTokenizer
使用到正則表達式進行分詞,如對一些金錢表示或者其他非空白序列。
tokenizer = RegexpTokenizer('\w+|$[\d.]+|\S+')
輸入"Good muffins cost $3.88\n in New York. Please buy me\n two of them.\n\n Thanks."
輸出['Good', 'muffins', 'cost', '$3.88', 'in', 'New', 'York', '.', 'Please', 'buy', 'me', 'two', 'of', 'them', '.', 'Thanks', '.'] - StanfordTokenizer
按空格進行分詞,對於$4.28之類的,將符號與數字分開。
輸入“Good muffins cost \(3.88\n in New York. Please buy me\n two of them.\n Thanks." 輸出['Good', 'muffins', 'cost', '\)', '3.88', 'in', 'New', 'York', '.', 'Please', 'buy', 'me', 'two', 'of', 'them', '.', 'Thanks', '.']
詞性標注
具體的符號意義,參考:https://www.cnblogs.com/elpsycongroo/p/9369111.html
情感詞典
nltk集成了情感詞典SentiwordNet,使用方法參考:https://stackoverflow.com/questions/38263039/sentiwordnet-scoring-with-python
參考
https://www.cnblogs.com/kylinsblog/p/7762675.html
吐槽
感覺官方文檔寫的是真爛,連一個詳細的API說明文檔都沒有。