# 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", "Children", "Chorus", "Customers", "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", "PASystem", "Patrick", "PatTron", "PatrickBubble", "Phil", "Plankton", "Pearl", "Paper", "Puff", "Sandy", "Scooter", "ShadyShoalsCaretaker", "Soil", "SpongeBob", "SpongeTron", "SpongeTrons", "SpongeBobBubble", "Voice", "Voices", "VolcanoSauceDrop", "Squidward", "SquidwardBubble", "KingNeptune", "HotDogVendor", "Audience", "Harold", "Girls", "RealisticFishHead", "AtomicFlounder", "DirtyBubble", "Ivy", "Tom", "SwordfishTrucker", "TVAnnouncer", "TV", "TimeMachine", "UnknownElderlyFish", "WindowAttendant", "Everyone", "Eel", "Man", "MrsTentacles"] ) 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 == "MrsTentacles": speaker = "Mrs. Tentacles" 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")