File size: 954 Bytes
7b2f59a 0f10ef1 3cb0756 7b2f59a 0f10ef1 7b2f59a 0f10ef1 3cb0756 7b2f59a 0f10ef1 7b2f59a 0903768 |
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 |
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()
|