Python NLTK:如何对英语动词进行词形还原?

3 投票
1 回答
11140 浏览
提问于 2025-04-18 09:52

我想对这段文字进行词形还原,但它只对名词进行了还原,我还需要对动词进行还原。

    >>> import nltk, re, string
    >>> from nltk.stem import WordNetLemmatizer
    >>> from urllib import urlopen
    >>> url="https://raw.githubusercontent.com/evandrix/nltk_data/master/corpora/europarl_raw/english/ep-00-01-17.en"
    >>> raw = urlopen(url).read()
    >>> raw ="".join(l for l in raw if l not in string.punctuation)
    >>> tokens=nltk.word_tokenize(raw)
    >>> from nltk.stem import WordNetLemmatizer
    >>> lemmatizer = WordNetLemmatizer()
    >>> lem = [lemmatizer.lemmatize(t) for t in tokens]
    >>> lem[:20]
['Resumption', 'of', 'the', 'session', 'I', 'declare', 'resumed', 'the', 'session', 'of', 'the', 'European', 'Parliament', 'adjourned', 'on', 'Friday', '17', 'December', '1999', 'and']

这里的动词“resumed”应该变成“resume”,你能告诉我该怎么做才能对整个文本进行词形还原吗?

1 个回答

14

在使用 pos 参数时,涉及到 wordnetlemmatizer:

>>> from nltk.stem import WordNetLemmatizer
>>> from nltk import pos_tag
>>> wnl = WordNetLemmatizer()
>>> wnl.lemmatize('resumed')
'resumed'
>>> wnl.lemmatize('resumed', pos='v')
u'resume'

这里有一段完整的代码,里面包含了 pos_tag 函数:

>>> from nltk import word_tokenize, pos_tag
>>> from nltk.stem import WordNetLemmatizer
>>> wnl = WordNetLemmatizer()
>>> txt = """Resumption of the session I declare resumed the session of the European Parliament adjourned on Friday 17 December 1999 , and I would like once again to wish you a happy new year in the hope that you enjoyed a pleasant festive period ."""
>>> [wnl.lemmatize(i,j[0].lower()) if j[0].lower() in ['a','n','v'] else wnl.lemmatize(i) for i,j in pos_tag(word_tokenize(txt))]
['Resumption', 'of', 'the', 'session', 'I', 'declare', u'resume', 'the', 'session', 'of', 'the', 'European', 'Parliament', u'adjourn', 'on', 'Friday', '17', 'December', '1999', ',', 'and', 'I', 'would', 'like', 'once', 'again', 'to', 'wish', 'you', 'a', 'happy', 'new', 'year', 'in', 'the', 'hope', 'that', 'you', u'enjoy', 'a', 'pleasant', 'festive', 'period', '.']

撰写回答