如何使用Stemmer或Lemmatizer来阻止特定的单词

问题描述 投票:1回答:1

我目前正试图阻止一个大的语料库(aprox。800k句子)。我设法阻止了基本的一个。现在的问题是我只想阻止一个特定的单词,例如这个方法只适用于引理是原始单词的子串。例如,苹果这个词的后缀是apple和's'。但如果不是子串,它就不会像牙齿一样分裂成牙齿。

我还读到了关于词形变换器WordNet的内容,我们可以在其中添加一个pos参数,如动词,名词或形容词。有没有办法可以应用上面的方法?

提前致谢!

java python nlp stemming lemmatization
1个回答
0
投票

这是一个完整的例子 -

import nltk
from nltk.corpus import wordnet
from difflib import get_close_matches as gcm
from itertools import chain
from nltk.stem.porter import *

texts = [ " apples are good. My teeth will fall out.",
          " roses are red. cars are great to have"]

lmtzr = nltk.WordNetLemmatizer()
stemmer = PorterStemmer()

for text in texts:
    tokens = nltk.word_tokenize(text) # should sent tokenize it first
    token_lemma = [ lmtzr.lemmatize(token) for token in tokens ] # take your pick here between lemmatizer and wordnet synset.
    wn_lemma = [ gcm(word, list(set(list(chain(*[i.lemma_names() for i in wordnet.synsets(word)]))))) for word in tokens ]
    #print(wn_lemma) # works for unconventional words like 'teeth' --> tooth. You might want to take a closer look
    tokens_final = [ stemmer.stem(tokens[i]) if len(tokens[i]) > len(token_lemma[i]) else token_lemma[i] for i in range(len(tokens)) ]
    print(tokens_final)

产量

['appl', 'are', 'good', '.', 'My', 'teeth', 'will', 'fall', 'out', '.']
['rose', 'are', 'red', '.', 'car', 'are', 'great', 'to', 'have']

说明

注意stemmer.stem(tokens[i]) if len(tokens[i]) > len(token_lemma[i]) else token_lemma[i]这是神奇发生的地方。如果被词形化的单词是主要单词的一个子集,那么这个单词就会被取词,否则它就会被保留为词形。

注意

您正在尝试的词形还原有一些边缘情况。 WordnetLemmatizer不够聪明,无法处理像'牙齿' - >'牙齿'这样的特殊情况。在这些情况下,你会想看看可能派上用场的Wordnet.synset

我在评论中包含了一个小案例供您调查。

© www.soinside.com 2019 - 2024. All rights reserved.