Convert_word plugin

Posted by Khonsu on Wed 28 Jul 2021 06:04 PM — 4 posts, 16,922 views.

#0
Hello. I've been trying to get an old plugin I found here to work with "special" characters. In the MUD I play there's an area that has a type of hieroglyph that you have to learn to translate. Instead of using a key to manually translate each and every time I was hoping to have a script convert it.

I've been trying to use the Lua script found here:
http://www.gammon.com.au/forum/?id=8747&reply=4#reply4


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Convert_Word"
   author="Nick Gammon"
   id="9c0ed228bdb4a71c77e85ce0"
   language="Lua"
   purpose="Converts one word to another"
   date_written="2008-06-29"
   requires="4.00"
   version="1.0"
   >

</plugin>

<!--  Script  -->

<script>

conversions = {
  foo = "bar",
  dog = "cat",
  horse = "donkey",

  -- add more here
  }
  
function OnPluginPacketReceived (s)
  return (string.gsub (s, "%a+", conversions))
end -- function OnPluginPacketReceived
</script>

</muclient>


I would want \:\ to convert to the letter S, for example.

However the characters I want it to match are "symbols" like \:\ and -:-, etc. Every time I try to match these I get different types of errors when putting \:\ in quotes or double brackets [[]]. I don't know enough to escape the special characters or whatever needs to be done. Trying to put an extra \ before each symbol will throw another error as well.

An example error would be : [string "Plugin: Convert_Word"]:3: '}' expected (to close '{' at line 1) near '='

Any help with this would be greatly appreciated.
Australia Forum Administrator #1
First, the "key" (left side) has to be in brackets if it isn't a "word". And if it is a string it has to also be in quotes, for example:


conversions = {
  ["/:/"] = "a",
  ["-:-"] = "b",
  ["$:$"] = "c",

  -- add more here
  }


Second, your regular expression (the string.gsub) won't match because "%a" only matches letters. So you need a way of matching these hieroglyphs. For example, do they always have a colon in the middle? In which case you might match on ".:." instead of "%a+" (this is because a dot matches anything).

Third, I wouldn't do this in OnPluginPacketReceived because things that look like your hieroglyphs might show up elsewhere. It would probably be better to make an alias, and then copy the hieroglyph to the clipboard, and then get the alias to do the translation, when you want it to. eg.


translate /:/ -:- $:$

Alias responds:

a b c
Amended on Wed 28 Jul 2021 10:41 PM by Nick Gammon
#2
Thank you! This has helped me with figuring out how I can match these. I guess it was more of a problem with %a and the script was not getting alpha characters.. or something, pretty inexperienced here. Gonna try to look that up. Unluckily not all the hieroglyphs have a colon in the middle or are three characters, although they are either 2 or 3 and is an alphabet cipher so there's not too many different ones.

I believe I could write multiple functions to translate different ones perhaps. Going to fiddle with it more but I've at least got something working now.
Australia Forum Administrator #3

Are they separated by spaces? That would help.

Otherwise I would do something like this:

  • In a loop, get the first two characters
  • Look them up to see if they match a 2-character glyph
  • If a match, output that, remove 2 characters from the start of the string, and repeat (until none left)

Otherwise

  • Get the first three characters
  • Look them up to see if they match a 3-character glyph
  • If a match, output that, remove 3 characters from the start of the string, and repeat (until none left)

If no match still? Maybe skip one character and try again.