-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatFilter.java
More file actions
57 lines (52 loc) · 1.48 KB
/
ChatFilter.java
File metadata and controls
57 lines (52 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* ChatFilter class
*
* Takes in each String to be broadcast and filters it based on specific 'bad' words from a text file
*
* @author Christopher Lehman
*
* @version 11/13/18
*
*/
public class ChatFilter {
private List<String> words;
public ChatFilter(String badWordsFileName) {
File file = new File(badWordsFileName);
words = new ArrayList<>();
//add all words in file to list for future checking
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
if (line.isEmpty()) {
continue;
}
words.add(line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public String filter(String msg) {
if (msg == null) {
return null;
}
String replacement = "";
for (String s : words) {
for (int i = 0; i < s.length(); i++) { // gets replacement string of proper length
replacement += "*";
}
msg = msg.replaceAll("(?i)" + s, replacement); //case-insensitive
replacement = "";
}
return msg;
}
}