Re: Censor

From Crys, 3 Weeks ago, written in Lua, viewed 13 times. This paste is a reply to Censor from Crys - view diff
URL https://paste.godclan.hu/view/61626b04 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.  
  58.   for _, message in ipairs(messages) do
  59.     print("Original:", message)
  60.     print("Censored:", censor(message))
  61.     print("================================")
  62.   end
  63.  
  64.   -- Output is this:
  65.   --[[
  66.     Original:   lorem
  67.     Censored:   lorem
  68.     ================================
  69.     Original:   lorem lofasz ipsum
  70.     Censored:   lorem lo**** ipsum
  71.     ================================
  72.     Original:   lorem ipsum kurva dolor
  73.     Censored:   lorem ipsum ***** dolor
  74.     ================================
  75.     Original:   lorem geci ipsum
  76.     Censored:   lorem **** ipsum
  77.     ================================
  78.     Original:   gecis lorem faSZszopó dolorem
  79.     Censored:   ****s lorem ********** dolorem
  80.     ================================
  81.     Original:   lorem ipsum KuRVa AnyÁd dolor sit amet
  82.     Censored:   lorem ipsum ***** AnyÁd dolor sit amet
  83.     ================================
  84.     Original:   KöcSöGLorem
  85.     Censored:   ********Lorem
  86.     ================================
  87.   ]]
  88. end

Replies to Re: Censor rss

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

Reply to "Re: Censor"

Here you can reply to the paste above