Created
January 5, 2024 02:44
-
-
Save Bluscream/0bbb80b6d44750b4bf43301ecf9702ba to your computer and use it in GitHub Desktop.
CrocDB language token modifier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
from random import choice, randint, sample | |
from typing import Any | |
from sqlite3 import connect | |
funny_names = ["Bluscream","Your mom","Joe Mama", "Stinky Frank"] | |
db_file = "C:/Users/blusc/Desktop/croc/CrocLOTG/croc.db" | |
input_csv = "translate/input.csv" | |
output_csv = "translate/out/replaced.csv" | |
def reverse(input: str): return ''.join(reversed(input)) | |
class Credits: | |
valid: bool = False | |
raw: str | |
y: int # Y | |
titles: list[str] # T | |
people: list[str] # A | |
w: int # W | |
def peoples_str(self) -> str: return ';'.join(['A' + p for p in self.people]) | |
def titles_str(self) -> str: return ';'.join(['T' + t for t in self.titles]) | |
def __init__(self, text: str): | |
self.raw = text | |
content = text.split(';') | |
_len = len(content) | |
if _len < 2: return | |
self.y = content[0][1:] | |
self.titles = [t[1:] for t in content if t.startswith('T')] | |
self.people = [p[1:] for p in content[2:-1]] | |
self.w = content[:1][0][1:] | |
self.valid = True | |
def __str__(self) -> str: | |
if hasattr(self, 'y'): | |
ret = [] | |
if self.y: ret.append(f"Y{self.y}") | |
if self.titles: ret.append(self.titles_str()) | |
if self.people: ret.append(self.peoples_str()) | |
if self.w: ret.append(f"W{self.w}") | |
return ';'.join(ret) | |
return self.raw | |
@classmethod | |
def new(cls, y: int = 12, titles: list[str] = ["Title"], people: list[str] = ["Person 1", "Person 2"], w: int = 3): | |
self = cls.__new__(cls) | |
self.y = y | |
self.titles = titles | |
self.people = people | |
self.w = w | |
self.valid = True | |
return self | |
@classmethod | |
def e(cls): | |
self = cls.__new__(cls) | |
self.raw = "E" | |
self.valid = False | |
return self | |
@classmethod | |
def q(cls): | |
self = cls.__new__(cls) | |
self.raw = "Q" | |
self.valid = False | |
return self | |
@classmethod | |
def isCredit(cls, key: str) -> bool: return key.startswith('Credits') and not key == 'Credits' | |
def replaceNames(self, replacements: list[str]): | |
if not self.valid: | |
print("replaceNames: Credits not valid") | |
return | |
for i, v in enumerate(self.people): | |
self.people[i] = choice(replacements) | |
class LangTokens: | |
credits: list[Credits | str] = [] | |
titles: dict[str, str] = {} | |
inputs: dict[str, str] = {} | |
others: dict[str, Any] = {} | |
def __init__(self, rows: dict[str, Any]): | |
credit_vars = [] | |
for key, val in rows.items(): | |
if Credits.isCredit(key): | |
credits = Credits(val) | |
self.credits.append(credits) | |
if not credits.valid: continue | |
credit_vars += credits.titles | |
print(f"Credit vars: {credit_vars}") | |
for key, val in rows.items(): | |
if key in credit_vars: self.titles[key] = val | |
elif Credits.isCredit(key): pass | |
elif key.startswith("INPUT2_"): | |
self.inputs[key] = val | |
else: self.others[key] = val | |
def toDict(self) -> dict[str, Any]: | |
ret = {} | |
for i, credit in enumerate(self.credits): | |
ret[f"Credits{str(i).zfill(2)}"] = str(credit) | |
for key, val in self.titles.items(): | |
ret[key] = val | |
for key, val in self.inputs.items(): | |
ret[key] = val | |
for key, val in self.others.items(): | |
ret[key] = val | |
return ret | |
def rewrite_credits(tokens: LangTokens): | |
tokens.credits = [] | |
for i in range(0, 98): | |
if randint(0, 50) == 0: | |
tokens.credits.append(Credits.e()) | |
else: | |
ppl = [] | |
for j in range(0, randint(1, 5)): | |
ppl.append(choice(funny_names)) | |
tokens.credits.append(Credits.new(12, [f"Title {i}"], ppl, 1)) | |
tokens.credits.append(Credits.q()) | |
with open(input_csv, "r") as file: | |
reader = csv.DictReader(file, delimiter=';') | |
header = next(reader) | |
rows = list(reader) | |
keyname = "token" | |
valname = "value" | |
dct = {} | |
for row in rows: | |
dct[row[keyname]] = row[valname] | |
tokens = LangTokens(dct) | |
# region edit | |
for name in funny_names: | |
tokens.others[name] = reverse(name) | |
for i, credit in enumerate(tokens.credits): | |
if not credit.valid or not credit.people: continue | |
credit.people += sample(funny_names, len(funny_names)) | |
# for i, name in enumerate(credit.people): | |
# credit.people[i] = reverse(name) | |
for key, val in tokens.others.items(): | |
tokens.others[key] = (val.lower()) | |
# endregion edit | |
outDict = tokens.toDict() | |
with open(output_csv, "w", newline='') as outFile: | |
writer = csv.DictWriter(outFile, fieldnames=header, delimiter=';') | |
print(writer.fieldnames) | |
writer.writeheader() | |
for k, v in outDict.items(): | |
newrow = {keyname: k, valname: v} | |
print(f"Writing {newrow}") | |
writer.writerow(newrow) | |
print("done") | |
with connect(db_file) as db: | |
cursor = db.cursor() | |
cursor.execute("DROP TABLE IF EXISTS LangTokens") | |
cmd = f"CREATE TABLE LangTokens ({keyname} TEXT, {valname} TEXT)" | |
print(cmd) | |
cursor.execute(cmd) | |
for k, v in outDict.items(): | |
cursor.execute(f"INSERT INTO LangTokens ({keyname}, {valname}) VALUES (?, ?)", (k, v)) | |
print("file done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment