nlp - Spacy:计算每个句子中特定标记的出现次数

标签 nlp counter spacy find-occurrences spacy-3

我想使用 spacy 计算语料库中每个句子的标记 的出现次数,并将每个句子的结果附加到列表中。到目前为止,下面的代码返回关于的总数(对于整个语料库)。

3 个句子的示例/期望输出:['1', '0', '2'] 当前输出:[3]

doc = nlp(corpus)
nb_and = []
for sent in doc.sents:
    i = 0
    for token in sent:
        if token.text == "and":
            i += 1
            nb_and.append(i)

最佳答案

你需要在每个句子被处理后,将i附加到nb_and:

for sent in doc.sents:
    i = 0
    for token in sent:
        if token.text == "and":
            i += 1
    nb_and.append(i)

测试代码:

import spacy
nlp = spacy.load("en_core_web_trf")
corpus = "I see a cat and a dog. None seems to be unhappy. My mother and I wanted to buy a parrot and a tortoise."
doc = nlp(corpus)
nb_and = []
for sent in doc.sents:
    i = 0
    for token in sent:
        if token.text == "and":
            i += 1
    nb_and.append(i)

nb_and
# => [1, 0, 2]

关于nlp - Spacy:计算每个句子中特定标记的出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69831095/

相关文章:

python - NLTK 正则表达式和 CFG

nlp - NLP 中 Stemming 的真正目的是什么?

java - 我的计数器没有增加并且在java中保持为1

python - 如何使用 Spacy 提取标签属性

nlp - 将 TF-IDF 与预训练的 Word 嵌入相结合

nlp - 使用 NLTK 处理复合词(2-grams)

nlp - 用于情感分析的训练数据

encoding - 变压器模型中位置编码的实现细节?

Java:如何在后台记录击键?

python - while 循环不会中断,当计数器不再小于数组长度时应该中断