分詞用到的一些方法和工具--NLTK的一些分詞方法


這些對象均來自nltk.tokenize庫

1. word_tokenize

  導入nltk的tokenize庫后,tokens = nltk.word_tokenize(sentence)語句進行分詞操作,sentence為待處理的字符串。返回一個列表。

  該方法要求被處理的字符串本身各個詞語之間有空格,能處理如don't, they'll等縮寫詞的情況。

2. TweetTokenizer

  Twitter-aware,按空格進行分詞,同時針對推文一些特性,去除@用戶名,保留表情等一些特殊符號。

  分兩種:

  (1)不帶參數token=TweetTokenizer().tokenize(sentence)處理。

      輸入"This is a coooool #dummysmiley: :-) :-P <3 and some arrows < > -> <--"

      輸出['This', 'is', 'a', 'cooool', '#dummysmiley', ':', ':-)', ':-P', '<3', '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。

3. 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,最后重組已保留-)。

4. 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', '.']

5. 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', '.']


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM