python - 如何对句子列表进行词形还原

标签 python list nltk lemmatization

如何在 Python 中对句子列表进行词形还原?

from nltk.stem.wordnet import WordNetLemmatizer
a = ['i like cars', 'cats are the best']
lmtzr = WordNetLemmatizer()
lemmatized = [lmtzr.lemmatize(word) for word in a]
print(lemmatized)

这是我尝试过的方法,但它给了我相同的句子。我需要先对单词进行分词才能正常工作吗?

最佳答案

长话短说:

pip3 install -U pywsd

然后:

>>> from pywsd.utils import lemmatize_sentence

>>> text = 'i like cars'
>>> lemmatize_sentence(text)
['i', 'like', 'car']
>>> lemmatize_sentence(text, keepWordPOS=True)
(['i', 'like', 'cars'], ['i', 'like', 'car'], ['n', 'v', 'n'])

>>> text = 'The cat likes cars'
>>> lemmatize_sentence(text, keepWordPOS=True)
(['The', 'cat', 'likes', 'cars'], ['the', 'cat', 'like', 'car'], [None, 'n', 'v', 'n'])

>>> text = 'The lazy brown fox jumps, and the cat likes cars.'
>>> lemmatize_sentence(text)
['the', 'lazy', 'brown', 'fox', 'jump', ',', 'and', 'the', 'cat', 'like', 'car', '.']

否则,看看pywsd中的函数是怎样的:

  • 标记字符串
  • 使用词性标注器并映射到 WordNet 词性标签集
  • 试图阻止
  • 最后用 POS 和/或词干调用词形还原器

参见 https://github.com/alvations/pywsd/blob/master/pywsd/utils.py#L129

关于python - 如何对句子列表进行词形还原,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50685343/

相关文章:

python - 使用 GridSearchCV 训练数据给我 ValueError,Sci-kit learn

python - 从我的嵌套和标记化列表中删除标点符号

python - 与NLTK库相关的一段Python代码在不同计算机上的不同结果

Python 列表轮换

c++ - 我应该使用什么类型的容器来存储我的 ObjectID 整数?

python - 如何将此字符串列表拆分为整数列表列表?

python - 使用 spacy 和 Matcher 提取 NER 主语 + 动词的问题

python - 将六位数字列拆分为一位数的分隔列

Python - 当 `new` 参数不是默认值时,为什么模拟补丁装饰器不将模拟对象传递给测试函数

python - 类型错误 : unsupported operand type(s) in "print >> ..." statement