2021-05-07 20:32:10 +02:00
|
|
|
|
from threading import Timer
|
2021-05-11 01:17:10 +02:00
|
|
|
|
import re
|
2021-05-07 20:32:10 +02:00
|
|
|
|
|
2022-02-12 19:23:59 +01:00
|
|
|
|
vars = None
|
|
|
|
|
|
2021-05-11 01:17:10 +02:00
|
|
|
|
#==================================================================#
|
|
|
|
|
# Decorator to prevent a function's actions from being run until
|
|
|
|
|
# at least x seconds have passed without the function being called
|
|
|
|
|
#==================================================================#
|
2021-05-07 20:32:10 +02:00
|
|
|
|
def debounce(wait):
|
|
|
|
|
def decorator(fun):
|
|
|
|
|
def debounced(*args, **kwargs):
|
|
|
|
|
def call_it():
|
|
|
|
|
fun(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
debounced.t.cancel()
|
|
|
|
|
except AttributeError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
debounced.t = Timer(wait, call_it)
|
|
|
|
|
debounced.t.start()
|
|
|
|
|
|
|
|
|
|
return debounced
|
|
|
|
|
|
2021-05-11 01:17:10 +02:00
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
#==================================================================#
|
|
|
|
|
# Replace fancy quotes and apostrope's with standard ones
|
|
|
|
|
#==================================================================#
|
|
|
|
|
def fixquotes(txt):
|
|
|
|
|
txt = txt.replace("“", '"')
|
|
|
|
|
txt = txt.replace("”", '"')
|
|
|
|
|
txt = txt.replace("’", "'")
|
|
|
|
|
txt = txt.replace("`", "'")
|
|
|
|
|
return txt
|
|
|
|
|
|
|
|
|
|
#==================================================================#
|
|
|
|
|
#
|
|
|
|
|
#==================================================================#
|
|
|
|
|
def trimincompletesentence(txt):
|
|
|
|
|
# Cache length of text
|
|
|
|
|
ln = len(txt)
|
|
|
|
|
# Find last instance of punctuation (Borrowed from Clover-Edition by cloveranon)
|
|
|
|
|
lastpunc = max(txt.rfind("."), txt.rfind("!"), txt.rfind("?"))
|
|
|
|
|
# Is this the end of a quote?
|
|
|
|
|
if(lastpunc < ln-1):
|
|
|
|
|
if(txt[lastpunc+1] == '"'):
|
|
|
|
|
lastpunc = lastpunc + 1
|
|
|
|
|
if(lastpunc >= 0):
|
|
|
|
|
txt = txt[:lastpunc+1]
|
|
|
|
|
return txt
|
|
|
|
|
|
|
|
|
|
#==================================================================#
|
|
|
|
|
#
|
|
|
|
|
#==================================================================#
|
|
|
|
|
def replaceblanklines(txt):
|
|
|
|
|
txt = txt.replace("\n\n", "\n")
|
|
|
|
|
return txt
|
|
|
|
|
|
|
|
|
|
#==================================================================#
|
|
|
|
|
#
|
|
|
|
|
#==================================================================#
|
2021-08-19 13:18:01 +02:00
|
|
|
|
def removespecialchars(txt, vars=None):
|
|
|
|
|
if vars is None or vars.actionmode == 0:
|
|
|
|
|
txt = re.sub(r"[#/@%<>{}+=~|\^]", "", txt)
|
|
|
|
|
else:
|
|
|
|
|
txt = re.sub(r"[#/@%{}+=~|\^]", "", txt)
|
2021-05-11 01:17:10 +02:00
|
|
|
|
return txt
|
|
|
|
|
|
|
|
|
|
#==================================================================#
|
|
|
|
|
# If the next action follows a sentence closure, add a space
|
|
|
|
|
#==================================================================#
|
2021-05-14 08:24:05 +02:00
|
|
|
|
def addsentencespacing(txt, vars):
|
2021-05-11 01:17:10 +02:00
|
|
|
|
# Get last character of last action
|
2021-05-14 08:24:05 +02:00
|
|
|
|
if(len(vars.actions) > 0):
|
2021-08-29 00:54:10 +02:00
|
|
|
|
if(len(vars.actions[vars.actions.get_last_key()]) > 0):
|
2021-12-27 00:29:54 +01:00
|
|
|
|
action = vars.actions[vars.actions.get_last_key()]
|
|
|
|
|
lastchar = action[-1] if len(action) else ""
|
2021-05-18 23:59:59 +02:00
|
|
|
|
else:
|
|
|
|
|
# Last action is blank, this should never happen, but
|
|
|
|
|
# since it did let's bail out.
|
|
|
|
|
return txt
|
2021-05-14 08:24:05 +02:00
|
|
|
|
else:
|
2021-12-27 00:29:54 +01:00
|
|
|
|
action = vars.prompt
|
|
|
|
|
lastchar = action[-1] if len(action) else ""
|
2021-05-13 07:26:42 +02:00
|
|
|
|
if(lastchar == "." or lastchar == "!" or lastchar == "?" or lastchar == "," or lastchar == ";" or lastchar == ":"):
|
2021-05-11 01:17:10 +02:00
|
|
|
|
txt = " " + txt
|
|
|
|
|
return txt
|
2021-10-23 17:30:48 +02:00
|
|
|
|
|
|
|
|
|
def singlelineprocessing(txt, vars):
|
|
|
|
|
txt = vars.regex_sl.sub('', txt)
|
|
|
|
|
if(len(vars.actions) > 0):
|
|
|
|
|
if(len(vars.actions[vars.actions.get_last_key()]) > 0):
|
2021-12-27 00:29:54 +01:00
|
|
|
|
action = vars.actions[vars.actions.get_last_key()]
|
|
|
|
|
lastchar = action[-1] if len(action) else ""
|
2021-10-23 17:30:48 +02:00
|
|
|
|
else:
|
|
|
|
|
# Last action is blank, this should never happen, but
|
|
|
|
|
# since it did let's bail out.
|
|
|
|
|
return txt
|
|
|
|
|
else:
|
2021-12-27 00:29:54 +01:00
|
|
|
|
action = vars.prompt
|
|
|
|
|
lastchar = action[-1] if len(action) else ""
|
2021-10-23 17:30:48 +02:00
|
|
|
|
if(lastchar != "\n"):
|
|
|
|
|
txt = txt + "\n"
|
|
|
|
|
return txt
|
2021-05-22 11:28:40 +02:00
|
|
|
|
|
|
|
|
|
#==================================================================#
|
|
|
|
|
# Cleans string for use in file name
|
|
|
|
|
#==================================================================#
|
|
|
|
|
def cleanfilename(filename):
|
2021-09-01 00:22:30 +02:00
|
|
|
|
filteredcharacters = ('/','\\')
|
|
|
|
|
filename = "".join(c for c in filename if c not in filteredcharacters).rstrip()
|
2021-05-22 11:28:40 +02:00
|
|
|
|
return filename
|
2021-05-11 01:17:10 +02:00
|
|
|
|
|
2022-02-12 19:23:59 +01:00
|
|
|
|
#==================================================================#
|
|
|
|
|
# Newline substitution for fairseq models
|
|
|
|
|
#==================================================================#
|
|
|
|
|
def encodenewlines(txt):
|
|
|
|
|
if(vars.newlinemode == "s"):
|
|
|
|
|
return txt.replace('\n', "</s>")
|
|
|
|
|
return txt
|
|
|
|
|
|
|
|
|
|
def decodenewlines(txt):
|
|
|
|
|
if(vars.newlinemode == "s"):
|
|
|
|
|
return txt.replace("</s>", '\n')
|
|
|
|
|
return txt
|