PythonNLTK:如何对英语中包含动词的文本进行柠檬化?

2024-04-25 00:01:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把这篇课文译成英语,它只是把名词译成英语,我还需要把动词译成英语

    >>> 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,你能告诉我怎样做才能使整篇课文变柠檬化吗


Tags: theinfromimporturlforstringraw
1条回答
网友
1楼 · 发布于 2024-04-25 00:01:16

在wordnetlemmatizer中使用pos参数:

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

相关问题 更多 >