-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
174 lines (158 loc) · 5.5 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import string
import re
def find_nth_str(haystack, needle, n):
start = haystack.find(needle)
while start >= 0 and n > 1:
start = haystack.find(needle, start+len(needle))
n -= 1
return start
def find_nth_list(haystack, needle, n):
start = haystack.index(needle)
while start >= 0 and n > 1:
start = haystack.index(needle, start+1)
n -= 1
return start
def find_nth_list_subset(haystack, needle, n):
if n < 0:
return -1
if n == 0:
n = 1
found = []
needle_size = len(needle.split(" "))
for i in range(len(haystack)):
sliced = " ".join(haystack[i:i+needle_size])
#print(sliced)
if needle == sliced:
found.append(i)
#print(found,"n:",n)
indexs=[]
if len(found) > n:
indexs=found[n-1:]
return indexs
elif len(found) == n:
indexs=[found[n-1]]
return indexs
elif len(found) < n:
indexs=[]
return indexs
def separate_single_multi(l):
singles, multis = [], []
for item in l:
i = item.strip()
if " " in i:
multis.append(i)
else:
singles.append(i)
return singles, multis
def verbose(func):
def inner(*args, **kwargs):
if kwargs.get("verbose", False):
i = kwargs.get("indent_level", 0)
indent = "\t"*i
print(f"{indent}{args[0].strip()}")
return func(*args, **kwargs)
return inner
class AnswerMapping:
@staticmethod
@verbose
def get_numbered_list_items(output, verbose=False, indent_level=0):
final = []
if "\n" in output:
candidates = output.split("\n")
for cand in candidates:
c = cand.strip()
if c.lower().strip() in ["", "answer:"]:
pass
elif re.match(r"\d+[.)]+ *", c):
startindex = c.find('.')
final.append(c[startindex+1:].strip())
else:
print(f"Unable to match nonempty {c}")
pass
else:
candidates = re.split(r"\d+[.)]", output)
for cand in candidates:
c = cand.strip()
if c.lower().strip() in ["", "answer:"]:
pass
else:
final.append(c)
return final
@staticmethod
@verbose
def get_true_or_false(output, default=True, verbose=False, indent_level=0):
output = output.lower()
true_condition = "yes " in output or "yes." in output or "true" in output
false_condition = "no " in output or "no." in output or "false" in output
if true_condition and not false_condition:
return True
elif false_condition and not true_condition:
return False
else:
if not true_condition and not false_condition:
print(f"Unable to map {output} to True or False")
else:
print(f"Mapping {output} to both True or False")
return default
@staticmethod
@verbose
def exemplar_format_list(output, verbose=False, indent_level=0, separator='|', true_only=True, identify_types=False):
if "\n" in output:
listed = AnswerMapping.get_numbered_list_items(output, verbose=False, indent_level=indent_level+1)
else:
listed = []
if "1" in output:
split = re.split(r"\d+[.)]", output)
for item in split:
if item.strip().lower() == "" or "answer" in item.strip().lower():
pass
else:
listed.append(item.strip())
final = []
typestring = []
for option in listed:
if separator in option:
split = option.split(separator)
explanation = None
if len(split) == 1:
print(f"Got only one value for {option} with separator '{separator}'")
continue
elif len(split) == 2:
entity, depends = split
if depends.strip().lower() in ["true", "false"]:
status = depends
else:
status = "True"
explanation = depends
elif len(split) == 3:
entity, status, explanation = split
else:
entity, status = split[0], split[1]
print(f"Got more than 3 values for {option} with separator '{separator}'")
if status.strip().lower() == "true" or not true_only:
if explanation is not None:
typestring.append(explanation.strip())
final.append(entity.strip().lower())
else:
pass
else:
final.append(option.strip().lower())
if not identify_types:
return final
else:
return final, typestring
class Parameters:
devices = ["cuda:0"]
@staticmethod
def get_device_ints(limit=3):
assert "cpu" not in Parameters.devices[:limit]
final = []
for item in Parameters.devices[:limit]:
if isinstance(item, int):
final.append(item)
elif item.isnumeric():
final.append(int(item))
else:
f, l = item.split(":")
final.append(int(l))
return final