Censor

From Crys, 3 Weeks ago, written in Lua, viewed 14 times.
URL https://paste.godclan.hu/view/2c1d61d9 Embed
Download Paste or View Raw
  1. -- censor function
  2. do
  3.   local words = {
  4.     "fasz",
  5.     "faSzszopó",
  6.     "kurva",
  7.     "geci",
  8.     "anyád",
  9.     "köcsög"
  10.   }
  11.  
  12.   local stringImpl = utf8 or string
  13.   local REPLACEMENT_CHAR = "*"
  14.  
  15.   function censor(input)
  16.     if type(input) ~= "string" then
  17.       return input
  18.     end
  19.    
  20.     local output = input
  21.     local haystack = stringImpl.lower(input)
  22.    
  23.     for _, word in ipairs(words) do
  24.       word = stringImpl.lower(word)
  25.      
  26.       local replacement = string.rep(REPLACEMENT_CHAR, stringImpl.len(word))
  27.       local start = 1
  28.          
  29.       while true do
  30.         local i, j = stringImpl.find(haystack, word, start, true)
  31.  
  32.         if i == nil then
  33.           break
  34.         end
  35.        
  36.         start = j + 1
  37.  
  38.         output = stringImpl.sub(output, 1, i - 1) .. replacement .. stringImpl.sub(output, start)
  39.       end
  40.     end
  41.  
  42.     return output
  43.   end
  44. end
  45.  
  46. -- test
  47. do
  48.   local messages = {
  49.     "lorem",
  50.     "lorem lofasz ipsum",
  51.     "lorem ipsum kurva dolor",
  52.     "lorem geci ipsum",
  53.     "gecis lorem faSZszopó dolorem",
  54.     "lorem ipsum KuRVa AnyÁd dolor sit amet",
  55.     "KöcSöGLorem"
  56.   }
  57.   for _, message in ipairs(messages) do
  58.     print("Original:", message)
  59.     print("Censored:", censor(message))
  60.     print("================================")
  61.   end
  62. end

Replies to Censor rss

Title Name Language When
Re: Censor Crys lua 3 Weeks ago.

Reply to "Censor"

Here you can reply to the paste above