रिप्लेसमेंट एंटिटी स्पैस का उपयोग करके अपने एंटिटी लेबल से
मैं अपने डेटा के लिए स्पैस का उपयोग करते हुए इसके लेबल के साथ प्रत्येक इकाई को प्रतिस्थापित करना चाहता हूं और मेरे पास लेबल लेबल के साथ संस्थाओं को बदलने के लिए 3000 पाठ पंक्तियां हैं,
उदाहरण के लिए:
"जॉर्जिया हाल ही में मुस्लिम संस्कृति पर प्रतिबंध लगाने वाला पहला अमेरिकी राज्य बना।"
और इस तरह बनना चाहते हैं:
"GPE हाल ही में NORP संस्कृति पर प्रतिबंध लगाने के लिए ORDINAL GPE राज्य बन गया है।" "
मुझे पाठ की पंक्तियों से अधिक कोड बदलने की आवश्यकता है।
बहुत बहुत धन्यवाद।
उदाहरण के लिए ये कोड लेकिन एक वाक्य के लिए, मैं s (स्ट्रिंग) को कॉलम में संशोधित करना चाहता हूं जिसमें 3000 पंक्तियाँ हैं
पहला एक: से ( SpaCy में इसके लेबल के साथ इकाई बदलें )
s= "His friend Nicolas J. Smith is here with Bart Simpon and Fred."
doc = nlp(s)
newString = s
for e in reversed(doc.ents): #reversed to not modify the offsets of other entities when substituting
start = e.start_char
end = start + len(e.text)
newString = newString[:start] + e.label_ + newString[end:]
print(newString)
#His friend PERSON is here with PERSON and PERSON.
दूसरा एक: ( मेरी एंटल एनोटेशन नाम का उपयोग करके मेरी फ़ाइल में टैग्स को जोड़ना )
import spacy
nlp = spacy.load("en_core_web_sm")
s ="Apple is looking at buying U.K. startup for $1 billion" doc = nlp(s) def replaceSubstring(s, replacement, position, length_of_replaced): s = s[:position] + replacement + s[position+length_of_replaced:] return(s) for ent in reversed(doc.ents): #print(ent.text, ent.start_char, ent.end_char, ent.label_) replacement = "<{}>{}</{}>".format(ent.label_,ent.text, ent.label_) position = ent.start_char length_of_replaced = ent.end_char - ent.start_char s = replaceSubstring(s, replacement, position, length_of_replaced) print(s) #<ORG>Apple</ORG> is looking at buying <GPE>U.K.</GPE> startup for <MONEY>$1 billion</MONEY>
जवाब
IIUC, आप जो चाहें हासिल कर सकते हैं:
- अपने पाठ को फ़ाइल से पढ़ना, प्रत्येक पाठ की अपनी पंक्ति
- अपने टैग के साथ, यदि कोई हो, संस्थाओं को प्रतिस्थापित करके परिणाम की प्रक्रिया
- डिस्क पर परिणाम लिखना, प्रत्येक पाठ की अपनी पंक्ति
डेमो:
import spacy
nlp = spacy.load("en_core_web_md")
#read txt file, each string on its own line
with open("./try.txt","r") as f:
texts = f.read().splitlines()
#substitute entities with their TAGS
docs = nlp.pipe(texts)
out = []
for doc in docs:
out_ = ""
for tok in doc:
text = tok.text
if tok.ent_type_:
text = tok.ent_type_
out_ += text + tok.whitespace_
out.append(out_)
# write to file
with open("./out_try.txt","w") as f:
f.write("\n".join(out))
इनपुट फ़ाइल की सामग्री:
जॉर्जिया हाल ही में मुस्लिम संस्कृति पर प्रतिबंध लगाने वाला पहला अमेरिकी राज्य बन गया है।
उनके मित्र निकोलस जे। स्मिथ यहां बार्ट सिम्पोन और फ्रेड के साथ हैं।
एप्पल $ 1 बिलियन के लिए यूके स्टार्टअप खरीदने की ओर देख रहा है।
आउटपुट फ़ाइल की सामग्री:
GPE हाल ही में NORP संस्कृति पर प्रतिबंध लगाने के लिए ORDINAL GPE राज्य बन गया है।
उसका मित्र PERSON PERSON PERSON यहाँ PERSON PERSON और PERSON के साथ है।
ORG MONEYONONEY MONEY के लिए GPE स्टार्टअप खरीदने की कोशिश कर रहा है।
MONEYMONEY
पैटर्न पर ध्यान दें ।
यह है क्योंकि:
doc = nlp("Apple is looking at buying U.K. startup for $1 billion")
for tok in doc:
print(f"{tok.text}, {tok.ent_type_}, whitespace='{tok.whitespace_}'")
Apple, ORG, whitespace=' '
is, , whitespace=' '
looking, , whitespace=' '
at, , whitespace=' '
buying, , whitespace=' '
U.K., GPE, whitespace=' '
startup, , whitespace=' '
for, , whitespace=' '
$, MONEY, whitespace='' # <-- no whitespace between $ and 1
1, MONEY, whitespace=' '
billion, MONEY, whitespace=''