forked from AIrjen/OneButtonPrompt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv_reader.py
261 lines (217 loc) · 13.3 KB
/
csv_reader.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import csv
import random
import os
import shutil
def random_read_from_csv(filename):
script_dir = os.path.dirname(os.path.abspath(__file__)) # Script directory
full_path = os.path.join(script_dir, "./csvfiles/" )
with open(full_path + filename + ".csv", "r",encoding="utf8") as csv_file:
csv_reader = csv.reader(csv_file)
output = (random.choice([line[0] for line in csv_reader]))
return output
def add_from_csv(completeprompt, csvfilename, addcomma, prefix, suffix):
randomreadfromcsv = random_read_from_csv(csvfilename)
#print(csvfilename+ ": " + randomreadfromcsv)
addtoprompt = prefix + " " + randomreadfromcsv + " " + suffix
if(addcomma == 1):
return ", ".join([completeprompt,addtoprompt])
return " ".join([completeprompt,addtoprompt])
def csv_to_list(csvfilename, antilist=[], directory="./csvfiles/", lowerandstrip=0, delimiter=";", listoflistmode = False, skipheader = False, gender = "all", insanitylevel = -1):
replacing = False
userfilesdirectory = "./userfiles/"
userfileaddonname = csvfilename + "_addon.csv"
userfilereplacename = csvfilename + "_replace.csv"
lightfilename = csvfilename + "_light.csv"
mediumfilename = csvfilename + "_medium.csv"
csvlist = []
script_dir = os.path.dirname(os.path.abspath(__file__))
full_path = os.path.join(script_dir, directory )
userfilesfolder = os.path.join(script_dir, userfilesdirectory )
directoryfilesfolder = os.path.join(script_dir, directory )
# check if there is a replace file
if(directory=="./csvfiles/" or directory=="./csvfiles/special_lists/" or directory=="./csvfiles/templates/"):
for filename in os.listdir(userfilesfolder):
if(filename == userfilereplacename):
# Just override the parameters, and let it run normally
full_path = os.path.join(script_dir, userfilesdirectory )
csvfilename = csvfilename + "_replace"
replacing = True
# Go check for light or medium files if there is no override and there is an insanitylevel
if(replacing == False and insanitylevel > 0):
if(insanitylevel < 4):
for filename in os.listdir(directoryfilesfolder):
if(filename == mediumfilename):
# Just override the parameters, and let it run normally
full_path = os.path.join(script_dir, directory )
csvfilename = csvfilename + "_light"
replacing = True
# under 7, than only SOMETIMES take the full list
if(insanitylevel < 7 and random.randint(0,13) < 12 and replacing == False):
for filename in os.listdir(directoryfilesfolder):
if(filename == lightfilename):
# Just override the parameters, and let it run normally
full_path = os.path.join(script_dir, directory )
csvfilename = csvfilename + "_medium"
replacing = True
# return empty list if we can't find the file. Build for antilist.csv
if(os.path.isfile(full_path + csvfilename + ".csv")):
with open(full_path + csvfilename + ".csv", "r", newline="",encoding="utf8") as file:
reader = csv.reader(file, delimiter=delimiter)
if(skipheader==True):
next(reader)
if(listoflistmode==True):
csvlist = list(reader)
else:
for row in reader:
value = row[0]
if(
gender != "all" and (row[1] == gender or row[1] == "genderless" or row[1] == "both")
or gender == "all"
):
if(value.lower().strip() not in antilist):
if(lowerandstrip == 1):
csvlist.append(row[0].lower().strip())
else:
csvlist.append(row[0])
# do the add ons!
if(directory=="./csvfiles/" or directory=="./csvfiles/special_lists/"):
if(os.path.isfile(userfilesfolder + csvfilename + "_addon" + ".csv")):
with open(userfilesfolder + csvfilename + "_addon" + ".csv", "r", newline="",encoding="utf8") as file:
reader = csv.reader(file, delimiter=delimiter)
if(skipheader==True):
next(reader)
if(listoflistmode==True):
csvlist.append(list(reader))
else:
for row in reader:
value = row[0]
if(
gender != "all" and (row[1] == gender or row[1] == "genderless" or row[1] == "both")
or gender == "all"
):
if(value.lower().strip() not in antilist):
if(lowerandstrip == 1):
csvlist.append(row[0].lower().strip())
else:
csvlist.append(row[0])
# remove duplicates, but check only for lowercase stuff
deduplicated_list = []
lowercase_elements = set()
if(listoflistmode==False):
for element in csvlist:
lowercase_element = element.lower()
if lowercase_element not in lowercase_elements:
lowercase_elements.add(lowercase_element)
deduplicated_list.append(element)
else:
deduplicated_list = csvlist
return deduplicated_list
def artist_category_by_category_csv_to_list(csvfilename,artist):
csvlist = []
mediumlist = []
descriptionlist = []
script_dir = os.path.dirname(os.path.abspath(__file__))
full_path = os.path.join(script_dir, "./csvfiles/" )
with open(full_path + csvfilename + ".csv", "r", newline="",encoding="utf8") as file:
reader = csv.DictReader(file, delimiter=",")
for row in reader:
if(row["Artist"] == artist):
csvlist.append(row["Tags"])
mediumlist.append(row["Medium"])
descriptionlist.append(row["Description"])
return csvlist, mediumlist, descriptionlist
def artist_category_csv_to_list(csvfilename,category):
csvlist = []
script_dir = os.path.dirname(os.path.abspath(__file__))
full_path = os.path.join(script_dir, "./csvfiles/" )
with open(full_path + csvfilename + ".csv", "r", newline="",encoding="utf8") as file:
reader = csv.DictReader(file, delimiter=",")
for row in reader:
if(row[category] == "1"):
csvlist.append(row["Artist"])
return csvlist
def artist_descriptions_csv_to_list(csvfilename):
csvlist = []
script_dir = os.path.dirname(os.path.abspath(__file__))
full_path = os.path.join(script_dir, "./csvfiles/" )
with open(full_path + csvfilename + ".csv", "r", newline="",encoding="utf8") as file:
reader = csv.DictReader(file, delimiter=",")
for row in reader:
csvlist.append(row["Description"])
return csvlist
def load_config_csv():
csvlist = []
script_dir = os.path.dirname(os.path.abspath(__file__))
full_path_config_file = os.path.join(script_dir, "./userfiles/" )
full_path_default_config_file = os.path.join(script_dir, "./csvfiles/config/" )
config_file = full_path_config_file + 'config.csv'
default_config_file = full_path_default_config_file + 'default_config.csv'
if not os.path.exists(config_file):
shutil.copy2(default_config_file, config_file)
print("Config file created.")
with open(config_file, "r", newline="",encoding="utf8") as file:
reader = csv.DictReader(file, delimiter=";")
csvlist = [list(row.values()) for row in reader if not any(value.startswith('#') for value in row.values())]
return csvlist
def load_negative_list():
primerlist = []
negativelist = []
primeraddonlist = []
negativeaddonlist = []
script_dir = os.path.dirname(os.path.abspath(__file__))
full_path_default_negative_file = os.path.join(script_dir, "./csvfiles/special_lists/" )
negative_file = full_path_default_negative_file + 'negativewords.csv'
full_path_replace_negative_file = os.path.join(script_dir, "./userfiles/" )
replace_negative_file = full_path_replace_negative_file + 'negativewords_replace.csv'
full_path_addon_negative_file = os.path.join(script_dir, "./userfiles/" )
addon_negative_file = full_path_addon_negative_file + 'negativewords_addon.csv'
if(os.path.isfile(replace_negative_file)):
negative_file = replace_negative_file
with open(negative_file, "r", newline="",encoding="utf8") as file:
reader = csv.DictReader(file, delimiter=";")
primerlist = [row["primer"] for row in reader]
with open(negative_file, "r", newline="",encoding="utf8") as file:
reader = csv.DictReader(file, delimiter=";")
negativelist = [row["negative"] for row in reader]
if(os.path.isfile(addon_negative_file)):
with open(addon_negative_file, "r", newline="",encoding="utf8") as file:
reader = csv.DictReader(file, delimiter=";")
primeraddonlist = [row["primer"] for row in reader]
with open(addon_negative_file, "r", newline="",encoding="utf8") as file:
reader = csv.DictReader(file, delimiter=";")
negativeaddonlist = [row["negative"] for row in reader]
primerlist += primeraddonlist
negativelist += negativeaddonlist
return primerlist, negativelist
def load_all_artist_and_category():
artistlist = []
categorylist = []
script_dir = os.path.dirname(os.path.abspath(__file__))
full_path_default_artist_file = os.path.join(script_dir, "./csvfiles/" )
artist_file = full_path_default_artist_file + 'artists_and_category.csv'
with open(artist_file, "r", newline="",encoding="utf8") as file:
reader = csv.DictReader(file, delimiter=",")
artistlist = [row["Artist"] for row in reader]
with open(artist_file, "r", newline="",encoding="utf8") as file:
reader = csv.DictReader(file, delimiter=",")
categorylist = [row["Tags"] for row in reader]
return artistlist, categorylist
def sort_and_dedupe_csv_file():
tokenlist = csv_to_list(csvfilename="tokens",skipheader=False)
tokenlist = sorted(set(tokenlist))
newlist = []
for i in tokenlist:
j = i.lower()
result = all(c == i[0] for c in i)
if result:
print("String {} have all characters same".format(i))
else:
newlist.append(j)
newlist = sorted(set(newlist))
script_dir = os.path.dirname(os.path.abspath(__file__))
full_path = os.path.join(script_dir, "./csvfiles/special_lists/tokenwriter.csv" )
with open(full_path, 'w', encoding="utf8") as fp:
for item in newlist:
# write each item on a new line
fp.write("%s\n" % item)
# sort_and_dedupe_csv_file()