A small spacy UD parsing example

Allikas: Lambda
Redaktsioon seisuga 29. aprill 2025, kell 12:42 kasutajalt Tammet (arutelu | kaastöö) (Uus lehekülg: '<pre> 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: p...')
(erin) ←Vanem redaktsioon | Viimane redaktsiooni (erin) | Uuem redaktsioon→ (erin)
Mine navigeerimisribale Mine otsikasti
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