mirror of
https://github.com/explosion/spaCy.git
synced 2025-10-24 20:51:30 +03:00
The function `dependency_labels_to_root(token)` defined in section *Get syntactic dependencies* does not terminate. Here is a complete example:
import spacy
nlp = spacy.load('en')
doc = nlp("Apple and banana are similar. Pasta and hippo aren't.")
def dependency_labels_to_root(token):
"""Walk up the syntactic tree, collecting the arc labels."""
dep_labels = []
while token.head is not token:
dep_labels.append(token.dep)
token = token.head
return dep_labels
dep_labels = dependency_labels_to_root(doc[1])
dep_labels
Replacing `is not` with `!=` solves the issue:
import spacy
nlp = spacy.load('en')
doc = nlp("Apple and banana are similar. Pasta and hippo aren't.")
def dependency_labels_to_root(token):
"""Walk up the syntactic tree, collecting the arc labels."""
dep_labels = []
while token.head != token:
dep_labels.append(token.dep)
token = token.head
return dep_labels
dep_labels = dependency_labels_to_root(doc[1])
dep_labels
The output is
['cc', 'nsubj']
|
||
|---|---|---|
| .. | ||
| _architecture.jade | ||
| _community-faq.jade | ||
| _language-data.jade | ||
| _lightning-tour.jade | ||
| _named-entities.jade | ||
| _pipelines.jade | ||
| _pos-deps.jade | ||
| _serialization.jade | ||
| _similarity.jade | ||
| _tokenization.jade | ||
| _training.jade | ||
| _vocab.jade | ||
| _word-vectors.jade | ||