spaCy में extended syntax के साथ Matching
Rule-based information extraction किसी भी NLP पाइपलाइन के लिए ज़रूरी होता है। Matcher क्लास कर्ली ब्रैकेट्स के अंदर कुछ ऑपरेटर की अनुमति देकर पैटर्न को और अभिव्यंजक बनाती है। ये ऑपरेटर extended comparison के लिए होते हैं और Python के in, not in और comparison ऑपरेटर्स जैसे दिखते हैं। इस अभ्यास में, आप spaCy की matching फ़ंक्शनैलिटी, Matcher, का उपयोग करके उदाहरण टेक्स्ट से दिए गए terms के मैच ढूँढने का अभ्यास करेंगे।
Matcher क्लास पहले से spacy.matcher लाइब्रेरी से इम्पोर्ट की गई है। आप इस अभ्यास में उदाहरण टेक्स्ट के Doc कंटेनर को doc कॉल करके उपयोग करेंगे। एक प्री-लोडेड spaCy मॉडल nlp नाम से उपलब्ध है।
यह अभ्यास पाठ्यक्रम का हिस्सा है
spaCy के साथ Natural Language Processing
अभ्यास निर्देश
Matcherऔरnlpका उपयोग करके एक matcher ऑब्जेक्ट परिभाषित करें।INऑपरेटर का उपयोग करके ऐसा पैटर्न बनाएँ जोtiny squaresऔरtiny mouthfulसे मेल खाए।- इस पैटर्न का उपयोग करके
docमें मैच ढूँढें। - मैचों के start और end token indices तथा उनके text span को प्रिंट करें।
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
nlp = spacy.load("en_core_web_sm")
doc = nlp(example_text)
# Define a matcher object
matcher = Matcher(nlp.____)
# Define a pattern to match tiny squares and tiny mouthful
pattern = [{"lower": ____}, {"lower": {____: ["squares", "mouthful"]}}]
# Add the pattern to matcher object and find matches
matcher.____("CustomMatcher", [____])
matches = ____(____)
# Print out start and end token indices and the matched text span per match
for match_id, start, end in matches:
print("Start token: ", ____, " | End token: ", ____, "| Matched text: ", doc[____:____].text)