【问题标题】:POS count of first word in sentences using SPACY使用 SPACY 的句子中第一个单词的 POS 计数
【发布时间】:2020-05-16 02:16:05
【问题描述】:

目的是检查句子第一个词的词性,并使用 Spacy 返回它的词性。

目前的努力:

import spacy
from collections import Counter
nlp = spacy.load("en_core_web_sm")
doc = nlp("The cat slept well. Dog is good.")
for sent in doc.sents:
    for token in sent:
        if token.i == 0:
            c = Counter(([token.pos_ for token in sent for sent in doc.sents]))
            print (c)

输出:

Counter({'DET': 2, 'NOUN': 2, 'VERB': 2, 'ADV': 2, 'PUNCT': 2})

考虑到 'the' 的 POS 是 DET 而 'Dog' 是 PROPN

期望的输出:

    DET: 1
    PROPN: 1

【问题讨论】:

    标签: python spacy


    【解决方案1】:

    您的if 条件仅针对第一句的第一个标记计算为TrueCounter 计算第一句话的标记的 POS 标签两次(即句子的数量)。这就是为什么您的Counter 为所有标签输出值2

    这是你想做的代码:

    from collections import Counter
    import spacy
    
    nlp = spacy.load("en_core_web_sm")
    doc = nlp("The cat slept well. Dog is good.")
    c = Counter((sent[0].pos_ for sent in doc.sents))
    print(c)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      • 2023-03-30
      • 1970-01-01
      • 2015-01-03
      • 1970-01-01
      • 2022-01-13
      相关资源
      最近更新 更多