【问题标题】:Am I missing the preprocessing function in spaCy's lemmatization?我错过了 spaCy 词形还原中的预处理功能吗?
【发布时间】:2020-10-03 15:24:13
【问题描述】:

我正在尝试使用 spacy 获取文档中所有标记的引理(即 token.lemma_)。

代码:

sentence = 'I'm looking for all of the lemmas. Please help me find them!'
nlp = spacy.load('en', disable=['parser', 'NER])
doc = nlp(sentence)
tokens = [tokens.lemma_ for token in doc]

预期结果:

['look', 'lemma', 'help', 'find']

实际结果:

[-PRON-, 'be', 'look', 'all', 'of', 'the', 'lemma', '.', 'please', 'help', '-PRON-', 'find', '-PRON', '!']

我在 spacy 中是否缺少某种预处理功能,还是必须单独进行预处理?我希望在词形还原之前删除所有标点符号和停用词。

【问题讨论】:

  • 您可以添加一个条件来排除不需要的令牌
  • @SergeyBushmanov 作为 spaCy 的一部分,还是作为结果的一个条件?
  • 第二个选项
  • 你说你需要得到所有的标记 lemmas,那么你为什么要谈论过滤掉一些东西呢?请澄清问题。注意be 是一个停用词,如果您使用if token.is_stop,它将被过滤,但您希望它出现在输出中。 [token.lemma_ for token in doc if not token.is_stop and not token.is_punct] 返回['look', 'lemma', 'help', 'find']。请为您的问题添加更多详细信息。
  • @WiktorStribiżew 我不知道“be”会成为这个的停用词,因为它不在 NLTK 停用词列表中。 is_stop 和 is_punct 正是我想要的。谢谢!

标签: python spacy lemmatization


【解决方案1】:

你可以使用

>>> [token.lemma_ for token in doc if not token.is_stop and not token.is_punct]
['look', 'lemma', 'help', 'find']

已添加以下部分:

  • if not token.is_stop - 如果令牌是停用词
  • and - 和
  • not token.is_punct - 如果标记是标点符号,则省略它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多