spaCy में एक कस्टम named entity बनाना
यदि spaCy की built-in named entities पर्याप्त नहीं हैं, तो आप spaCy के entity_ruler पाइपलाइन component का उपयोग करके अपनी खुद की entities बना सकते हैं।
entity_ruler component आपको अपनी entities बनाने और उन्हें spaCy पाइपलाइन में जोड़ने देता है।
आप शुरुआत में nlp.add_pipe() से इस component को मौजूदा पाइपलाइन में जोड़ेंगे, और इसे "ner" component से before रखें ताकि आपके अपने rules पहले लागू हों। add_pipe() वही component लौटाता है जिसे उसने जोड़ा है।
इसके बाद आप component पर add_patterns() कॉल कर सकते हैं और उसे उस टेक्स्ट pattern की dictionary पास कर सकते हैं जिसे आप किसी entity के साथ लेबल करना चाहते हैं।
क्योंकि Acme एक टेक्नोलॉजी कंपनी है, आप "smartphone" pattern को "PRODUCT" entity टैग से टैग करने का निर्णय लेते हैं।
spaCy इम्पोर्ट किया जा चुका है और nlp में लोडेड en_core_web_sm मॉडल है। चूँकि पाइपलाइन बदलने का पहले से बने docs पर कोई प्रभाव नहीं पड़ता, नए component के लगने के बाद आप call_4_channel_2.wav file के ट्रांसक्राइब्ड टेक्स्ट से doc को फिर से बनाएँगे।
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Spoken Language Processing
अभ्यास निर्देश
"entity_ruler"component कोnlpपाइपलाइन में"ner"component से पहले जोड़ें और उसेrulerमें सेव करें।"pattern"key के लिए मान के रूप में"smartphone"जोड़ें।docमें मौजूद entity attributes प्रिंट करें।
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Add an entity_ruler component to the pipeline, before ner
ruler = nlp.add_pipe(____, before=____)
# Define pattern for new entity
ruler.add_patterns([{"label": "PRODUCT", "pattern": ____}])
# Recreate the doc so it uses the updated pipeline
doc = nlp(call_4_channel_2_text)
# Test new entity
for entity in doc.____:
print(entity.text, entity.label_)