Markk commited on
Commit
a7d3244
1 Parent(s): fb832a3

Add Dataset Tool

Browse files
Files changed (1) hide show
  1. dataset_tool.py +180 -0
dataset_tool.py ADDED
@@ -0,0 +1,180 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Author: @MarkK
2
+ # Date: 2023-03-18
3
+ # Description: A program that user can use for comfy interaction with the dataset (edit/add/delete).
4
+
5
+ import csv
6
+ import re
7
+ import sys
8
+ import os
9
+
10
+ # welcome message and list of available commands
11
+ print("""
12
+ __ __ _
13
+ \ \ / / | |
14
+ \ \ /\ / /__| | ___ ___ _ __ ___ ___
15
+ \ \/ \/ / _ \ |/ __/ _ \| '_ ` _ \ / _ \
16
+ \ /\ / __/ | (_| (_) | | | | | | __/
17
+ \/ \/ \___|_|\___\___/|_| |_| |_|\___|
18
+
19
+ Welcome to Dataset-tool!
20
+ """)
21
+
22
+ print("Available commands:")
23
+ print("- del (delete the last row from 'dataset.csv')")
24
+ print("- showXXXX (print the XXXX row inside 'dataset.csv')")
25
+ print("- last (print the last row from 'dataset.csv')")
26
+ print("- set:XXXX (set EP_ID)")
27
+ print("Usage: Enter a multi-line text string + Hit Enter + Hit Ctrl+D to extract data and import it inside dataset")
28
+ print("Usage: Enter a command + Hit Enter + Hit Ctrl+D to execute command")
29
+
30
+ print()
31
+
32
+
33
+ # list of known speakers
34
+ known_speakers = (["SpongeBob", "Patrick", "Squidward", "Krabs", "Plankton", "driver",
35
+ "Anchovy", "Sandy", "Narrator", "Larry", "Gary", "Montage",
36
+ "guy", "All", "Loser", "Scooter", "Pants", "Frank", "Both", "Karen",
37
+ "SpongeBobBubble", "PatrickBubble", "SquidwardBubble", "Fred", "Puff"])
38
+ ep_id = "x"
39
+
40
+ # loop the program
41
+ while True:
42
+
43
+ # take input from user
44
+ print("Enter a text string or a command: ")
45
+ input_string = sys.stdin.read()
46
+
47
+ # check if input is a command
48
+ if input_string.startswith("set"):
49
+ index = input_string[4:]
50
+ ep_id = index
51
+ print("Set index: ", ep_id)
52
+ elif input_string.startswith("last"):
53
+ try:
54
+ with open("dataset.csv", mode="r") as file:
55
+ lines = file.readlines()
56
+ if len(lines) > 0:
57
+ row = lines[-1].strip().split(";")
58
+ print(f"Last row: {row[0]}: {row[1]}")
59
+ else:
60
+ print("Error: dataset is empty.")
61
+ except IOError:
62
+ print("Error: could not open dataset.csv.")
63
+ elif input_string.startswith("del"):
64
+ try:
65
+ with open("dataset.csv", mode="r") as file:
66
+ lines = file.readlines()
67
+ if len(lines) > 0:
68
+ row = lines[-1].strip().split(";")
69
+ with open("dataset.csv", mode="w") as file:
70
+ file.writelines(lines[:-1])
71
+ print(f"Deleted row: {row[0]}: {row[1]}")
72
+ else:
73
+ print("Error: dataset is empty.")
74
+ except IOError:
75
+ print("Error: could not open dataset.csv.")
76
+ elif input_string.startswith("show"):
77
+ try:
78
+ index = int(input_string[4:])
79
+ with open("dataset.csv", mode="r") as file:
80
+ reader = csv.reader(file, delimiter=";")
81
+ rows = list(reader)
82
+ if index >= 0 and index < len(rows):
83
+ row = rows[index]
84
+ print(f"Speaker: {row[0]}\nReplica: {row[1]}\n")
85
+ else:
86
+ print("Error: invalid row index.")
87
+ except ValueError:
88
+ print("Error: invalid row index.")
89
+ except IOError:
90
+ print("Error: could not open dataset.csv.")
91
+ else:
92
+ num_lines = input_string.count('\n')
93
+ if num_lines > 1:
94
+ lines = input_string.split("\n")
95
+ lines.pop()
96
+ for line in lines:
97
+ # extract speaker and replica from input string
98
+ match = re.match(r"([A-Za-z ]+):\s*(.*)", line)
99
+ if match:
100
+ speaker = match.group(1)
101
+ replica = match.group(2)
102
+ if speaker not in known_speakers or speaker is None:
103
+ speaker = "{system}"
104
+ if speaker == "Krabs":
105
+ speaker = "Mr. Krabs"
106
+ if speaker == "Puff":
107
+ speaker = "Mrs. Puff"
108
+ if speaker == "driver":
109
+ speaker = "Bus driver"
110
+ if speaker == "Narrator":
111
+ speaker = "French Narrator"
112
+ if speaker == "guy":
113
+ speaker = "Sports guy"
114
+ else:
115
+ match = re.search(r"I(\d+):\s*(.*)", line)
116
+ if match:
117
+ speaker = f"Incidental {match.group(1)}"
118
+ replica = match.group(2)
119
+ elif line.startswith("["):
120
+ speaker = "{system}"
121
+ replica = line
122
+ else:
123
+ print("Error: Could not extract speaker and replica from line.")
124
+ speaker = "Error: Could not extract speaker and replica from line."
125
+ replica = "Error: Could not extract speaker and replica from line."
126
+
127
+ # write to dataset.csv file
128
+ try:
129
+ with open("dataset.csv", mode="a", newline="") as file:
130
+ writer = csv.writer(file, delimiter=";", quotechar=" ")
131
+ writer.writerow([speaker, replica, ep_id])
132
+
133
+ # print the result
134
+ print(f"Speaker: {speaker}\nReplica: {replica}\n")
135
+ except PermissionError:
136
+ print("PermissionError while opening file.\n")
137
+ except UnicodeEncodeError:
138
+ print("UnicodeEncodeError while opening file.\n")
139
+ speaker = "Error: UnicodeEncodeError while opening file."
140
+ replica = "Error: UnicodeEncodeError while opening file."
141
+ else:
142
+ # extract speaker and replica from input string
143
+ match = re.match(r"([A-Za-z ]+):\s*(.*)", input_string)
144
+ if match:
145
+ speaker = match.group(1)
146
+ replica = match.group(2)
147
+ if speaker not in known_speakers or speaker is None:
148
+ speaker = "{system}"
149
+ if speaker == "Krabs":
150
+ speaker = "Mr. Krabs"
151
+ if speaker == "Puff":
152
+ speaker = "Mrs. Puff"
153
+ if speaker == "driver":
154
+ speaker = "Bus driver"
155
+ if speaker == "Narrator":
156
+ speaker = "French Narrator"
157
+ if speaker == "guy":
158
+ speaker = "Sports guy"
159
+ else:
160
+ match = re.search(r"I(\d+):\s*(.*)", input_string)
161
+ if match:
162
+ speaker = f"Incidental {match.group(1)}"
163
+ replica = match.group(2)
164
+ elif input_string.startswith("["):
165
+ speaker = "{system}"
166
+ replica = input_string
167
+ else:
168
+ print("Error: Could not extract speaker and replica from input string.")
169
+ continue
170
+
171
+ # write to dataset.csv file
172
+ try:
173
+ with open("dataset.csv", mode="a", newline="") as file:
174
+ writer = csv.writer(file, delimiter=";", quotechar=" ")
175
+ writer.writerow([speaker, replica, ep_id])
176
+
177
+ # print the result
178
+ print(f"Speaker: {speaker}\nReplica: {replica}\n")
179
+ except PermissionError:
180
+ print("PermissionError while opening file.\n")