forked from BeastByteAI/scikit-llm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
72 lines (60 loc) · 1.66 KB
/
utils.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
67
68
69
70
71
72
import json
from typing import Any
import numpy as np
import pandas as pd
def to_numpy(X: Any) -> np.ndarray:
"""Converts a pandas Series or list to a numpy array.
Parameters
----------
X : Any
The data to convert to a numpy array.
Returns
-------
X : np.ndarray
"""
if isinstance(X, pd.Series):
X = X.to_numpy().astype(object)
elif isinstance(X, list):
X = np.asarray(X, dtype=object)
if isinstance(X, np.ndarray) and len(X.shape) > 1:
X = np.squeeze(X)
return X
def find_json_in_string(string: str) -> str:
"""Finds the JSON object in a string.
Parameters
----------
string : str
The string to search for a JSON object.
Returns
-------
json_string : str
"""
start = string.find("{")
end = string.rfind("}")
if start != -1 and end != -1:
json_string = string[start : end + 1]
else:
json_string = "{}"
return json_string
def extract_json_key(json_: str, key: str):
"""Extracts JSON key from a string.
json_ : str
The JSON string to extract the key from.
key : str
The key to extract.
"""
original_json = json_
for i in range(2):
try:
json_ = original_json.replace("\n", "")
if i == 1:
json_ = json_.replace("'", '"')
json_ = find_json_in_string(json_)
as_json = json.loads(json_)
if key not in as_json.keys():
raise KeyError("The required key was not found")
return as_json[key]
except Exception:
if i == 0:
continue
return None