ULX

Author Topic: Some table management mishaps  (Read 265 times)

0 Members and 1 Guest are viewing this topic.

Offline BlueNova

  • Full Member
  • ***
  • Posts: 108
  • Karma: 11
  • The most powerful force in the universe.
Some table management mishaps
« on: March 22, 2019, 12:03:58 PM »
So I've been writing a simple word/slur filter for a plugin I'm making with just custom lua I write as a collection of that sort of thing. And when I got to writing the word/slur/chat filter I found a weird bug.

It seems to only want to filter a set amount. For example if I have it filter out the word "cheese" then if I type cheese once it'll filter it out fine, if I do it again it'll work but if I type cheese 3 or more times it won't grab all of them, only the first two instances, it's weird.

Here's the logic behind it.

Code: Lua
  1. local message = string.Split(text, " ") --Text comes from a PlayerSay hook
  2.  
  3. for _, word in pairs(message) do
  4.    local prev_word = word
  5.    word = string.gsub(word, "[%p]", "")
  6.  
  7.    if badWords[string.lower(word)] then
  8.       table.insert(message, getIndex(message, prev_word), cleanWords[math.random(#cleanWords)])
  9.       table.remove(message, getIndex(message, prev_word))
  10.    end
  11. end

Thinking I was missing something that a second pair of eyes could point me towards, if you need more just let me know.
Thanks

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2680
  • Karma: 417
    • |G4P| Gman4President
Re: Some table management mishaps
« Reply #1 on: March 22, 2019, 01:20:17 PM »
The way you are doing this is the 'hard' way.

in this method, it will only match exact strings of words.. so to use your 'cheese' example...

I like cheese   --->> "I like"

But you can get around it easily by cramming words together.

I like cheese.  --->> "I like cheese."

since 'cheese.' != 'cheese'

The way I would do it is reverse.


Instead of making a table of words in your sentence and then checking each against your table of bad words.. why don't you iterate through your table of bad words and check against the entire string of your sentence.

Offline BlueNova

  • Full Member
  • ***
  • Posts: 108
  • Karma: 11
  • The most powerful force in the universe.
Re: Some table management mishaps
« Reply #2 on: March 22, 2019, 02:21:16 PM »
The way you are doing this is the 'hard' way.

in this method, it will only match exact strings of words.. so to use your 'cheese' example...

I like cheese   --->> "I like"

But you can get around it easily by cramming words together.

I like cheese.  --->> "I like cheese."

since 'cheese.' != 'cheese'

The way I would do it is reverse.


Instead of making a table of words in your sentence and then checking each against your table of bad words.. why don't you iterate through your table of bad words and check against the entire string of your sentence.


Hadn't thought of that, I'mma try it