Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. static Map<String, String[]> words = new HashMap<>();
  2.  
  3. static int largestWordLength = 0;
  4.  
  5. public static void loadConfigs() {
  6. try {
  7. BufferedReader reader = new BufferedReader(new InputStreamReader(new URL("https://docs.google.com/spreadsheets/d/1hIEi2YG3ydav1E06Bzf2mQbGZ12kh2fe4ISgLg_UBuM/export?format=csv").openConnection().getInputStream()));
  8. String line = "";
  9. int counter = 0;
  10. while((line = reader.readLine()) != null) {
  11. counter++;
  12. String[] content = null;
  13. try {
  14. content = line.split(",");
  15. if(content.length == 0) {
  16. continue;
  17. }
  18. String word = content[0];
  19. String[] ignore_in_combination_with_words = new String[]{};
  20. if(content.length > 1) {
  21. ignore_in_combination_with_words = content[1].split("_");
  22. }
  23.  
  24. if(word.length() > largestWordLength) {
  25. largestWordLength = word.length();
  26. }
  27. words.put(word.replaceAll(" ", ""), ignore_in_combination_with_words);
  28.  
  29. } catch(Exception e) {
  30. e.printStackTrace();
  31. }
  32.  
  33. }
  34. System.out.println("Loaded " + counter + " words to filter out");
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38.  
  39. }
  40.  
  41.  
  42. /**
  43. * Iterates over a String input and checks whether a cuss word was found in a list, then checks if the word should be ignored (e.g. bass contains the word *ss).
  44. * @param input
  45. * @return
  46. */
  47.  
  48. public static ArrayList<String> badWordsFound(String input) {
  49. if(input == null) {
  50. return new ArrayList<>();
  51. }
  52.  
  53. // don't forget to remove leetspeak, probably want to move this to its own function and use regex if you want to use this
  54.  
  55. input = input.replaceAll("1","i");
  56. input = input.replaceAll("!","i");
  57. input = input.replaceAll("3","e");
  58. input = input.replaceAll("4","a");
  59. input = input.replaceAll("@","a");
  60. input = input.replaceAll("5","s");
  61. input = input.replaceAll("7","t");
  62. input = input.replaceAll("0","o");
  63. input = input.replaceAll("9","g");
  64.  
  65.  
  66. ArrayList<String> badWords = new ArrayList<>();
  67. input = input.toLowerCase().replaceAll("[^a-zA-Z]", "");
  68.  
  69. // iterate over each letter in the word
  70. for(int start = 0; start < input.length(); start++) {
  71. // from each letter, keep going to find bad words until either the end of the sentence is reached, or the max word length is reached.
  72. for(int offset = 1; offset < (input.length()+1 - start) && offset < largestWordLength; offset++) {
  73. String wordToCheck = input.substring(start, start + offset);
  74. if(words.containsKey(wordToCheck)) {
  75. // for example, if you want to say the word bass, that should be possible.
  76. String[] ignoreCheck = words.get(wordToCheck);
  77. boolean ignore = false;
  78. for(int s = 0; s < ignoreCheck.length; s++ ) {
  79. if(input.contains(ignoreCheck[s])) {
  80. ignore = true;
  81. break;
  82. }
  83. }
  84. if(!ignore) {
  85. badWords.add(wordToCheck);
  86. }
  87. }
  88. }
  89. }
  90.  
  91.  
  92. for(String s: badWords) {
  93. System.out.println(s + " qualified as a bad word in a username");
  94. }
  95. return badWords;
  96.  
  97. }
  98.  
  99. public static String filterText(String input, String username) {
  100. ArrayList<String> badWords = badWordsFound(input);
  101. if(badWords.size() > 0) {
  102. return "This message was blocked because a bad word was found. If you believe this word should not be blocked, please message support.";
  103. }
  104. return input;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement