-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert.py
156 lines (119 loc) · 4.48 KB
/
convert.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import os
import re
import sys
import json
class TOKEN:
def __init__(self, id, text, ner):
self.id = id
self.orth = text
self.space = ' '
self.tag = '-'
self.ner = ner
def serialize(self):
return self.__dict__
def get_tokens(token_id, text, label='', word_delimiter=' '):
tokens = []
words = [i.strip() for i in text.split(word_delimiter) if len(i) > 0]
if label:
action_tag = 'B-'
else:
action_tag = 'O'
for word in words[:-1]:
token = TOKEN(token_id, word, action_tag+label)
token_id += 1
if label:
action_tag = 'I-'
tokens.append(token)
if label:
if len(words) == 1:
action_tag = 'U-'
else:
action_tag = 'L-'
token = TOKEN(token_id, words[-1], action_tag+label)
tokens.append(token)
token_id += 1
return tokens, token_id
class SENTENCE:
def __init__(self):
self.tokens = []
self.brackets = []
def add_tokens(self, tokens):
for token in tokens:
self.tokens.append(token)
def serialize(self):
self.tokens = [i.serialize() for i in self.tokens]
return self.__dict__
class PARAGRAPH:
def __init__(self, raw=None):
self.raw = raw
self.sentences = []
self.cats = []
self.entities = []
self.links = []
def add_sentence(self, sentence):
self.sentences.append(sentence)
def add_entity(self, entity):
self.entities.append(entity)
def serialize(self):
self.sentences = [i.serialize() for i in self.sentences]
return self.__dict__
class ANNOTATION:
def __init__(self, id):
self.id = id
self.paragraphs = []
def add_paragraph(self, paragraph):
self.paragraphs.append(paragraph)
def serialize(self):
self.paragraphs = [i.serialize() for i in self.paragraphs]
return self.__dict__
def convert_to_bilou(jsonl_file, para_delimiter='\n\n\n', line_delimiter='\n', word_delimiter=' '):
fl = open(jsonl_file, 'r', encoding='utf8')
lines = fl.readlines()
annotations = []
annotation_id = 0
for line in lines:
doc = json.loads(line)
text_key = 'text' if 'text' in doc.keys() else 'data'
text = doc[text_key]
entity_key = 'entities' if 'entities' in doc.keys() else 'label'
entities = doc[entity_key]
start_key = 'start_offset' if entity_key == 'entities' else 0
end_key = 'end_offset' if entity_key == 'entities' else 1
label_key = 'label' if entity_key == 'entities' else 2
text_without_entities = ""
last_idx = 0
for e in entities:
text_without_entities += text[last_idx:e[start_key]]+'_|'*(e[end_key]-e[start_key])
last_idx = e[end_key]
token_id = 0
entity_idx = 0
annotation = ANNOTATION(annotation_id)
annotation_id += 1
paragraphs = [i for i in text_without_entities.split(para_delimiter) if len(i) > 0]
txt_para = [i for i in text.split(para_delimiter) if len(i) > 0]
for i, p in enumerate(paragraphs):
paragraph = PARAGRAPH(raw=txt_para[i])
sentences = [i for i in p.split(line_delimiter) if len(i) > 0]
for s in sentences:
sentence = SENTENCE()
words = [i.strip() for i in s.split(word_delimiter) if len(i) > 0]
for word in words:
if re.search('_\|{1,}', word):
entity = entities[entity_idx]
entity_idx += 1
tokens, token_id = get_tokens(token_id, text[entity[start_key]:entity[end_key]], entity[label_key], word_delimiter)
sentence.add_tokens(tokens)
paragraph.add_entity([entity[start_key], entity[end_key], entity[label_key]])
else:
token = TOKEN(token_id, word, 'O')
sentence.add_tokens([token])
paragraph.add_sentence(sentence)
annotation.add_paragraph(paragraph)
annotations.append(annotation.serialize())
out_path = jsonl_file.replace('\\', '/')
out_file_path = out_path[:out_path.rindex('/')+1] if '/' in out_path else './'
with open(out_file_path+'annotation_iob.json', 'w') as f:
json.dump(annotations, f)
if __name__ == '__main__':
file_name = sys.argv[1]
convert_to_bilou(file_name)