Поиск связанных сущностей из трех списков - PullRequest
2 голосов
/ 06 марта 2019

У меня есть три списка, которые содержат следующие данные:

Entities:  ['Ashraf', 'Afghanistan', 'Afghanistan', 'Kabul']
Relations:  ['Born', 'President', 'employee', 'Capital', 'Located', 'Lecturer', 'University']
sentence_list: ['Ashraf','Born', 'in', 'Kabul', '.' 'Ashraf', 'is', 'the', 'president', 'of', 'Afghanistan', '.', ...]

Поскольку sentence_list - это список предложений. В каждом предложении я хочу проверить, если какое-либо слово из Entities и Relations, комбинация конкретных слов должна быть добавлена ​​в другой список. например, (Ashraf, born, Kabul) в первом предложении.

Что я сделал:

Первое неполное решение:

# read file
with open('../data/parse.txt', 'r') as myfile:
    json_data = json.load(myfile)

for i in range(len(json_data)): # the dataset was in json format
     if json_data[i]['word'] in relation(json_data)[0]: # I extract the relations
         print(json_data[i]['word'])
     if json_data[i]['word'] in entities(json_data)[0]:
         print(json[i]['word'])

Вывод: (Ashraf, Born, Ashraf), где я хочу (Ashraf, Born, Kabul)

Следующее неполное решение: я сохранил json_data в списке, а затем сделал это:

json_data2 = []
for i in range(len(json_data)):
    json2_data.append(json_data[i]['word'])
print(json_data2)


'''
Now I tried if I can find any element of `Entities` list and `Relations` list
in each sentence of `sentence_list`. And then it should store matched 
entities and relations based on sentence to a list. '''

for line in json_data2:
    for rel in relation(obj):
        for ent in entities(obj):
            match = re.findall(rel,  line['word'])
            if match:
                print('word matched relations: %s ==> word: %s' % (rel,  line['address']))
            match2 = re.findall(ent, line['word'])
            if match2:
                print('word matched entities: %s ==> word: %s' % (ent,  line['address']))

К сожалению, не работает?

1 Ответ

1 голос
/ 06 марта 2019

Вы можете использовать следующее понимание списка :

to_match = set(Entities+Relations)
l = [{j for j in to_match if j in i} 
        for i in ' '.join(sentence_list).split('.')[:-1]]

Выход

[{'Ashraf', 'Born', 'Kabul'}, {'Afghanistan', 'Ashraf'}]

Обратите внимание, что явозвращая список sets, чтобы избежать дублирования значений, учитывая, что, например, в Entities Afghanistan появляется дважды.

Полезные чтения:

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...