-
Notifications
You must be signed in to change notification settings - Fork 0
/
EL_CNNDM.py
66 lines (55 loc) · 2.32 KB
/
EL_CNNDM.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import json
import argparse
import jsonlines
import requests
from REL.mention_detection import MentionDetection
from REL.utils import process_results
from REL.entity_disambiguation import EntityDisambiguation
from REL.ner import Cmns, load_flair_ner
wiki_version = "wiki_2019"
IP_ADDRESS = "http://localhost"
PORT = "1235"
base_url = "/home/yuqi/ex_sum/EL_data"
def example_preprocessing(texts):
# user does some stuff, which results in the format below.
processed = {}
for i, sentence in enumerate(texts):
processed["test_doc"+str(i+1)] = [texts[i], []]
return processed
def main():
parser = argparse.ArgumentParser("EL_CNNDM.py")
parser.add_argument('--data_dir', type=str, default='data/CNNDM', help='the data directory.')
parser.add_argument('--save_root', type=str, default='interest/CNNDM', help='the interest directory.')
args = parser.parse_args()
mention_detection = MentionDetection(base_url, wiki_version)
tagger_ner = load_flair_ner("ner-fast")
tagger_ngram = Cmns(base_url, wiki_version, n=5)
config = {
"mode": "eval",
"model_path": os.path.join(base_url, "ed-wiki-2019/model"),
}
model = EntityDisambiguation(base_url, wiki_version, config)
# get file dir
files_dir = []
for root, dir, files in os.walk(args.data_dir):
for file in files:
files_dir.append(os.path.join(root, file))
# begin read jsonl
for file in files_dir:
INTEREST_FILE = os.path.join(args.save_root, file.split('/')[-1].replace('.label', ''))
# print(INTEREST_FILE)
with jsonlines.open(file, 'r') as f_r:
with jsonlines.open(INTEREST_FILE, 'w') as f_w:
for line in f_r:
texts = line['text']
result = {'text': texts,
'linking_entity': []}
input_text = example_preprocessing(texts)
mentions_dataset, n_mentions = mention_detection.find_mentions(input_text, tagger_ner)
predictions, timing = model.predict(mentions_dataset)
linking_entity = process_results(mentions_dataset, predictions, input_text)
result['linking_entity'] = linking_entity
jsonlines.Writer.write(f_w,result)
if __name__ == '__main__':
main()