File size: 8,730 Bytes
a7d3244
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58ebaff
 
 
 
 
 
cfb717b
a7d3244
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58ebaff
a7d3244
 
 
 
 
cfb717b
 
 
 
a7d3244
 
 
 
 
 
 
 
58ebaff
a7d3244
 
 
58ebaff
 
 
a7d3244
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58ebaff
a7d3244
 
 
 
 
 
 
 
 
 
 
 
 
58ebaff
a7d3244
 
 
58ebaff
 
 
a7d3244
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Author: @MarkK
# Date: 2023-03-18
# Description: A program that user can use for comfy interaction with the dataset (edit/add/delete).

import csv
import re
import sys
import os

# welcome message and list of available commands
print("""
 __          __  _                           
 \ \        / / | |                          
  \ \  /\  / /__| | ___ ___  _ __ ___   ___  
   \ \/  \/ / _ \ |/ __/ _ \| '_ ` _ \ / _ \ 
    \  /\  /  __/ | (_| (_) | | | | | |  __/ 
     \/  \/ \___|_|\___\___/|_| |_| |_|\___| 
                                                                                       
Welcome to Dataset-tool!
""")

print("Available commands:")
print("- del (delete the last row from 'dataset.csv')")
print("- showXXXX (print the XXXX row inside 'dataset.csv')")
print("- last (print the last row from 'dataset.csv')")
print("- set:XXXX (set EP_ID)")
print("Usage: Enter a multi-line text string + Hit Enter + Hit Ctrl+D to extract data and import it inside dataset")
print("Usage: Enter a command + Hit Enter + Hit Ctrl+D to execute command")
print()


# list of known speakers
known_speakers = (["All", "AngryCrowd", "Anchovy", "BarnacleBoy", "Both", "BubbleBass", "ClamHeadCandyCad", "Crowd", "Cowboy", "Clams",
                   "Customer", "Fred", "FlyingDutchman", "Fireman", "Gary", "Group", "guy", "Karen", "Kid", "Kids", "King", "Krabs", "Larry", "Loser", "MermaidMan",
                   "Montage", "MrsSquarePants", "MrSquarePants", "Morty", "MovieWoman", "Measurer", "Mother", "NematodeOne", "Nematodes", "NormanRockbass", "Narrator",
                   "OrangeCafeteriaWorker", "Octavius", "Pants", "Patrick", "PatTron", "PatrickBubble", "Phil", "Plankton", "Pearl",
                   "Puff", "Sandy", "Scooter", "ShadyShoalsCaretaker", "SpongeBob", "SpongeTron", "SpongeTrons", "SpongeBobBubble", "Voice", "Voices", "VolcanoSauceDrop",
                   "Squidward", "SquidwardBubble", "SwordfishTrucker", "TVAnnouncer", "TV", "TimeMachine", "UnknownElderlyFish"]
)
ep_id = "x"

# loop the program
while True:

    # take input from user
    print("Enter a text string or a command: ")
    input_string = sys.stdin.read()

    # check if input is a command
    if input_string.startswith("set"):
        index = input_string[4:]
        ep_id = index
        print("Set index: ", ep_id) 
    elif input_string.startswith("last"):
        try:
            with open("dataset.csv", mode="r") as file:
                lines = file.readlines()
                if len(lines) > 0:
                    row = lines[-1].strip().split(";")
                    print(f"Last row: {row[0]}: {row[1]}")
                else:
                    print("Error: dataset is empty.")
        except IOError:
            print("Error: could not open dataset.csv.")
    elif input_string.startswith("del"):
        try:
            with open("dataset.csv", mode="r") as file:
                lines = file.readlines()
                if len(lines) > 0:
                    row = lines[-1].strip().split(";")
                    with open("dataset.csv", mode="w") as file:
                        file.writelines(lines[:-1])
                    print(f"Deleted row: {row[0]}: {row[1]}")
                else:
                    print("Error: dataset is empty.")
        except IOError:
            print("Error: could not open dataset.csv.")
    elif input_string.startswith("show"):
        try:
            index = int(input_string[4:])
            with open("dataset.csv", mode="r") as file:
                reader = csv.reader(file, delimiter=";")
                rows = list(reader)
                if index >= 0 and index < len(rows):
                    row = rows[index]
                    print(f"Speaker: {row[0]}\nReplica: {row[1]}\n")
                else:
                    print("Error: invalid row index.")
        except ValueError:
            print("Error: invalid row index.")
        except IOError:
            print("Error: could not open dataset.csv.")
    else:
        num_lines = input_string.count('\n')
        if num_lines > 1:
            lines = input_string.split("\n")
            lines.pop()
            for line in lines:
                # extract speaker and replica from input string
                match = re.match(r"([A-Za-z ]+):\s*(.*)", line)
                if match:
                    speaker = match.group(1)
                    replica = match.group(2)
                    if speaker is None:
                        speaker = "{system}"
                    if speaker == "Krabs":
                        speaker = "Mr. Krabs"
                    if speaker == "Puff":
                        speaker = "Mrs. Puff"
                    if speaker == "MrsSquarePants":
                        speaker = "Mrs. SquarePants"
                    if speaker == "MrSquarePants":
                        speaker = "Mr. SquarePants"
                    if speaker == "driver":
                        speaker = "Bus driver"
                    if speaker == "Narrator":
                        speaker = "French Narrator"
                    if speaker == "guy":
                        speaker = "Sports guy"
                else:
                    match = re.search(r"I(\d+):\s*(.*)", line)
                    match2 = re.search(r"T(\d+):\s*(.*)", line)
                    if match:
                        speaker = f"Incidental {match.group(1)}"
                        replica = match.group(2)
                    elif match2:
                        speaker = f"Teen {match2.group(1)}"
                        replica = match2.group(2)
                    elif line.startswith("["):
                        speaker = "{system}"
                        replica = line
                    else:
                        print("Error: Could not extract speaker and replica from line.")
                        speaker = "Error: Could not extract speaker and replica from line."
                        replica = "Error: Could not extract speaker and replica from line."
                    
                # write to dataset.csv file
                try:
                    with open("dataset.csv", mode="a", newline="") as file:
                        writer = csv.writer(file, delimiter=";", quotechar=" ")
                        writer.writerow([speaker, replica, ep_id])

                    # print the result
                    print(f"Speaker: {speaker}\nReplica: {replica}\n")
                except PermissionError:
                    print("PermissionError while opening file.\n")
                except UnicodeEncodeError:
                    print("UnicodeEncodeError while opening file.\n")
                    speaker = "Error: UnicodeEncodeError while opening file."
                    replica = "Error: UnicodeEncodeError while opening file."
        else:
            # extract speaker and replica from input string
            match = re.match(r"([A-Za-z ]+):\s*(.*)", input_string)
            if match:
                speaker = match.group(1)
                replica = match.group(2)
                if speaker is None:
                    speaker = "{system}"
                if speaker == "Krabs":
                    speaker = "Mr. Krabs"
                if speaker == "Puff":
                    speaker = "Mrs. Puff"
                if speaker == "driver":
                    speaker = "Bus driver"
                if speaker == "Narrator":
                    speaker = "French Narrator"
                if speaker == "guy":
                    speaker = "Sports guy"
            else:
                match = re.search(r"I(\d+):\s*(.*)", input_string)
                match2 = re.search(r"T(\d+):\s*(.*)", input_string)
                if match:
                    speaker = f"Incidental {match.group(1)}"
                    replica = match.group(2)
                elif match2:
                    speaker = f"Teen {match2.group(1)}"
                    replica = match2.group(2)
                elif input_string.startswith("["):
                    speaker = "{system}"
                    replica = input_string
                else:
                    print("Error: Could not extract speaker and replica from input string.")
                    continue

            # write to dataset.csv file
            try:
                with open("dataset.csv", mode="a", newline="") as file:
                    writer = csv.writer(file, delimiter=";", quotechar=" ")
                    writer.writerow([speaker, replica, ep_id])

                # print the result
                print(f"Speaker: {speaker}\nReplica: {replica}\n")
            except PermissionError:
                print("PermissionError while opening file.\n")