![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | twi |
Hello, I’m writing a script that will allow strings to refer properly to a custom player character or something of that nature (chiefly, implementing pronouns and the grammatical rules to go along with them). Right now the issue I’m having trouble with is how I would go about dynamically capitalizing {placeholder}s in accordance with their context within a string (the most obvious example being when they appear at the beginning of a sentence or string).
Oversimplified example:
var testcase = "Susie is a {species}. {species}s have soft fur and four legs."
print(testcase.format({"species" : "cat"})
naïve output: Susie is a cat. cats have soft fur and four legs.
By now I’ve designed a function which in its current state looks like this:
assert(typeof(a) == TYPE_STRING)
var i : int = 0 # Character pointer
var s = ""
var t = ""
var skip = []
var res = a
while i < res.find_last("{"):
var cap = false
# Locate and store the placeholder
i = res.find("{",0)
while i in skip:
i = res.find("{",i+1)
s = res.substr(i, (res.find("}",i+1) - i) + 1)
match s:
"{NAME}": t = pcname # yes this is against style; i'm not bothered
"{THEY}" : t = set[prns.SUB] # "set" is the selected array containing the pronouns themselves
"{THEM}" : t = set[prns.OBJ] # and "prns" is an enum defining the indices of each class of pronoun.
"{THEIR}" : t = set[prns.POS] # This structure can present its own problems but it's what I have right now
"{THEIRS}" : t = set[prns.IPOS] # Placeholder name differs from abbreviated technical name of pronoun
"{THEYRE}" : t = set[prns.PRES] # so as to be more natural to type in a sentence
"{THEYVE}" : t = set[prns.HAVE]
"{IS}" : t = set[prns.IS] # Singular "they" requires this
"{S}" : t = set[prns.S] # For grammatical reasons
"{ES}" : t = set[prns.ES] # Also for grammatical reasons
"{N}" : t = set[prns.N] # Ditto
_ : # placeholder not found
skip.append(i)
t = s
if i == 0:
cap = true
elif res[i-2] == "." and res[i-1] == " ":
cap = true
t = t.capitalize() if cap else t
res.erase(i, (res.find("}",i+1) - i) + 1)
res = res.insert(i,t)
return(res)
Forgive my beginner programming skills but I can’t help but feel like there’s a better or more efficient way to do this. This doesn’t seem like that out-there of a use case so maybe I’m missing something obvious? Yet I can’t find any questions or instruction relating to this online.
EDIT: I’ve made a few fixes which should prevent improper capitalization or hanging given the wrong string but my question still stands.