簡體   English   中英

NLTK單詞vs word_tokenize

[英]NLTK words vs word_tokenize

我正在探索一些NLTK的語料庫並遇到以下行為: word_tokenize()和單詞產生不同的單詞集()

以下是使用webtext的示例:

from nltk.corpus import webtext

當我運行以下,

len(set(word_tokenize(webtext.raw('wine.txt'))))

我明白了:3488

當我運行以下,

len(set(webtext.words('wine.txt')))

我明白了:3414

我在文檔中可以找到的是word_tokenize是一個標點符號和單詞列表。 但它也說單詞是標點符號和單詞列表。 我想知道,這里發生了什么? 他們為什么不同?

我已經嘗試過查看設定差異了。

U = set(word_tokenize(webtext.raw('wine.txt')))
V = set(webtext.words('wine.txt'))

tok_not_in_words = U.difference(V) # in tokenize but not in words
words_not_in_tok = V.difference(U) # in words but not in tokenize

我所能看到的是word_tokenize包含帶連字符的單詞,單詞分割帶連字符的單詞。

任何幫助表示贊賞。 謝謝!

首先讓我們看看兩種方法中的令牌計數,並查看最most_common單詞:

>>> import nltk
>>> from nltk import word_tokenize
>>> from nltk.corpus import webtext

>>> counts_from_wordtok = Counter(word_tokenize(webtext.raw('wine.txt')))
>>> counts_from_wordtok.most_common(10)
[(u'.', 2824), (u',', 1550), (u'a', 821), (u'and', 786), (u'the', 706), (u'***', 608), (u'-', 518), (u'of', 482), (u'but', 474), (u'I', 390)]

>>> counts_from_words = Counter(webtext.words('wine.txt'))
>>> counts_from_words.most_common(10)
[(u'.', 2772), (u',', 1536), (u'-', 832), (u'a', 821), (u'and', 787), (u'the', 706), (u'***', 498), (u'of', 482), (u'but', 474), (u'I', 392)]


>>> len(word_tokenize(webtext.raw('wine.txt')))
31140
>>> len(webtext.words('wine.txt'))
31350

有些東西聞起來很腥...

讓我們怎么細看webtext接口能夠實現,它采用了LazyCorpusLoaderhttps://github.com/nltk/nltk/blob/develop/nltk/corpus/ 初始化的.py#L235

webtext = LazyCorpusLoader(
    'webtext', PlaintextCorpusReader, r'(?!README|\.).*\.txt', encoding='ISO-8859-2')

如果我們看一下PlaintextCorpusReader如何加載語料庫並標記化https://github.com/nltk/nltk/blob/develop/nltk/corpus/reader/plaintext.py#L41

class PlaintextCorpusReader(CorpusReader):
    CorpusView = StreamBackedCorpusView

    def __init__(self, root, fileids,
                 word_tokenizer=WordPunctTokenizer(),
                 sent_tokenizer=nltk.data.LazyLoader(
                     'tokenizers/punkt/english.pickle'),
                 para_block_reader=read_blankline_block,
                 encoding='utf8'):

啊哈! 它使用WordPunctTokenizer而不是默認的修改TreebankTokenizer

WordPunctTokenizer是一個簡單的標記化程序,可在https://github.com/nltk/nltk/blob/develop/nltk/tokenize/regexp.py#L171找到

所述word_tokenize()函數是修飾TreebankTokenizer特有NLTK https://github.com/nltk/nltk/blob/develop/nltk/tokenize/ 初始化的.py#L97

如果我們看看什么是webtext.words()調用,我們遵循https://github.com/nltk/nltk/blob/develop/nltk/corpus/reader/plaintext.py#L81

def words(self, fileids=None):
    """
    :return: the given file(s) as a list of words
        and punctuation symbols.
    :rtype: list(str)
    """
    return concat([self.CorpusView(path, self._read_word_block, encoding=enc)
                   for (path, enc, fileid)
                   in self.abspaths(fileids, True, True)])

通過https://github.com/nltk/nltk/blob/develop/nltk/corpus/reader/plaintext.py#L119訪問_read_word_block()

def _read_word_block(self, stream):
    words = []
    for i in range(20): # Read 20 lines at a time.
        words.extend(self._word_tokenizer.tokenize(stream.readline()))
    return words

它正在逐行讀取文件!

因此,如果我們加載webtext語料庫並使用WordPunctTokenizer我們會得到相同的數字:

>>> from nltk.corpus import webtext
>>> from nltk.tokenize import WordPunctTokenizer
>>> wpt = WordPunctTokenizer()
>>> len(wpt.tokenize(webtext.raw('wine.txt')))
31350
>>> len(webtext.words('wine.txt'))
31350

更神秘

您還可以通過指定tokenizer對象來創建新的webtext語料庫對象,例如

