sten
sten

Reputation: 7476

Custom dependency graph

Is there a way to use Spacy to create custom dependency graph i.e. to manually specify which words are connected.

If not is there other tool to create similar looking dep-diagrams ? with arcs..

if as a bonus can also draw in text mode ..that would be cool.

deps

Upvotes: 0

Views: 217

Answers (1)

polm23
polm23

Reputation: 15593

You can modify the .head or .dep_ attribute on tokens to change the dependency graph, or you can just pass any data to displaCy, as described in the docs. Example:

ex = {
    "words": [
        {"text": "This", "tag": "DT"},
        {"text": "is", "tag": "VBZ"},
        {"text": "a", "tag": "DT"},
        {"text": "sentence", "tag": "NN"}
    ],
    "arcs": [
        {"start": 0, "end": 1, "label": "nsubj", "dir": "left"},
        {"start": 2, "end": 3, "label": "det", "dir": "left"},
        {"start": 1, "end": 3, "label": "attr", "dir": "right"}
    ]
}
html = displacy.render(ex, style="dep", manual=True)

displaCy does not support text output, but there are libraries like deplacy you can use for that. Example output from the README:

I         PRON  <══════════════╗   nsubj
saw       VERB  ═══════════╗═╗═╝═╗ ROOT
a         DET   <════════╗ ║ ║   ║ det
horse     NOUN  ═══════╗═╝<╝ ║   ║ dobj
yesterday NOUN  <══════║═════╝   ║ npadvmod
which     DET   <════╗ ║         ║ nsubj
had       AUX   ═══╗═╝<╝         ║ relcl
no        DET   <╗ ║             ║ det
name      NOUN  ═╝<╝             ║ dobj
.         PUNCT <════════════════╝ punct

Upvotes: 2

Related Questions