PtokaX forum

Archive => Archived 4.0 boards => Help with Lua 4 scripts => Topic started by: BottledHate on 27 September, 2004, 01:41:15

Title: HELP! word replacing function.
Post by: BottledHate on 27 September, 2004, 01:41:15
okies.. this is from word replace bot... there are some requirements at the bottom... the function posted here currently works great.. it's just big and ugly and needs optimization.... anyone want to help?


--//been using this in titmouse for testing purposes.


string={sub = strsub, find = strfind, gsub = gsub, len = strlen, lower = strlower, type = type,format = format } --//backward compat for lua5 to 4.


rTable={
["word"] = "WWW",
["*hi"] = "XXX",
}
--// "*" in front of replace word means it will find only word alone. no "*" mean it will find it wherever it is.

doStuff = function(d)
   local reply = d
   r = nil
   for k,v in rTable do
      x = k
      if string.sub(x,1,1) =="*" then --// * means find word only.
         x = string.sub(k,2)
         reply = string.gsub(reply,"(%S+)",
            function(w)
               if string.lower(w) == x then
                  r = 1
                  return rTable["*"..x]
               else
                  return w
               end
            end)
      else --// no * means find word in word or alone...
         WRcnt = 0
         local m = gsub(string.lower(reply),"(%S+)",
            function (w)
               if string.find(w,x) then
                  WRcnt = WRcnt + 1
               end
            end)
         for y = -1,WRcnt do
            local s,e = nil,nil
            s,e = string.find(string.lower(reply), x)
            if s then
               r = 1
               reply = string.sub(reply,1,s-1)..v..string.sub(reply,e+1)
            end
         end
      end
   end
   if r then
      return reply
   end
end

data = "hI TesTinG TesTword woRd TesThi Hi hi"
print(doStuff(data))
--//output should look like: "XXX TesTinG TesTWWW WWW TesThi XXX XXX"
--//note: it only replaced "hi" if it's alone...
--//note: no change in other text CaSe (other text being none replaced text)...
--//note: if no words are found, no return.
--//note: must find word regardless of its CaSe

thanks for your time!!!

-BH
Title:
Post by: BottledHate on 28 September, 2004, 08:11:41
damn i'm about to unbottle the hate again.. i help so many people here.. and the time i ask for help... not a single reply.  THANKS.
Title: Try this one my m8!
Post by: WickeD on 28 September, 2004, 10:40:49

-- AntiLeet by Sedulus for DCH++ Original in Lua 5
-- modded to Lua 4 for use on Ptokax
-- by Typhoon?

tWords = {
["ne"] = "any",
["ne1"] = "anyone",
["vb"] = "visual basic",
["m8"] = "mate",
["ty"] = "thank you",
["wb"] = "welcome back",
["yr"] = "year",
["y"] = "why",
["u"] = "you",
["4"] = "for",
["dun"] = "don't",
["ur"] = "your",
["ure"] = "you're",
["coz"] = "because",
["wrk"] = "work",
["r"] = "are",
["cud"] = "could",
["dusnt"] = "doesn't",
["didnt"] = "didn't",
["luv"] = "love",
["wiv"] = "with",
["cudnt"] = "couldn't",
["ull"] = "you'll",
["urs"] = "yours",
["im"] = "I'm",
["sumtimes"] = "sometimes",
["dont"] = "don't",
["1ce"] = "once",
["cant"] = "can't",
["wuts"] = "what's",
["dem"] = "them",
   ["fuck"]= "havin sex",
   ["shit"] = "bleeeeeeeeh",
   ["m8"] = "mate",
   ["ass"] = "my hiny",
   ["gf"] = "girlfriend",
   ["asshole"] = "(place where dirty stuff exits the body)",
   ["dick"] = "male saucage",
   ["cum"] = "body cream",
   ["fucker"] = "I love you",
   ["fuckers"] = "I love you guy's",
   ["fucking"] = "doing dirty",
   ["bastard"] = "naughty boy",
   ["hell"] = "mac donalds",
   ["whore"] = "angel",
   ["yw"] = "your welcome",
   ["np"] = "no problem",
   ["q"] = "Question",
   ["thx"] = "thank you",
   ["afk"]= "away from the smassboard",
   ["lol"]= "Laughing Out Loud",
   ["afc"]= "Away for coffee",
}

function DataArrival(user,data )
replaced = nil
local newmsg = gsub(data,"(%w+)",
      function (word)
if tWords[strlower( word )] then
replaced = 1
return tWords[strlower( word )]
else
            return word
end
end)
if replaced then
SendToAll(" [deleeted] "..newmsg.."|")
return 1
end
end




//WickeD
Title:
Post by: BottledHate on 28 September, 2004, 14:18:20
nope... seee nit plenty of times. doesn't do everything.  read the requirements down at the bottom.

-BH

edit: in fact, if you look, you'll see that sed's word replace function is allready used partly... it just doesn't do enough... or isn't picky enough i guess...

-BH
Title:
Post by: nErBoS on 28 September, 2004, 15:05:38
Hi,

