spaCy में PhraseMatcher
जब आप unstructured टेक्स्ट प्रोसेस करते हैं, तो अक्सर आपके पास लंबी सूचियाँ और डिक्शनरीज़ होती हैं जिन्हें आप दिए गए टेक्स्ट में स्कैन करके मैच करना चाहते हैं. Matcher के patterns हाथ से बनाए जाते हैं और हर token को अलग से कोड करना पड़ता है. अगर आपके पास वाक्यांशों (phrases) की लंबी सूची है, तो Matcher अब सबसे अच्छा विकल्प नहीं रहता. ऐसी स्थिति में, PhraseMatcher क्लास हमें लंबी डिक्शनरीज़ को मैच करने में मदद करती है. इस अभ्यास में, आप PhraseMatcher क्लास का उपयोग करके कई terms के साथ matching shapes वाले patterns निकालने का अभ्यास करेंगे.
en_core_web_sm मॉडल पहले से लोड है और nlp के रूप में उपयोग के लिए तैयार है. PhraseMatcher क्लास इम्पोर्ट की गई है. एक text स्ट्रिंग और terms की एक सूची आपके उपयोग के लिए उपलब्ध है.
यह अभ्यास पाठ्यक्रम का हिस्सा है
spaCy के साथ Natural Language Processing
अभ्यास निर्देश
- दिए गए
termsके shape से मैच कराने के लिएattrसहितPhraseMatcherक्लास को initialize करें. PhraseMatcherऑब्जेक्ट में जोड़ने के लिएpatternsबनाएँ.- दिए गए patterns के मैच ढूँढें और
textमें उनके start और end token indices तथा matching सेक्शन को प्रिंट करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
text = "There are only a few acceptable IP addresse: (1) 127.100.0.1, (2) 123.4.1.0."
terms = ["110.0.0.0", "101.243.0.0"]
# Initialize a PhraseMatcher class to match to shapes of given terms
matcher = ____(nlp.____, attr = ____)
# Create patterns to add to the PhraseMatcher object
patterns = [nlp.make_doc(____) for term in terms]
matcher.____("IPAddresses", patterns)
# Find matches to the given patterns and print start and end characters and matches texts
doc = ____
matches = ____
for match_id, start, end in matches:
print("Start token: ", ____, " | End token: ", ____, "| Matched text: ", doc[____:____].text)