Skip to content Skip to sidebar Skip to footer

Repeating Entity In Replacing Entity With Their Entity Label Using Spacy

Code: 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 e

Solution 1:

Lets try:

import spacy
from spacy.gold import biluo_tags_from_offsets, spans_from_biluo_tags
nlp = spacy.load("en_core_web_md")

#read txt file, each string on its own linewithopen("./try.txt","r") as f:
    texts = f.read().splitlines()

docs = nlp.pipe(texts)
out_text = ""for doc in docs:
    offsets = []
    for ent in doc.ents:
        offsets.append((ent.start_char, ent.end_char, ent.label_))
    tags = biluo_tags_from_offsets(doc, offsets)
    text = *zip([tok for tok in doc],tags),
    out = []
    for item in text:
        tag = item[1].split("-")
        if tag[0] == "O":
            out.append(item[0].text+item[0].whitespace_)
        if tag[0] == "U":
            out.append(item[0].ent_type_+item[0].whitespace_)
        elif tag[0] == "L":
            out.append(item[0].ent_type_+item[0].whitespace_)
    out_text += "".join(out)+"\n"withopen("out_try.txt","w") as f:
    f.write(out_text)

Contents of the output file:

GPE recently became the ORDINAL GPE state to "ban NORP culture.
His friend PERSON is here with PERSON and PERSON.
ORG is looking at buying GPE startup for MONEY

Post a Comment for "Repeating Entity In Replacing Entity With Their Entity Label Using Spacy"