spaCy ile İleri Düzey NLP
Ines Montani
spaCy core developer
| İstatistiksel modeller | Kural tabanlı sistemler | |
|---|---|---|
| Kullanım alanları | uygulamanın örneklere göre genelleme yapması gerekir | |
| Gerçek dünya örnekleri | ürün adları, kişi adları, özne/nesne ilişkileri | |
| spaCy özellikleri | varlık tanıyıcı, bağımlılık ayrıştırıcı, sözcük türü etiketleyici |
| İstatistiksel modeller | Kural tabanlı sistemler | |
|---|---|---|
| Kullanım alanları | uygulamanın örneklere göre genelleme yapması gerekir | sonlu sayıda örnek içeren sözlük |
| Gerçek dünya örnekleri | ürün adları, kişi adları, özne/nesne ilişkileri | dünya ülkeleri, şehirler, ilaç adları, köpek ırkları |
| spaCy özellikleri | varlık tanıyıcı, bağımlılık ayrıştırıcı, sözcük türü etiketleyici | ayrıştırıcı (tokenizer), Matcher, PhraseMatcher |
# Initialize with the shared vocab from spacy.matcher import Matcher matcher = Matcher(nlp.vocab)# Patterns are lists of dictionaries describing the tokens pattern = [{'LEMMA': 'love', 'POS': 'VERB'}, {'LOWER': 'cats'}] matcher.add('LOVE_CATS', None, pattern)# Operators can specify how often a token should be matched pattern = [{'TEXT': 'very', 'OP': '+'}, {'TEXT': 'happy'}]# Calling matcher on doc returns list of (match_id, start, end) tuples doc = nlp("I love cats and I'm very very happy") matches = matcher(doc)
matcher = Matcher(nlp.vocab) matcher.add('DOG', None, [{'LOWER': 'golden'}, {'LOWER': 'retriever'}]) doc = nlp("I have a Golden Retriever")for match_id, start, end in matcher(doc): span = doc[start:end] print('Matched span:', span.text)# Get the span's root token and root head token print('Root token:', span.root.text) print('Root head token:', span.root.head.text)# Get the previous token and its POS tag print('Previous token:', doc[start - 1].text, doc[start - 1].pos_)
Matched span: Golden RetrieverRoot token: Retriever Root head token: havePrevious token: a DET
PhraseMatcher, normal ifadeler ya da anahtar sözcük aramaya benzer – ama token’lara erişimle!Doc nesneleri alırMatcher’dan daha verimli ve hızlıdırfrom spacy.matcher import PhraseMatcher matcher = PhraseMatcher(nlp.vocab)pattern = nlp("Golden Retriever") matcher.add('DOG', None, pattern) doc = nlp("I have a Golden Retriever")# iterate over the matches for match_id, start, end in matcher(doc): # get the matched span span = doc[start:end] print('Matched span:', span.text)
Matched span: Golden Retriever
spaCy ile İleri Düzey NLP