Why are spaCy dependency symbol definitions 'case' and 'compound' not recognized like 'nsubj' in the spacy.symbols package?

Viewed 579

The Problem:

spaCy dependency symbols 'compound' and 'case' error out as 'undefined' whereas 'nsubj' is recognized even though all three dependency symbols are shown active in the graphical output. Should not the 'from spacy.symbols import *' define all symbols as it does for 'nsubj' and others?

spaCy Documentation

https://spacy.io/api/annotation#dependency-parsing Shows 'case' and 'compound' defined in both the English and Universal label dependency sets.

Environment

Windows 10; python 3.7.1; spaCy 2.3.1; using Anaconda3 environment; installed packages using conda; Running code in Jupyter. All packages installed are listed after the code below.

Code Example

import spacy
from spacy import displacy
from spacy.symbols import *
nlp = spacy.load("en_core_web_sm")      # loaded the small model but also fails with the large model
doc = nlp("Autonomous family cars and people's drones are the future.")
displacy.render(doc, style='dep')       # draw a graph; shows dependencies assigned including 'compound' and 'case'

for t in doc:
    if t.dep == nsubj:                        # dependency 'nsubj' IS recognized
        print(f"Found nsub token")
    if t.dep == compound:                     # dependency 'compound' is NOT recognized
        print(f"Found compound token")
    if t.dep == case:                         # dependency 'case' is NOT recognized
        print(f"Found case token")
1 Answers

You are right, the symbols module does not contain case or compound. You can see all symbols with the below code:

from spacy import symbols
help(symbols)

A workaround for this problem is to store the actual value of each missing dependency into a variable. First lets find the dependency labels for each token and their number:

import spacy
from spacy.symbols import *
nlp = spacy.load("en_core_web_lg")
doc = nlp("Autonomous family cars and people's drones are the future.")

for t in doc:
    print(t, t.dep_, t.dep)

Now that we know the actual values for case and compound, we can create the variables for these symbols.

compound = 7037928807040764755
CASE = 8110129090154140942

And the original code will now work as intended.

for t in doc:
    if t.dep == nsubj:
        print("Found nsub token")
    if t.dep == compound:
        print("Found compound token")
    if t.dep == CASE:
        print("Found case token")
Related