move the case-ref analysis to a helper

This commit is contained in:
InspectorCaracal 2022-07-06 15:37:52 -06:00
parent 8fd19928b2
commit 169ec4fa59

View file

@ -235,6 +235,26 @@ class RecogError(Exception):
class LanguageError(Exception):
pass
def _get_case_ref(string):
"""
Helper function which parses capitalization and
returns the appropriate case-ref character for emotes.
"""
# default to retaining the original case
case = "~"
# internal flags for the case used for the original /query
# - t for titled input (like /Name)
# - ^ for all upercase input (like /NAME)
# - v for lower-case input (like /name)
# - ~ for mixed case input (like /nAmE)
if string.istitle():
case = "t"
elif string.isupper():
case = "^"
elif string.islower():
case = "v"
return case
# emoting mechanisms
def parse_language(speaker, emote):
@ -368,22 +388,7 @@ def parse_sdescs_and_recogs(sender, candidates, string, search_mode=False, case_
# first, find and replace any self-refs
for self_match in list(_RE_SELF_REF.finditer(string)):
matched = self_match.group()
case = "~" # retain original case of sdesc
if case_sensitive:
# case sensitive mode
# internal flags for the case used for the original /query
# - t for titled input (like /Name)
# - ^ for all upercase input (like /NAME)
# - v for lower-case input (like /name)
# - ~ for mixed case input (like /nAmE)
matchtext = matched.lstrip(_PREFIX)
if matchtext.istitle():
case = "t"
elif matchtext.isupper():
case = "^"
elif matchtext.islower():
case = "v"
case = _get_case_ref(matched.lstrip(_PREFIX)) if case_sensitive else "~"
key = f"#{sender.id}{case}"
# replaced with ref
string = string.replace(matched,f"{{{key}}}")
@ -481,24 +486,9 @@ def parse_sdescs_and_recogs(sender, candidates, string, search_mode=False, case_
errors.append(_EMOTE_NOMATCH_ERROR.format(ref=marker_match.group()))
elif nmatches == 1:
# a unique match - parse into intermediary representation
case = "~" # retain original case of sdesc
if case_sensitive:
# case sensitive mode
# internal flags for the case used for the original /query
# - t for titled input (like /Name)
# - ^ for all upercase input (like /NAME)
# - v for lower-case input (like /name)
# - ~ for mixed case input (like /nAmE)
matchtext = marker_match.group().lstrip(_PREFIX)
if matchtext.istitle():
case = "t"
elif matchtext.isupper():
case = "^"
elif matchtext.islower():
case = "v"
key = f"#{obj.id}{case}"
case = _get_case_ref(marker_match.group()) if case_sensitive else "~"
# recombine emote with matched text replaced by ref
key = f"#{obj.id}{case}"
string = f"{head}{{{key}}}{tail}"
mapping[key] = obj