A small spacy UD parsing example

Allikas: Lambda
import spacy

nlp = spacy.load('en_core_web_sm')

sentence = "Apple's CEO Tim Cook visited the company's headquarters in Cupertino."
doc = nlp(sentence)
for token in doc: print(f"""\nTOKEN: {str(token)}\n=====\nTAG: {str(token.tag_):10} POS: {token.pos_}\nEXPLANATION: {spacy.explain(token.tag_)}""")


piano_text = "Gus is learning piano"
piano_doc = nlp(piano_text)
for token in piano_doc: 
  print(
    f"""
    TOKEN: {token.text}
    =====
    {token.tag_ = }
    {token.head.text = }
    {token.dep_ = }"""
    )

from spacy import displacy
displacy.serve(piano_doc, style="dep")
# see http://127.0.0.1:5000