Views
No views yet
XXXX <PER> entity1 </PER> XXXXXXX <ORG> entity2 </ORG> XXXXX). Then we use the BERT [CLS] representation to make a prediction.1>>> from transformers import pipeline, AutoModelForTokenClassification, AutoTokenizer, AuotoModelForSequenceClassification
2
3>>> ner_model = AutoModelForTokenClassification.from_pretrained("ychenNLP/arabic-ner-ace")
4>>> ner_tokenizer = AutoTokenizer.from_pretrained("ychenNLP/arabic-ner-ace")
5>>> ner_pip = pipeline("ner", model=ner_model, tokenizer=ner_tokenizer, grouped_entities=True)
6
7>>> re_model = AutoModelForSequenceClassification.from_pretrained("ychenNLP/arabic-relation-extraction")
8>>> re_tokenizer = AutoTokenizer.from_pretrained("ychenNLP/arabic-relation-extraction")
9>>> re_pip = pipeline("text-classification", model=re_model, tokenizer=re_tokenizer)
10
11def process_ner_output(entity_mention, inputs):
12 re_input = []
13 for idx1 in range(len(entity_mention) - 1):
14 for idx2 in range(idx1 + 1, len(entity_mention)):
15 ent_1 = entity_mention[idx1]
16 ent_2 = entity_mention[idx2]
17
18 ent_1_type = ent_1['entity_group']
19 ent_2_type = ent_2['entity_group']
20 ent_1_s = ent_1['start']
21 ent_1_e = ent_1['end']
22 ent_2_s = ent_2['start']
23 ent_2_e = ent_2['end']
24 new_re_input = ""
25 for c_idx, c in enumerate(inputs):
26 if c_idx == ent_1_s:
27 new_re_input += "<{}>".format(ent_1_type)
28 elif c_idx == ent_1_e:
29 new_re_input += "</{}>".format(ent_1_type)
30 elif c_idx == ent_2_s:
31 new_re_input += "<{}>".format(ent_2_type)
32 elif c_idx == ent_2_e:
33 new_re_input += "</{}>".format(ent_2_type)
34 new_re_input += c
35 re_input.append({"re_input": new_re_input, "arg1": ent_1, "arg2": ent_2, "input": inputs})
36 return re_input
37
38def post_process_re_output(re_output, text_input, ner_output):
39 final_output = []
40 for idx, out in enumerate(re_output):
41 if out["label"] != 'O':
42 tmp = re_input[idx]
43 tmp['relation_type'] = out
44 tmp.pop('re_input', None)
45 final_output.append(tmp)
46
47 template = {"input": text_input,
48 "entity": ner_output,
49 "relation": final_output}
50
51 return template
52
53text_input = """ويتزامن ذلك مع اجتماع بايدن مع قادة الدول الأعضاء في الناتو في قمة موسعة في العاصمة الإسبانية، مدريد."""
54
55ner_output = ner_pip(text_input) # inference NER tags
56
57re_input = process_ner_output(ner_output, text_input) # prepare a pair of entity and predict relation type
58
59re_output = []
60for idx in range(len(re_input)):
61 tmp_re_output = re_pip(re_input[idx]["re_input"]) # for each pair of entity, predict relation
62 re_output.append(tmp_re_output[0])
63
64
65
66re_ner_output = post_process_re_output(re_output, text_input, ner_output) # post process NER and relation predictions
67print("Sentence: ",re_ner_output["input"])
68print('====Entity====')
69for ent in re_ner_output["entity"]:
70 print('{}--{}'.format(ent["word"], ent["entity_group"]))
71print('====Relation====')
72for rel in re_ner_output["relation"]:
73 print('{}--{}:{}'.format(rel['arg1']['word'], rel['arg2']['word'], rel['relation_type']['label']))
74
75Sentence: ويتزامن ذلك مع اجتماع بايدن مع قادة الدول الأعضاء في الناتو في قمة موسعة في العاصمة الإسبانية، مدريد.
76====Entity====
77بايدن--PER
78قادة--PER
79الدول--GPE
80الناتو--ORG
81العاصمة--GPE
82الاسبانية--GPE
83مدريد--GPE
84====Relation====
85قادة--الدول:ORG-AFF
86الدول--الناتو:ORG-AFF
87العاصمة--الاسبانية:PART-WHOLE1@inproceedings{lan2020gigabert,
2 author = {Lan, Wuwei and Chen, Yang and Xu, Wei and Ritter, Alan},
3 title = {Giga{BERT}: Zero-shot Transfer Learning from {E}nglish to {A}rabic},
4 booktitle = {Proceedings of The 2020 Conference on Empirical Methods on Natural Language Processing (EMNLP)},
5 year = {2020}
6 }