Henry E
Henry E

Reputation: 350

Dependency parsing of noun chunks in spacy

There's a couple of existing questions about getting noun chunks in spacy, which is relatively straightforward.

What I'm interested in is replicating the dependency parsing on top of pre-specified ngrams inside a sentence. Like in the example below, from this spacy talk, where Alex Smith and East London are treated as a single token in the dependency parse.

enter image description here

Upvotes: 1

Views: 1629

Answers (1)

Petr Matuska
Petr Matuska

Reputation: 573

This was probably done via options parameters where you specify "collapse_phrases" : True

Details at https://spacy.io/api/top-level#options-dep

Example which creates a svg file which you can open in your browser

import spacy
from spacy import displacy
from pathlib import Path

nlp = spacy.load('en_core_web_sm', parse=True, tag=True, entity=True)

doc = nlp("Alex Smith was fatally stabbed in East London")
print(doc.ents)
options = {"color": "white", "collapse_phrases" : True, "bg": "#000000"}
svg = displacy.render(doc, style="dep", options=options)

output_path = Path("dependency_plot.svg")
output_path.open("w", encoding="utf-8").write(svg)

Upvotes: 1

Related Questions