Ersetzen Sie die Entität durch ihre Entitätsbezeichnung mit spacy
Ich möchte für meine Daten tun, indem ich jede Entität mit Spacy durch ihre Beschriftung ersetze. Ich habe 3000 Textzeilen, die benötigt werden, um Entitäten durch ihre Beschriftungsentität zu ersetzen.
beispielsweise:
"Georgia war kürzlich der erste US-Bundesstaat, der" die muslimische Kultur verbot ".
Und wollen so werden:
"GPE wurde kürzlich zum ORDINAL GPE-Staat, um die NORP-Kultur zu verbieten." ""
Ich möchte, dass Code mehr als nur Textzeilen ersetzt.
Vielen Dank.
Zum Beispiel diese Codes, aber für einen Satz möchte ich s (Zeichenfolge) so ändern, dass die Spalte 3000 Zeilen enthält
Erster: von ( Ersetzen Sie die Entität durch ihr Label in 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.
Zweiter: from ( Zusammenführen von Tags in meine Datei mithilfe der Annotation für benannte Entitäten )
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>
Antworten
IIUC, Sie können erreichen, was Sie wollen mit:
- Lesen Sie Ihre Texte aus einer Datei, wobei jeder Text in einer eigenen Zeile steht
- Verarbeiten von Ergebnissen durch Ersetzen von Tags durch Entitäten, falls vorhanden
- Schreiben Sie die Ergebnisse auf die Disc, wobei jeder Text in einer eigenen Zeile steht
Demo:
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))
Inhalt der Eingabedatei:
Georgia war kürzlich der erste US-Bundesstaat, der "die muslimische Kultur verbot".
Sein Freund Nicolas J. Smith ist hier mit Bart Simpon und Fred.
Apple beabsichtigt, ein britisches Startup für 1 Milliarde US-Dollar zu kaufen
Inhalt der Ausgabedatei:
GPE wurde kürzlich zum ORDINAL GPE-Staat, um "die NORP-Kultur zu verbieten.
Sein Freund PERSON PERSON PERSON ist hier mit PERSON PERSON und PERSON.
ORG erwägt den Kauf eines GPE-Startups für MONEYMONEY MONEY
Beachten Sie das MONEYMONEY
Muster.
Das ist weil:
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=''