Swap words I type in chat as custom addon

Is there a way to make a custom addon that simply swaps entered words before saying them in chat (it’s ok if it does it for all types of chat as long as it works)? The reason I ask is because I want to make it easier/automatic for me to speak like a dwarf for RP reason, so for example, if I type in “your” I want it to say “yer” instead. I want that to be what is actually said instead, so that everyone can see it. I tried the addon “Tongues” but it’s bugging out on me, not sure if it’s because i’m an allied race. Regardless, I would really appreciate it if someone could help me out with this, the basic working code, and then I will add the list of words that I need myself, like “cannot” or “can’t” to “cannae” , etc.

This is a very old addon, but seems to have the functionality you are looking for. I have not used it, nor had a chance to examine the code, so hopefully helpful.

wowinterface PirateSpeak
(Sorry, I can’t post links for some reason)

1 Like

If it were me, I’d start with Misspelled and replace the dictionary with one that matches what you’re doing.

That seems like the simplest way to create a “dialect” version of the language you’re using.

(Sorry, it wouldn’t let me post again so I tried deleting my previous post. I had to switch to another toon.)

I just have a question about the code.

For example, this will replace the words on the left with the word on the right:
{o={" man"," boy"," guy"}, r={" lad"}},

Notice how I had to put a space in front of each word so that it won’t, for example, change “fireman” to “firelad”. The space is my solution to this problem. However, if I type “man” as the first word of my sentence, it won’t convert it because there is no space before it. Is there something I can put where the space would be to indicate a word being the first word in a sentence? I tried putting “^man” but then the addon stopped working/wouldn’t work at all.

If it’s doing a regex replace you could try \bman\b

[added]
Looks like there’s no word boundary char in the lua text substitutions
https://www.lua.org/manual/5.3/manual.html#6.4.1

[added-2]
This might help
https://stackoverflow.com/questions/32852605/how-to-check-if-a-word-appears-as-a-whole-word-in-a-string-in-lua

I don’t know the code but you will probably need a double entry if you want to do complicated replaces (lua does pattern matching, not full regex)
{o={" man"," boy"," guy"}, r={" lad"}},

and another for the beginning of lines (everything is case sensitive so man is not equal to Man)

{o={"^ -Man"}, r={"Lad"}},

I’ve a bit of experience processing natural language and there are some tricks.

First, it’s simpler to compare and search with both the target and object strings converted to lower case and then examine the original afterwards to see if you need to make the first letter of the resulting value capitalized. That gets around the double-entry issue Fizzie mentioned.

Second, the dictionary coding documentation in that addon I referenced is pretty straightforward (albeit somewhat technical) and 99% of the problems you’re going to face are in already handled in that addon.

Essentially, all you need to do is find the words you want to alter in there and replace the output with your versions.

can’s negation suffix would become -nae for instance (actually probably the entirity of the -n’t suffix would be -nae or -na.

It’s certainly a shorter path to completion than writing your own code IMHO.

Parsing by word is not that tough. The following is a pure-Lua (versus WoW-Lua) sample of how to do it.

It’s not written particularly efficiently. I just sort of whipped it up for this post in a few minutes, but it does work and it does nominally output an arbitrary string as a table of words in the correct sequence with both an “as written” and “lower case” version of each word.

local sentence = "This is a test paragraph. It has multiple sentences in it, including sentences with punctuation in the middle."

function TrimSpacesNewLineCarriageReturn(inString)
--    Assumes that "inString" has already been vetted as a string

    local s	 = inString
    local l1 = string.len(s)
    local l2 = 0
    while (l1 > 0 and not (l1 == l2)) do
        l2 = string.len(s)
        s  = string.gsub(string.gsub(s, "^%s*", ""), "%s*$", "")
        s  = string.gsub(string.gsub(s, "^\n*", ""), "\n*$", "")
        s  = string.gsub(string.gsub(s, "^\r*", ""), "\r*$", "")
        l1 = string.len(s)
    end
    return s

end

function WordParseTable(inString)

    local s
    local t = {}
    if not (type(inString) == "string") then
        return t
    else
        s = TrimSpacesNewLineCarriageReturn(inString)
    end
    while string.len(s) > 0 do
        local w = string.match(s, "^%S*")
        s = TrimSpacesNewLineCarriageReturn(string.sub(s, string.len(w) + 1))
        table.insert(t, (string.gsub(w, "%W", "")))
    end
    return t

end

local wordTable = WordParseTable(sentence)

for k, v in ipairs(wordTable) do
    print(k, v, string.lower(v))
end

Thanks a lot everyone.