-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_update_dataset.py
217 lines (188 loc) · 7.15 KB
/
func_update_dataset.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
from csv import DictReader
import pandas as pd
import numpy as np
def open_file(filepath):
df = pd.read_csv(filepath, encoding="utf-8")
df = df.rename(columns=lambda x: x.strip())
df['identifier'] = df['identifier'].astype('Int64')
df.set_index('identifier', inplace=True)
return df
def save_file(df, filepath):
file_location = filepath.split('.csv')[0]
save_file_location = file_location + '_updated.csv'
df.to_csv(save_file_location, encoding='utf-8')
def add_row(df):
new_row = {
"title": np.nan,
"description": np.nan,
"url": np.nan,
"type": np.nan,
"assesses": np.nan,
"comesAfter": np.nan,
"alternativeContent": np.nan,
"requires": np.nan,
"contains": np.nan,
"isPartOf": np.nan,
"isFormatOf": np.nan,
}
print("Do you want to: add to the end (e), add to specific index (i).")
choice = input("input: ")
match choice:
case "e":
# Add to the end of the dataframe, before the "end" node
df.loc[df.tail(1).index.item() - 0.5] = new_row
df = df.sort_index().reset_index(drop=True)
identifier = df.tail(1).index.item() - 1
case "i":
print("Give index:")
print("TBD")
# shift_row(index, df, new_row)
# index = input("input: ")
case _:
print("Did not catch that. Try again?")
return update_row(df, identifier, new_row)
def edit(df):
print("Please indicate what row you wish to edit.")
identifier = input("Row ID:")
new_row = {
"title": df.at[int(identifier), "title"],
"description": df.at[int(identifier), "description"],
"url": df.at[int(identifier), "url"],
"type": df.at[int(identifier), "type"],
"assesses": df.at[int(identifier), "assesses"],
"comesAfter": df.at[int(identifier), "comesAfter"],
"alternativeContent": df.at[int(identifier), "alternativeContent"],
"requires": df.at[int(identifier), "requires"],
"contains": df.at[int(identifier), "contains"],
"isPartOf": df.at[int(identifier), "isPartOf"],
"isFormatOf": df.at[int(identifier), "isFormatOf"],
}
return update_row(df, identifier, new_row)
def update_row(df, identifier, new_row):
print("")
print("What do you wish to edit? Please, select one of the following: ")
print("title (t), "
"description (d), "
"url (u), "
"type (type), "
"assesses (a), "
"comesAfter (ca), "
"alternativeContent (ac), "
"requires (r), "
"contains (c), "
"isPartOf (ipo), "
"isFormatOf (ifo), ")
print("Do you want to see your current edits? Print (p), ",
"Done with edits to ID: " + str(identifier) + ". Type (save) to save and exit or (cancel) to cancel and exit.")
# input("Press enter to continue...")
while True:
edit_choice = input('input: ')
match edit_choice:
case "t":
print("Change title.")
new_row["title"] = input("Input: ")
case "d":
print("Change description")
new_row["description"] = input("Input: ")
case "u":
print("Change URL")
new_row["url"] = input("Input: ")
case "type":
print("Change type")
new_row["type"] = input("Input:")
case "a":
print("Change assesses relation")
new_row["assesses"] = input("Input: ")
case "ca":
print("Change comesAfter relation")
new_row["comesAfter"] = input("Input: ")
case "ac":
print("Change alternativeContent relation")
new_row["alternativeContent"] = input("Input: ")
case "r":
print("Change requires relation")
new_row["requires"] = input("Input: ")
case "c":
print("Change contains relation")
new_row["contains"] = input("Input: ")
case "ipo":
print("Change isPartOf relation")
new_row["isPartOf"] = input("Input: ")
case "ifo":
print("Change isFormatOf relation")
new_row["isFormatOf"] = input("Input: ")
case "p":
print(new_row)
case "save":
df = save_row(df, identifier, new_row)
break
case "cancel":
break
case _:
print("Did not catch that. Try again?")
# input("Press enter to continue...")
return df
def save_row(df, identifier, new_row):
print("Edit_row")
df.at[int(identifier), "title"] = new_row["title"]
df.at[int(identifier), "description"] = new_row["description"]
df.at[int(identifier), "url"] = new_row["url"]
df.at[int(identifier), "assesses"] = new_row["assesses"]
df.at[int(identifier), "comesAfter"] = new_row["comesAfter"]
df.at[int(identifier), "alternativeContent"] = new_row["alternativeContent"]
df.at[int(identifier), "requires"] = new_row["requires"]
df.at[int(identifier), "contains"] = new_row["contains"]
df.at[int(identifier), "isPartOf"] = new_row["isPartOf"]
df.at[int(identifier), "isFormatOf"] = new_row["isFormatOf"]
return df
def remove_empty_lines(df):
df = df.dropna(how='all')
return df
# identifier, title, description, url, type, assesses, comesAfter, alternativeContent, requires, contains, isPartOf, isFormatOf
def delete_row(df, id):
id = int(id)
df = delete_relationship(df, id)
df = df.drop(id)
df = shift_nodes_after_empty_lines(df)
return df
def delete_relationship(df, id):
df = df.replace(id, np.nan)
df = delete_relationship_lists(df, id)
return df
def delete_relationship_lists(df, id):
for index, row in df.iterrows():
try:
if np.isnan(df.at[index, 'contains']):
pass
except:
df.loc[index,'contains'] = df.loc[index, 'contains'].replace(str(id), np.nan)
try:
if np.isnan(df.at[index, 'requires']):
pass
except:
df.loc[index, 'requires'] = df.loc[index, 'requires'].replace(str(id), np.nan)
return df
def shift_nodes_after_empty_lines(df):
old_index = -1
change = False
for index, row in df.iterrows():
index_int = int(index)
new_index = old_index + 1
if index_int != new_index:
change = True
df.rename(index={index: str(new_index)}, inplace=True)
df = df.replace(index, str(new_index))
df = check_alt_list(df, str(index), str(new_index))
old_index = new_index
if change:
print("Changed index of node(s).")
return df
def check_alt_list(df, old_index, new_index):
for index, row in df.iterrows():
try:
if np.isnan(df.at[index, 'contains']):
pass
except:
if old_index in df.loc[index,'contains']:
df.loc[index,'contains'] = df.loc[index, 'contains'].replace(old_index, new_index)
return df