1

我正在使用 Spacy 提取与格和直接对象。Noun.chunks 已经对其根源进行了依赖标记,例如dativeand dobj,我想做的是获取Span并将其保存为 Doc 以应用进一步分析。

我有以下代码:

import spacy
nlp = spacy.load("en_core_web_lg")
doc = nlp(open("/-textfile").read())

到目前为止一切顺利,接下来我得到了 Span 对象;

datives = []

for dat in doc.noun_chunks:
    if dat.root.dep_ == "dative" and dat.root.head.pos_ == "VERB":
            dative.append(dat.sent)

现在我有了所有的noun.chunks词根是与格而 head 是VERB

但是,我想tokendatives []

dativesent = datives.as_doc()

但问题是datives []已经是一个列表,我无法将其转换为DOC.

我怎样才能用dative-noun.chunks将句子保存为DOC?

4

1 回答 1

1

Span您可以像 a 一样遍历一个句子(即 a )Doc来访问标记:

import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("She gave the dog a bone. He read a book. They gave her a book.")

dative_sents = []
for nc in doc.noun_chunks:
    if nc.root.dep_ == "dative" and nc.root.head.pos_ == "VERB":
        dative_sents.append(nc.sent)

for dative_sent in dative_sents:
    print("Sentence with dative:", dative_sent.text)
    for token in dative_sent:
        print(token.text, token.pos_, token.dep_)
    print()

输出:

Sentence with dative: She gave the dog a bone.
She PRON nsubj
gave VERB ROOT
the DET det
dog NOUN dative
a DET det
bone NOUN dobj
. PUNCT punct

Sentence with dative: They gave her a book.
They PRON nsubj
gave VERB ROOT
her PRON dative
a DET det
book NOUN dobj
. PUNCT punct
于 2020-04-09T08:00:48.850 回答