>>> from nltk.tokenize import _treebank_word_tokenizer
>>> from nltk.corpus import LazyCorpusLoader, PlaintextCorpusReader
>>> from nltk.corpus import webtext

# LazyCorpusLoader expects a tokenizer object,
# but word_tokenize() is a function, so we have to 
# import the tokenizer object that word_tokenize wraps around
>>> webtext2 = LazyCorpusLoader('webtext', PlaintextCorpusReader, r'(?!README|\.).*\.txt', encoding='ISO-8859-2', word_tokenizer=_treebank_word_tokenizer)

>>> len(webtext2.words('wine.txt'))
28385

>>> len(word_tokenize(webtext2.raw('wine.txt')))
31140


>>> list(webtext2.words('wine.txt'))[:100]
[u'Lovely', u'delicate', u',', u'fragrant', u'Rhone', u'wine.', u'Polished', u'leather', u'and', u'strawberries.', u'Perhaps', u'a', u'bit', u'dilute', u',', u'but', u'good', u'for', u'drinking', u'now.', u'***', u'Liquorice', u',', u'cherry', u'fruit.', u'Simple', u'and', u'coarse', u'at', u'the', u'finish.', u'**', u'Thin', u'and', u'completely', u'uninspiring.', u'*', u'Rough.', u'No', u'Stars', u'Big', u',', u'fat', u',', u'textured', u'Chardonnay', u'-', u'nuts', u'and', u'butterscotch.', u'A', u'slightly', u'odd', u'metallic/cardboard', u'finish', u',', u'but', u'probably', u'***', u'A', u'blind', u'tasting', u',', u'other', u'than', u'the', u'fizz', u',', u'which', u'included', u'five', u'vintages', u'of', u'Cote', u'Rotie', u'Brune', u'et', u'Blonde', u'from', u'Guigal', u'.', u'Surprisingly', u'young', u'feeling', u'and', u'drinking', u'well', u',', u'but', u'without', u'any', u'great', u'complexity.', u'A', u'good', u'***', u'Charming', u',', u'violet-fragranced', u'nose.']

>>> word_tokenize(webtext2.raw('wine.txt'))[:100]
[u'Lovely', u'delicate', u',', u'fragrant', u'Rhone', u'wine', u'.', u'Polished', u'leather', u'and', u'strawberries', u'.', u'Perhaps', u'a', u'bit', u'dilute', u',', u'but', u'good', u'for', u'drinking', u'now', u'.', u'***', u'Liquorice', u',', u'cherry', u'fruit', u'.', u'Simple', u'and', u'coarse', u'at', u'the', u'finish', u'.', u'**', u'Thin', u'and', u'completely', u'uninspiring', u'.', u'*', u'Rough', u'.', u'No', u'Stars', u'Big', u',', u'fat', u',', u'textured', u'Chardonnay', u'-', u'nuts', u'and', u'butterscotch', u'.', u'A', u'slightly', u'odd', u'metallic/cardboard', u'finish', u',', u'but', u'probably', u'***', u'A', u'blind', u'tasting', u',', u'other', u'than', u'the', u'fizz', u',', u'which', u'included', u'five', u'vintages', u'of', u'Cote', u'Rotie', u'Brune', u'et', u'Blonde', u'from', u'Guigal', u'.', u'Surprisingly', u'young', u'feeling', u'and', u'drinking', u'well', u',', u'but', u'without', u'any', u'great']

這是因為word_tokenize做了sent_tokenize其實之前令牌化的句子進言: https://github.com/nltk/nltk/blob/develop/nltk/tokenize/ 初始化的.py#L113

但是PlaintextCorpusReader. _read_word_block() PlaintextCorpusReader. _read_word_block()事先沒有sent_tokenizehttps://github.com/nltk/nltk/blob/develop/nltk/corpus/reader/plaintext.py#L119

讓我們先用句子標記來重新計算:

>>> len(word_tokenize(webtext2.raw('wine.txt')))
31140

>>> sum(len(tokenized_sent) for tokenized_sent in webtext2.sents('wine.txt'))
31140

注: sent_tokenizerPlaintextCorpusReader使用sent_tokenizer=nltk.data.LazyLoader('tokenizers/punkt/english.pickle')其與共享相同的對象nltk.sent_tokenize()函數。

瞧!

為什么words()不首先進行句子標記化?

我想這是因為它最初使用WordPunctTokenizer不需要的字符串,句子首先表征,而TreebankWordTokenizer要求的字符串,首先表征。

為什么在“深度學習”和“機器學習”時代,我們仍在使用基於正則表達式的標記化器,而NLP中的其他所有東西都主要基於這些標記?

我沒有想法......但也有其他選擇,例如http://gmb.let.rug.nl/elephant/about.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM