Added json utils to save and load json files
This commit is contained in:
parent
97bac464f3
commit
dbf1d99408
18
Project_Model/Libs/BPE/Utils/json_utils.py
Normal file
18
Project_Model/Libs/BPE/Utils/json_utils.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
def save_json(vocabulary: dict, path: Path):
|
||||||
|
|
||||||
|
json_string = json.dumps(vocabulary)
|
||||||
|
FILE = open(path, "w")
|
||||||
|
FILE.write(json_string)
|
||||||
|
FILE.close()
|
||||||
|
|
||||||
|
|
||||||
|
def load_json(path: Path) -> dict[tuple[int, int], int]:
|
||||||
|
FILE = open(path, "r")
|
||||||
|
json_string = FILE.read()
|
||||||
|
FILE.close()
|
||||||
|
|
||||||
|
return json.loads(json_string)
|
||||||
49
Project_Model/Libs/BPE/Utils/vocabulary.py
Normal file
49
Project_Model/Libs/BPE/Utils/vocabulary.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
from ..Errors import OutOfDictionaryException
|
||||||
|
|
||||||
|
|
||||||
|
def nanos_vocabulary2json_str(vocabulary: dict[tuple[int, int], int]) -> str:
|
||||||
|
|
||||||
|
JSON: dict[str, int] = {}
|
||||||
|
|
||||||
|
for key, item in vocabulary.items():
|
||||||
|
TUPLE_STR = f"{key}"
|
||||||
|
JSON[TUPLE_STR] = item
|
||||||
|
|
||||||
|
return json.dumps(JSON)
|
||||||
|
|
||||||
|
|
||||||
|
def nanos_json_str2vocabulary(json_string: str) -> dict[tuple[int, int], int]:
|
||||||
|
|
||||||
|
JSON: dict[str, int] = json.loads(json_string)
|
||||||
|
VOCABULARY: dict[tuple[int, int], int] = {}
|
||||||
|
|
||||||
|
for key, item in JSON.items():
|
||||||
|
REDUCED_KEY = len(key) - 1
|
||||||
|
KEY_STR = key[1:REDUCED_KEY]
|
||||||
|
VOC_KEY = tuple(map(int, KEY_STR.split(",")))
|
||||||
|
|
||||||
|
if len(VOC_KEY) != 2:
|
||||||
|
raise OutOfDictionaryException()
|
||||||
|
|
||||||
|
# Checked for weird things above
|
||||||
|
VOCABULARY[VOC_KEY] = item # type: ignore
|
||||||
|
|
||||||
|
return VOCABULARY
|
||||||
|
|
||||||
|
|
||||||
|
def save_nanos_vocabulary(vocabulary: dict[tuple[int, int], int], path: Path):
|
||||||
|
|
||||||
|
json_string = nanos_vocabulary2json_str(vocabulary)
|
||||||
|
FILE = open(path, "w")
|
||||||
|
FILE.write(json_string)
|
||||||
|
FILE.close()
|
||||||
|
|
||||||
|
|
||||||
|
def load_nanos_vocabulary(path: Path) -> dict[tuple[int, int], int]:
|
||||||
|
FILE = open(path, "r")
|
||||||
|
json_string = FILE.read()
|
||||||
|
FILE.close()
|
||||||
|
|
||||||
|
return nanos_json_str2vocabulary(json_string)
|
||||||
Loading…
x
Reference in New Issue
Block a user