Удалить B и я помечаю на NER - PullRequest
0 голосов
/ 08 января 2020

У меня есть новостные статьи, я хочу сделать NER, используя дипавлов к этим статьям. Сущность использует схему маркировки BIO. Здесь «B» обозначает начало объекта, «I» обозначает «внутри» и используется для всех слов, составляющих объект, кроме первого, а «O» означает отсутствие объекта. Коды NER выглядят следующим образом:

def listOfTuples(list1, list2): 
    return list(map(lambda x, y:(x,y), list1, list2)) 

ner_result = []
for x in split:
    for y in split[0]:
        news_ner = ner_model([str(y)])
        teks =  news_ner[0][0]
        tag = news_ner[1][0]
        ner_result.extend(listOfTuples(teks, tag))

print([i for i in ner_result if i[1] != 'O'])

Что ж, результаты NER выглядят следующим образом.

[('KOMPAScom', 'B-ORG'), ('Kompascom', 'I-ORG'), ('IFCN', 'B-ORG'), ('-', 'I-ORG'), ('International', 'I-ORG'), ('Fact', 'I-ORG'), ('-', 'I-ORG'), ('Checking', 'I-ORG'), ('Network', 'I-ORG'), ('Kompascom', 'B-ORG'), ('49', 'B-CARDINAL'), ('IFCN', 'B-ORG'), ('Kompascom', 'B-ORG'), ('Redaksi', 'B-ORG'), ('Kompascom', 'I-ORG'), ('Wisnu', 'B-PERSON'), ('Nugroho', 'I-PERSON'), ('Jakarta', 'B-GPE'), ('Rabu', 'B-DATE'), ('17', 'I-DATE'), ('/', 'I-DATE'), ('10', 'I-DATE'), ('/', 'I-DATE'), ('2018', 'I-DATE'), ('KOMPAScom', 'B-ORG'), ('Redaksi', 'B-ORG'), ('Kompascom', 'I-ORG'), ('Wisnu', 'B-PERSON'), ('Nugroho', 'I-PERSON'), ('Kompascom', 'B-ORG'), ('Bentara', 'I-ORG'), ('Budaya', 'I-ORG'), ('Jakarta', 'I-ORG'), ('Palmerah', 'I-ORG')]

Я хочу удалить теги B и I, а затем объединить текст в теги B и I, поэтому вывод идет следующим образом.

[('KOMPAScom Kompascom', 'ORG'), ('IFCN - International Fact - Checking Network', 'ORG'), ('Kompascom', 'ORG'), ('49', 'CARDINAL'), ('IFCN', 'ORG'), ('Kompascom', 'ORG'), ('Redaksi Kompascom', 'ORG'), ('Wisnu Nugroho', 'PERSON'), ('Jakarta', 'GPE'), ('Rabu 17/10/2018', 'DATE'), ('KOMPAScom', 'ORG'), ('Redaksi Kompascom', 'ORG'), ('Wisnu Nugroho', 'PERSON'), ('Kompascom Bentara Budaya Jakarta Palmerah', 'ORG')]

У вас есть идеи?

1 Ответ

1 голос
/ 08 января 2020

Вы можете просто перебирать помеченный текст и соединять токены, принадлежащие одному и тому же объекту. Это не удивительно элегантно, но работает. Как то так:

def collapse(ner_result):
    # List with the result
    collapsed_result = []

    # Buffer for tokens belonging to the most recent entity
    current_entity_tokens = []
    current_entity = None

    # Iterate over the tagged tokens
    for token, tag in ner_result:
        if tag == "O":
            continue
        # If an enitity span starts ...
        if tag.startswith("B-"):
            # ... if we have a previous entity in the buffer, store it in the result list
            if current_entity is not None:
                collapsed_result.append(
                    (" ".join(current_entity_tokens), current_entity))

            current_entity = tag[2:]
            # The new entity has so far only one token
            current_entity_tokens = [token]
        # If the entity continues ...
        elif tag == "I-" + current_entity:
            # Just add the token buffer
            current_entity_tokens.append(token)
        else:
            raise ValueError("Invalid tag order.")

    # The last entity is still in the buffer, so add it to the result
    # ... but only if there were some entity at all
    if current_entity is not None:
        collapsed_result.append(
            (" ".join(current_entity_tokens), current_entity))
    return collapsed_result
...