Pencocokan berbasis aturan

NLP Lanjutan dengan spaCy

Ines Montani

spaCy core developer

Mengapa tidak pakai regular expression?

  • Cocok pada objek Doc, bukan hanya string
  • Cocok pada token dan atribut token
  • Gunakan prediksi model
  • Contoh: "duck" (verba) vs. "duck" (nomina)
NLP Lanjutan dengan spaCy

Pola kecocokan

  • Daftar dictionary, satu per token

  • Cocok teks token persis

    [{'ORTH': 'iPhone'}, {'ORTH': 'X'}]
    
  • Cocok atribut leksikal

    [{'LOWER': 'iphone'}, {'LOWER': 'x'}]
    
  • Cocok atribut token apa pun

    [{'LEMMA': 'buy'}, {'POS': 'NOUN'}]
    
NLP Lanjutan dengan spaCy

Menggunakan Matcher (1)

import spacy

# Import the Matcher
from spacy.matcher import Matcher

# Load a model and create the nlp object nlp = spacy.load('en_core_web_sm')
# Initialize the matcher with the shared vocab matcher = Matcher(nlp.vocab)
# Add the pattern to the matcher pattern = [{'ORTH': 'iPhone'}, {'ORTH': 'X'}] matcher.add('IPHONE_PATTERN', None, pattern)
# Process some text doc = nlp("New iPhone X release date leaked")
# Call the matcher on the doc matches = matcher(doc)
NLP Lanjutan dengan spaCy

Menggunakan Matcher (2)

# Call the matcher on the doc
doc = nlp("New iPhone X release date leaked")
matches = matcher(doc)

# Iterate over the matches for match_id, start, end in matches:
# Get the matched span matched_span = doc[start:end] print(matched_span.text)
iPhone X
  • match_id: nilai hash nama pola
  • start: indeks awal span yang cocok
  • end: indeks akhir span yang cocok
NLP Lanjutan dengan spaCy

Mencocokkan atribut leksikal

pattern = [
    {'IS_DIGIT': True},
    {'LOWER': 'fifa'},
    {'LOWER': 'world'},
    {'LOWER': 'cup'},
    {'IS_PUNCT': True}
]
doc = nlp("2018 FIFA World Cup: France won!")
2018 FIFA World Cup:
NLP Lanjutan dengan spaCy

Mencocokkan atribut token lain

pattern = [
    {'LEMMA': 'love', 'POS': 'VERB'},
    {'POS': 'NOUN'}
]
doc = nlp("I loved dogs but now I love cats more.")
loved dogs
love cats
NLP Lanjutan dengan spaCy

Menggunakan operator dan kuantifier (1)

pattern = [
    {'LEMMA': 'buy'},
    {'POS': 'DET', 'OP': '?'},  # opsional: cocok 0 atau 1 kali
    {'POS': 'NOUN'}
]
doc = nlp("I bought a smartphone. Now I'm buying apps.")
bought a smartphone
buying apps
NLP Lanjutan dengan spaCy

Menggunakan operator dan kuantifier (2)

Deskripsi
{'OP': '!'} Negasi: cocok 0 kali
{'OP': '?'} Opsional: cocok 0 atau 1 kali
{'OP': '+'} Cocok ≥1 kali
{'OP': '*'} Cocok 0 atau lebih kali
NLP Lanjutan dengan spaCy

Ayo berlatih!

NLP Lanjutan dengan spaCy

Preparing Video For Download...