简体   繁体   中英

how do i make python find words that look similar to a bad word, but not necessarily a proper word in english?

I'm making a cyberbullying detection discord bot in python, but sadly there are some people who may find their way around conventional English and spell a bad word in a different manner, like the n-word with 3 g's or the f word without the c. There are just too many variants of bad words some people may use. How can I make python find them all?

I've tried pyenchant but it doesn't do what I want it to do. If I put suggest("racist slur"), "sucker" is in the array. I can't seem to find anything that works.

Will I have to consider every possibility separately and add all the possibilities into a single dictionary? (I hope not.)

It's not necessarily python's job to do the heavy lifting but rather its ecosystem. You may want to look into Natural Language Understanding algorithms and find a way that suits your specific needs. This takes some time and further expertise to figure out.

You may want to start with pytorch, it has helped my learning curve a lot. Their docs regarding text: https://pytorch.org/text/stable/index.html

Also, I'd suggest, you have a look around at kaggle, several datascience challenges have a prize on them to tackle the same task you are aiming to solve. https://www.kaggle.com/c/jigsaw-multilingual-toxic-comment-classification

These competitions usually have public starter notebooks to get you started with your own implementation.

You could try looping through the string that you are moderating and putting it into an array. For example, if you wanted to blacklist "foo"

x=[["f","o","o"],[" "], ["f","o","o","o"]]

then count the letters in each word to count how many of each letter is in each word:

y = [["f":"1", "o":"2"], [" ":"1"], ["f":"1", "o":"3"]]

then see that y[2] is very similar to y[0] (the banned word). While this method is not perfect, it is a start.

Another thing to look in to is using a neural language interpreter that detects if a word is being used in a derogatory way. A while back, Google designed one of these.

The other answer is just that no bot is perfect. You might just have to put these common misspellings in the blacklist. However, the automatic approach would be awesome if you got it working with 100% accuracy.

Unfortunately, spell checking (for different languages) alone is still an open problem that people do research on, so there is no perfect solution for this, let alone for the case when the user intentionally tries to insert some "errors".

Fortunately, there is a conceptually limited number of ways people can intentionally change the input word in order to obtain a new word that resembles the initial one enough to be understood by other people. For example, bad actors could try to:

  • duplicate some letters multiple times
  • add some separators (eg "-", ".") between characters
  • delete some characters (eg the f word without "c")
  • reverse the word
  • potentially others

My suggestion is to initially keep it simple, if you don't want to delve into machine learning. As a possible approach you could try to:

  1. manually create a set of lower-case bad words with their duplicated letters removed (eg "killer" -> "kiler").

  2. manually/automatically add to this set variants of these words with one or multiple letters missing that can still be easily understood (eg "kiler" +-> "kilr").

  3. extract the words in the message (eg by message_str.split() )

  4. for each word and its reversed version:

    a. remove possible separators (eg "-", ".")

    b. convert it to lower case and remove consecutive, duplicate letters

    c. check if this new form of the word is present in the set, if so, censor it or the entire message

This solution lacks the protection against words with characters separated by one or multiple white spaces / newlines (eg "killer" -> "killer"). Depending on how long the messages are (I believe they are generally short in chat rooms), you can try to consider each substring of the initial message with removed whitespaces, instead of each word detected by the white space separator in step 3. This will take more time, as generating each substring will take alone O(message_length^2) time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM