HELP! word replacing function.
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

HELP! word replacing function.

Started by BottledHate, 27 September, 2004, 01:41:15

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

BottledHate

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
Homepage: www.bottledhate.webhop.org

Compiling  Lua scripts is LAME!!!!!

BottledHate

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.
Homepage: www.bottledhate.webhop.org

Compiling  Lua scripts is LAME!!!!!

WickeD

-- 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

BottledHate

#3
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
Homepage: www.bottledhate.webhop.org

Compiling  Lua scripts is LAME!!!!!

nErBoS

#4
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.
--## nErBoS Spot ##--

BottledHate

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
Homepage: www.bottledhate.webhop.org

Compiling  Lua scripts is LAME!!!!!

nErBoS

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
--## nErBoS Spot ##--

BottledHate

i look forward to seeing what you come up with :)

-BH
Homepage: www.bottledhate.webhop.org

Compiling  Lua scripts is LAME!!!!!

bastya_elvtars

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 ;)
Everything could have been anything else and it would have just as much meaning.

BottledHate

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
Homepage: www.bottledhate.webhop.org

Compiling  Lua scripts is LAME!!!!!

SMF spam blocked by CleanTalk