Your code seems fine to me, the only thiing that you should do is to change (%S+) for (%w+) like the script of Sedulus (this because %S+ can't catch the last hi).

But here you have another apraoch for your do stuff..

doStuff = function(d)
local word,replace,sWord
for word, replace in rTable do
i = string.len(word)
if (string.sub(word,1,1) == "\*") then
sWord = string.sub(word,2,string.len(word))
d = string.gsub(d,"(%w+)",
function(w)
if (rTable["\*"..string.lower(w)] ~= nil) then
return rTable["\*"..string.lower(w)]
else
return w
end
end)
else
d = string.gsub(d,"(%w+)",
function(w)
local pos,len,reply = 0,string.len(w),w
while (len+1 > pos+i) do
local index = string.lower(string.sub(w,pos+1,pos+i))
if (rTable[index] ~= nil) then
reply = string.sub(w,0,pos)..rTable[index]..string.sub(w,i+pos+1,len)
break
end
pos = pos + 1
end
return reply
end)
end
end
return d
end

Best regards, nErBoS

P.S - Watch out the comparing of *, as you now its a magical char, so you should use \* or the ascii code to compare it, its more safe.
Title:
Post by: BottledHate on 29 September, 2004, 02:40:02
not quite nerbos... close though....

string={sub = strsub, find = strfind, gsub = gsub, len = strlen, lower = strlower, type = type,format = format }

rTable={
["word"] = "wEeE",
   ["*hi"] = "SSS"
}

--//nerbos's function....\\--

doStuff = function(d)
local word,replace,sWord
for word, replace in rTable do
i = string.len(word)
if (string.sub(word,1,1) == "\*") then
sWord = string.sub(word,2,string.len(word))
d = string.gsub(d,"(%w+)",
function(w)
if (rTable["\*"..string.lower(w)] ~= nil) then
return rTable["\*"..string.lower(w)]
else
return w
end
end)
else
d = string.gsub(d,"(%w+)",
function(w)
local pos,len,reply = 0,string.len(w),w
while (len+1 > pos+i) do
local index = string.lower(string.sub(w,pos+1,pos+i))
if (rTable[index] ~= nil) then
reply = string.sub(w,0,pos)..rTable[index]..string.sub(w,i+pos+1,len)
break
end
pos = pos + 1
end
return reply
end)
end
end
return d
end
data = "hI TestIng TesTinGwordwordhiword TesThi Hi hi"
data2 = "nothing to find here :) so no return."
print("data:")
print(doStuff(data))
print("data2:")
print(doStuff(data2))
your output was: SSS TestIng TesTinGwEeEwordhiword TesThi SSS SSS
should've  been : SSS TestIng TesTinGwEeEwEeEhiwEeE TesThi SSS SSS

also.. it returned the data2.. which it shouldn't. return nil on no replace.....

thanks so much for your input and help!!!!

here's another variation plop and i came up with...
doWowMoreStuffToDo = function(d)
   rW = nil
   d = string.gsub(d,
      "(%S+)",
      function(w)
         local u = string.lower(w)
         if rTable["*"..u] then
         rW = 1
            return rTable["*"..u]
         else
            return w
         end
      end)
      for k,v in rTable do
         x = k
         WRcnt = 0
         local m = gsub(string.lower(d),"(%S+)",
            function (w)
               if string.find(w,x) then
                  WRcnt = WRcnt + 1
               end
            end)
         for y = -1,WRcnt do
            local s,e = nil,nil
            s,e = string.find(string.lower(d), x)
            if s then
               rW = 1
               d = string.sub(d,1,s-1)..v..string.sub(d,e+1)
            end
         end
      end
   if rW then
      return d
   end
end

it works great too.. but still big and ugly imho ;)  i'm sure we can make it smaller...... :D (without loosing any of the functionality.)

'%S+' works in every test i've thrown at it. and i beleive '*' is only special when used in pattern matching... nothing special about a '*' in a regular string(as far as i've seen). or at least not the way i'm using it...

thanks again for your time, help, and input!


-BH
Title:
Post by: nErBoS on 29 September, 2004, 03:33:51
Hi,

My matematic fails. When i have a little more time i will have a better look and see if i can make it pretty  :))

About the "%S+", i have tried and missed on the last word. I don't remenber the case  :P

Best regards, nErBoS
Title:
Post by: BottledHate on 29 September, 2004, 10:33:26
i look forward to seeing what you come up with :)

-BH
Title:
Post by: bastya_elvtars on 29 September, 2004, 11:46:00
Quote--//output should look like: "XXX TesTinG TesTWWW WWW TesThi XXX XXX"
--//note: it only replaced "hi" if it's alone...
--//note: no change in other text CaSe (other text being none replaced text)...
--//note: if no words are found, no return.
--//note: must find word regardless of its CaSe

i do not clearly understand yout point. i executed this, and got the output:

QuoteXXX TesTinG TesTWWW WWW TesThi XXX XXX

please let me know what you exactly want ;)
Title:
Post by: BottledHate on 29 September, 2004, 21:18:52
QuoteOriginally posted by bastya_elvtars
i do not clearly understand yout point. i executed this, and got the output:

please let me know what you exactly want ;)

QuoteOriginally posted by BottledHate
it works great too.. but still big and ugly imho ;) i'm sure we can make it smaller...... :D (without loosing any of the functionality.)

QuoteOriginally posted by BottledHate
... the function posted here currently works great.. it's just big and ugly and needs optimization.... anyone want to help?

i thought it was pretty clear what i was up to... sorry..

-BH