Kural Tabanlı Eşleme

spaCy ile İleri Düzey NLP

Ines Montani

spaCy core developer

Neden sadece düzenli ifadeler değil?

  • Sadece dizgilerle değil, Doc nesneleriyle eşleştirin
  • Tokenlar ve token özniteliklerini eşleştirin
  • Modelin tahminlerini kullanın
  • Örnek: "duck" (fiil) vs. "duck" (isim)
spaCy ile İleri Düzey NLP

Eşleme kalıpları

  • Sözlük listeleri, her token için biri

  • Tam token metinlerini eşleştirin

    [{'ORTH': 'iPhone'}, {'ORTH': 'X'}]
    
  • Sözcüksel öznitelikleri eşleştirin

    [{'LOWER': 'iphone'}, {'LOWER': 'x'}]
    
  • Herhangi bir token özniteliğini eşleştirin

    [{'LEMMA': 'buy'}, {'POS': 'NOUN'}]
    
spaCy ile İleri Düzey NLP

Matcher kullanımı (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)
spaCy ile İleri Düzey NLP

Matcher kullanımı (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: kural adı için karma değeri
  • start: eşleşen aralığın başlangıç indeksi
  • end: eşleşen aralığın bitiş indeksi
spaCy ile İleri Düzey NLP

Sözcüksel öznitelikleri eşleme

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:
spaCy ile İleri Düzey NLP

Diğer token özniteliklerini eşleme

pattern = [
    {'LEMMA': 'love', 'POS': 'VERB'},
    {'POS': 'NOUN'}
]
doc = nlp("I loved dogs but now I love cats more.")
loved dogs
love cats
spaCy ile İleri Düzey NLP

Operatörler ve niceleyiciler kullanma (1)

pattern = [
    {'LEMMA': 'buy'},
    {'POS': 'DET', 'OP': '?'},  # optional: match 0 or 1 times
    {'POS': 'NOUN'}
]
doc = nlp("I bought a smartphone. Now I'm buying apps.")
bought a smartphone
buying apps
spaCy ile İleri Düzey NLP

Operatörler ve niceleyiciler kullanma (2)

Açıklama
{'OP': '!'} Olumsuz: 0 kez eşleştir
{'OP': '?'} İsteğe bağlı: 0 veya 1 kez
{'OP': '+'} 1 veya daha fazla kez
{'OP': '*'} 0 veya daha fazla kez
spaCy ile İleri Düzey NLP

Haydi pratik yapalım!

spaCy ile İleri Düzey NLP

Preparing Video For Download...