Tokenizing words in German

Viewed 289

I am trying to determine the subject in a German sentence. For English I used to do:

import spacy
nlp = spacy.load('en')
sent = "I shot an elephant"
doc=nlp(sent)

sub_toks = [tok for tok in doc if (tok.dep_ == "nsubj") ]

print(sub_toks) 

But it doesn't work for nlp = spacy.load('de_core_news_sm') and using a German sentence. It returns an empty list.

I tried looking here, even if they have parts of speech instead of subject, object etc. But it returns empty lists too. Is this even possible in German?

1 Answers

try this snippet:

sentences inside the module spacy gives you examples for sentences in german

import spacy
from spacy.lang.de.examples import sentences 

nlp = spacy.load("de_core_news_sm")
doc = nlp(sentences[0])
print(doc.text)
for token in doc:
    print(token.text, token.pos_, token.dep_)

See https://spacy.io/models/de

Related