spaCy Matcher और PhraseMatcher

spaCy के साथ Natural Language Processing

Azadeh Mobasher

Principal Data Scientist

spaCy में Matcher

 

  • RegEx पैटर्न जटिल, पढ़ने और डिबग करने में कठिन हो सकते हैं.
  • spaCy एक पढ़ने योग्य और प्रोडक्शन-रेडी विकल्प देता है: Matcher क्लास.

 

import spacy
from spacy.matcher import Matcher

nlp = spacy.load("en_core_web_sm") doc = nlp("Good morning, this is our first day on campus.")
matcher = Matcher(nlp.vocab)
spaCy के साथ Natural Language Processing

spaCy में Matcher

 

  • मैच का आउटपुट मिले पैटर्न के start और end टोकन इंडेक्स शामिल करता है.
pattern = [{"LOWER": "good"}, {"LOWER": "morning"}]

matcher.add("morning_greeting", [pattern])
matches = matcher(doc) for match_id, start, end in matches: print("Start token: ", start, " | End token: ", end, "| Matched text: ", doc[start:end].text)
>>> Start token:  0  | End token:  2 | Matched text:  Good morning
spaCy के साथ Natural Language Processing

Matcher की extended syntax सपोर्ट

 

  • मैचिंग पैटर्न तय करते समय ऑपरेटर इस्तेमाल कर सकते हैं.
  • Python के in, not in और comparison ऑपरेटर जैसे ही ऑपरेटर.

 

Attribute Value type Description
IN any type एट्रिब्यूट वैल्यू लिस्ट का मेंबर है
NOT_IN any type एट्रिब्यूट वैल्यू लिस्ट का मेंबर नहीं है
==, >=, <=, >, < int, float बराबरी या असमानता जाँचने के लिए comparison ऑपरेटर
spaCy के साथ Natural Language Processing

Matcher की extended syntax सपोर्ट

  • IN ऑपरेटर से good morning और good evening दोनों मैच करें
doc = nlp("Good morning and good evening.")
matcher = Matcher(nlp.vocab)
pattern = [{"LOWER": "good"}, {"LOWER": {"IN": ["morning", "evening"]}}]
matcher.add("morning_greeting", [pattern])
matches = matcher(doc)
  • IN ऑपरेटर से मैचिंग का आउटपुट
for match_id, start, end in matches:
    print("Start token: ", start, " | End token: ", end,
          "| Matched text: ", doc[start:end].text)
>>> Start token:  0  | End token:  2 | Matched text:  Good morning
Start token:  3  | End token:  5 | Matched text:  good evening
spaCy के साथ Natural Language Processing

spaCy में PhraseMatcher

 

  • PhraseMatcher क्लास दिए गए टेक्स्ट में लंबे phrase की लिस्ट मैच करता है.

 

from spacy.matcher import PhraseMatcher
nlp = spacy.load("en_core_web_sm")
matcher = PhraseMatcher(nlp.vocab)
terms = ["Bill Gates", "John Smith"]
spaCy के साथ Natural Language Processing

spaCy में PhraseMatcher

  • PhraseMatcher का आउटपुट मिले पैटर्न के start और end टोकन इंडेक्स देता है
patterns = [nlp.make_doc(term) for term in terms]
matcher.add("PeopleOfInterest", patterns)

doc = nlp("Bill Gates met John Smith for an important discussion regarding importance of AI.")
matches = matcher(doc) for match_id, start, end in matches: print("Start token: ", start, " | End token: ", end, "| Matched text: ", doc[start:end].text)
>>> Start token:  0  | End token:  2 | Matched text:  Bill Gates
Start token:  3  | End token:  5 | Matched text:  John Smith
spaCy के साथ Natural Language Processing

spaCy में PhraseMatcher

  • हम PhraseMatcher क्लास का attr आर्ग्युमेंट इस्तेमाल कर सकते हैं
matcher = PhraseMatcher(nlp.vocab, attr = "LOWER")

terms = ["Government", "Investment"] patterns = [nlp.make_doc(term) for term in terms] matcher.add("InvestmentTerms", patterns) doc = nlp("It was interesting to the investment division of the government.")
matcher = PhraseMatcher(nlp.vocab, attr = "SHAPE")

terms = ["110.0.0.0", "101.243.0.0"] patterns = [nlp.make_doc(term) for term in terms] matcher.add("IPAddresses", patterns) doc = nlp("The tracked IP address was 234.135.0.0.")
spaCy के साथ Natural Language Processing

अभ्यास करते हैं!

spaCy के साथ Natural Language Processing

Preparing Video For Download...