Spacy kullanan varlık etiketleri ile ikame varlık

Dec 17 2020

Verilerim için, Spacy kullanarak her bir varlığı etiketiyle değiştirerek yapmak istiyorum ve varlıkları etiket varlıkları ile değiştirmek için gereken 3000 metin satırım var,

Örneğin:

"Gürcistan son zamanlarda" Müslüman kültürünü yasaklayan "ilk ABD devleti oldu.

Ve böyle olmak istiyorum:

"GPE kısa süre önce NORP kültürünü yasaklayan SIRADAN GPE durumu oldu". "

Kodun metin satırlarından daha fazlasını değiştirmesini istiyorum.

Çok teşekkürler.

Örneğin bu kodlar ancak bir cümle için, 3000 satır içeren sütunu (string) değiştirmek istiyorum

İlki: from ( Varlığı SpaCy'deki etiketiyle değiştirin )

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.

İkincisi: from ( Adlandırılmış varlık ek açıklamasını kullanarak etiketleri dosyama birleştirme )

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>

Yanıtlar

1 SergeyBushmanov Dec 17 2020 at 13:45

IIUC, aşağıdakilerle istediğinizi başarabilirsiniz:

  1. Metinlerinizi dosyadan, her metin kendi satırında okumak
  2. Varlıkları varsa etiketleri ile değiştirerek sonuçları işleme
  3. Sonuçların diske yazılması, her metin kendi satırında

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))

Girdi dosyasının içeriği:

Gürcistan kısa süre önce "Müslüman kültürünü yasaklayan ilk ABD eyaleti oldu.
Arkadaşı Nicolas J. Smith, Bart Simpon ve Fred ile burada.
Apple, 1 milyar dolara İngiltere'deki startup'ı satın almak istiyor

Çıktı dosyasının içeriği:

GPE kısa süre önce NORP kültürünü yasaklayan SIRADAN GPE durumu oldu.
Arkadaşı PERSON PERSON PERSON, PERSON PERSON ve PERSON ile burada.
ORG, MONEYMONEY MONEY için GPE startup'ı satın almayı düşünüyor

Desene dikkat edin MONEYMONEY.

Bunun nedeni ise:

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=''