【发布时间】:2019-08-26 13:24:44
【问题描述】:
我正在使用 spacy 来解析文档,不幸的是,我无法按照我期望的方式处理名词块。以下是我的代码:
# Import spacy
import spacy
nlp = spacy.load("en_core_web_lg")
# Add noun chunking to the pipeline
merge_noun_chunks = nlp.create_pipe("merge_noun_chunks")
nlp.add_pipe(merge_noun_chunks)
# Process the document
docs = nlp.pipe(["The big dogs chased the fast cat"])
# Print out the tokens
for doc in docs:
for token in doc:
print("text: {}, lemma: {}, pos: {}, tag: {}, dep: {}".format(tname, token.text, token.lemma_, token.pos_, token.tag_, token.dep_))
我得到的输出如下:
text: The big dogs, lemma: the, pos: NOUN, tag: NNS, dep: nsubj
text: chased, lemma: chase, pos: VERB, tag: VBD, dep: ROOT
text: the fast cat, lemma: the, pos: NOUN, tag: NN, dep: dobj
问题出在输出的第一行,其中“the big dogs”以一种意想不到的方式被解析:它创建了一个“the”的“引理”,并指出它是“NOUN”的一个“pos”, “NNS”的“标签”和“nsubj”的“dep”。
我希望得到的输出如下:
text: The big dogs, lemma: the big dog, pos: NOUN, tag: NNS, dep: nsubj
text: chased, lemma: chase, pos: VERB, tag: VBD, dep: ROOT
text: the fast cat, lemma: the fast cat, pos: NOUN, tag: NN, dep: dobj
我预计“引理”将是短语“the big dog”,复数形式变为单数,短语将是“名词”的“pos”,“NNS”的“tag”和“dep” ”的“nsubj”。
这是正确的行为,还是我错误地使用了 spacy?如果我错误地使用 spacy,请告诉我执行此任务的正确方式。
【问题讨论】: