-
Notifications
You must be signed in to change notification settings - Fork 3
/
labelled_to_unlabelled.py
66 lines (46 loc) · 1.88 KB
/
labelled_to_unlabelled.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
"""
Code Language: python
Script: hf_labelled_to_unlabelled.py
Imports: click, os
Functions: main(), process(), delete_intent_ids_from_examples()
Description: Converts labelled HF data to Unlabelled format
"""
# **********************************************************************************************************************
# standard imports
import json
from os.path import isfile
# 3rd party imports
import click
@click.command()
@click.option('-f', '--filepath', type=str, required=True, help='HF labelled json file path')
@click.option('-o', '--output_filepath', type=str, default='', help='HF unlabelled json file path')
def main(filepath: str, output_filepath: str) -> None:
'''Main Function'''
process(filepath, output_filepath)
def process(filepath: str, output_filepath: str) -> None:
'''Convert labelled to unlabelled file'''
if isfile(filepath):
with open(filepath, mode="r", encoding="utf8") as f:
data = json.load(f)
else:
raise FileNotFoundError(f"{filepath} does not exist")
if "intents" in data.keys():
del data["intents"]
if "tags" in data.keys():
del data["tags"]
if "examples" in data.keys():
data["examples"] = delete_intent_ids_from_examples(data["examples"])
if output_filepath == '':
output_filepath = filepath.split("/")
output_filepath[-1] = "unlabelled.json"
output_filepath = '/'.join(output_filepath)
with open(output_filepath, mode="w", encoding="utf8") as f:
json.dump(data, f, indent=3)
def delete_intent_ids_from_examples(examples: list) -> list:
'''deletes intent ids from examples'''
for i,_ in enumerate(examples):
if "intents" in examples[i].keys():
del examples[i]["intents"]
return examples
if __name__ == "__main__":
main() # pylint: disable=no-value-for-parameter