nerd / prepare_data.py
ramybaly
fixed fine-grained ner labels format
0f10ef1
raw
history blame contribute delete
954 Bytes
import glob
import json
import pickle
def main():
id2label_coarse = {int(i): tag for i, tag in json.load(open('./id2coarse_tags.json', 'r')).items()}
id2label_fine = {int(i): tag for i, tag in json.load(open('./id2fine_tags.json', 'r')).items()}
files = glob.glob('./*json')
for file in files:
if 'tags.json' in file:
continue
with open(file, 'r') as f:
sentences = f.readlines()
with open(file.replace('.json', '.txt'), 'w') as f:
for sentence in sentences:
sentence = json.loads(sentence)
tokens = sentence['tokens']
ner_tags_ids = sentence['coarse_tags']
ner_tags_fine_ids = sentence['fine_tags']
ner_tags_coarse = [id2label_coarse[t] for t in ner_tags_ids]
ner_tags_fine = [id2label_fine[t] for t in ner_tags_fine_ids]
for tok, tc, tf in zip(tokens, ner_tags_coarse, ner_tags_fine):
f.write(f'{tok}\t{tc}\t{tf}\n')
f.write('\n')
if __name__ == '__main__':
main()