File size: 2,181 Bytes
2ea1065
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
#
import random
import sys, os, pdb
import json, math
import datasets
from datasets import load_dataset
import csv
import shutil

random.seed(42)

class UniformFileName(object):
    def __init__(self) -> None:
        pass

    def _load_dir_json(self, dir_path=None):
        if dir_path is None or not os.path.exists(dir_path): return None
        total_data = [] # assume data is a dict of dialogs
        for filename in sorted(os.listdir(dir_path)):
            if not filename.startswith("dialogues_"): continue
            if not filename.endswith(".json"): continue
            file_path = os.path.join(dir_path, filename)
            data = self._load_json(path=file_path)
            if type(data) == list:
                total_data.extend(data)
            else:
                total_data.append(data)
        return total_data

    def uniform(self):
        for category in os.listdir("."):
            if not os.path.isdir(os.path.join("./", category)): continue
            for data_name in os.listdir(os.path.join("./", category)):
                if not os.path.isdir(os.path.join("./", category, data_name)): continue
                for mode in ["train", "val", "test"]:
                    folder_to_check = os.path.join("./", category, data_name, mode)
                    if not os.path.exists(folder_to_check): continue
                    filelist = os.listdir(folder_to_check)
                    if "dialogues_1.json" in filelist: continue
                    if "dialogs_1.json" not in filelist: raise ValueError(f"dialog file does not exist for {folder_to_check}")
                    for filename in filelist:
                        if filename.startswith("dialogs_") and filename.endswith(".json"):
                            filename_new = filename.replace("dialogs_", "dialogues_")
                            shutil.copy(os.path.join(folder_to_check, filename), os.path.join(folder_to_check, filename_new))
                            print(f"copy from {filename} to {filename_new} in {folder_to_check}")
    
def main():
    uniform = UniformFileName()
    uniform.uniform()


if __name__ == "__main__":
    main()