Struktur Data: Doc, Span, dan Token

NLP Lanjutan dengan spaCy

Ines Montani

spaCy core developer

Objek Doc

# Create an nlp object
from spacy.lang.en import English
nlp = English()

# Import the Doc class from spacy.tokens import Doc
# The words and spaces to create the doc from words = ['Hello', 'world', '!'] spaces = [True, False, False]
# Create a doc manually doc = Doc(nlp.vocab, words=words, spaces=spaces)
NLP Lanjutan dengan spaCy

Objek Span (1)

Ilustrasi objek Span di dalam Doc dengan indeks token

NLP Lanjutan dengan spaCy

Objek Span (2)

# Import the Doc and Span classes
from spacy.tokens import Doc, Span

# The words and spaces to create the doc from
words = ['Hello', 'world', '!']
spaces = [True, False, False]

# Create a doc manually
doc = Doc(nlp.vocab, words=words, spaces=spaces)

# Create a span manually span = Span(doc, 0, 2)
# Create a span with a label span_with_label = Span(doc, 0, 2, label="GREETING")
# Add span to the doc.ents doc.ents = [span_with_label]
NLP Lanjutan dengan spaCy

Praktik terbaik

  • Doc dan Span sangat kuat dan menyimpan referensi serta relasi kata dan kalimat
    • Ubah hasil ke string sesedikit mungkin
    • Gunakan atribut token jika ada – misalnya, token.i untuk indeks token
  • Jangan lupa meneruskan vocab bersama
NLP Lanjutan dengan spaCy

Ayo berlatih!

NLP Lanjutan dengan spaCy

Preparing Video For Download...