【问题标题】:Using Counter on a list of Spacy tokens returns a non unique dict of the tokens在 Spacy 标记列表上使用 Counter 返回标记的非唯一字典
【发布时间】:2022-11-22 12:11:09
【问题描述】:

我想用计数器类计算一个 spacy 令牌列表。 IE。:

[hello,how,are,you,hello]

其中每个元素的类型都是<class 'spacy.tokens.token.Token'>。 但是,当我想通过计数器计算列表中每个令牌的出现次数时,如下所示:

    return Counter(joined)

结果是令牌的非唯一字典或者换句话说:与以前相同的列表,但它现在是一个字典,每个键的值为 1。在下面的屏幕截图中可以看到,字典中似乎有两次相同的键。

这是什么原因?

【问题讨论】:

  • 请添加代码 sn-p 以便每个人都可以重现结果!

标签: python dictionary nlp counter spacy


【解决方案1】:

如果标记具有相同的文本,它们就不是等价的,它们必须位于同一 Doc 对象中的相同位置。但是屏幕截图中的输出(不要发布文本屏幕截图...)只是令牌的 repr,这是它的文本。

如果只想计算文本,请使用 token.text,如下所示:

from collections import Counter
import spacy

nlp = spacy.blank("en")
doc = nlp("this is text, this is text")
out = Counter([tok.text for tok in doc])

【讨论】:

    猜你喜欢
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-10
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多