【问题标题】:Does the lemmatization mechanism reduce the size of the corpus?词形还原机制是否会减小语料库的大小?
【发布时间】:2019-01-27 08:30:44
【问题描述】:

尊敬的社区成员,

在数据的预处理过程中,将 raw_data 拆分为标记后,我使用了流行的 WordNet Lemmatizer 来生成词干。我正在对具有 18953 个令牌的数据集进行实验。

我的问题是,词形还原过程会减少语料库的大小吗? 我很困惑,请在这方面提供帮助。任何帮助表示赞赏!

【问题讨论】:

  • 它不应该减少令牌的数量,但可能会减少令牌的集合
  • 好的,感谢您的回复。

标签: python python-3.x nltk wordnet lemmatization


【解决方案1】:

词形还原将句子中的每个标记(又名form)转换为其引理形式(又名type):

>>> from nltk import word_tokenize
>>> from pywsd.utils import lemmatize_sentence

>>> text = ['This is a corpus with multiple sentences.', 'This was the second sentence running.', 'For some reasons, there is a need to second foo bar ran.']

>>> lemmatize_sentence(text[0]) # Lemmatized sentence example.
['this', 'be', 'a', 'corpus', 'with', 'multiple', 'sentence', '.']
>>> word_tokenize(text[0]) # Tokenized sentence example. 
['This', 'is', 'a', 'corpus', 'with', 'multiple', 'sentences', '.']
>>> word_tokenize(text[0].lower()) # Lowercased and tokenized sentence example.
['this', 'is', 'a', 'corpus', 'with', 'multiple', 'sentences', '.']

如果我们对句子进行词形还原,每个标记都应该接收相应的词形,所以没有。无论是form 还是type,“单词”的数量都保持不变:

>>> num_tokens = sum([len(word_tokenize(sent.lower())) for sent in text])
>>> num_lemmas = sum([len(lemmatize_sentence(sent)) for sent in text])
>>> num_tokens, num_lemmas
(29, 29)


>>> [lemmatize_sentence(sent) for sent in text] # lemmatized sentences
[['this', 'be', 'a', 'corpus', 'with', 'multiple', 'sentence', '.'], ['this', 'be', 'the', 'second', 'sentence', 'running', '.'], ['for', 'some', 'reason', ',', 'there', 'be', 'a', 'need', 'to', 'second', 'foo', 'bar', 'ran', '.']]

>>> [word_tokenize(sent.lower()) for sent in text] # tokenized sentences
[['this', 'is', 'a', 'corpus', 'with', 'multiple', 'sentences', '.'], ['this', 'was', 'the', 'second', 'sentence', 'running', '.'], ['for', 'some', 'reasons', ',', 'there', 'is', 'a', 'need', 'to', 'second', 'foo', 'bar', 'ran', '.']]

“压缩”本身是指在您对句子进行词形还原后,整个语料库中表示的 unique 标记的数量,例如

>>> lemma_vocab = set(chain(*[lemmatize_sentence(sent) for sent in text]))
>>> token_vocab = set(chain(*[word_tokenize(sent.lower()) for sent in text]))
>>> len(lemma_vocab), len(token_vocab)
(21, 23)

>>> lemma_vocab
{'the', 'this', 'to', 'reason', 'for', 'second', 'a', 'running', 'some', 'sentence', 'be', 'foo', 'ran', 'with', '.', 'need', 'multiple', 'bar', 'corpus', 'there', ','}
>>> token_vocab
{'the', 'this', 'to', 'for', 'sentences', 'a', 'second', 'running', 'some', 'is', 'sentence', 'foo', 'reasons', 'with', 'ran', '.', 'need', 'multiple', 'bar', 'corpus', 'there', 'was', ','}

注意:词形还原是一个预处理步骤。但它应该用词形还原形式覆盖您的原始语料库。

【讨论】:

  • 非常感谢阿尔瓦斯。它有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-01
  • 2014-07-15
  • 2014-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多