-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathPersianSwear.java
89 lines (72 loc) · 2.09 KB
/
PersianSwear.java
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PersianSwear {
private final List<String> swearWords = new ArrayList<>();
public PersianSwear() {
try (
BufferedReader reader = new BufferedReader(new FileReader("data.txt"))
) {
String line;
while ((line = reader.readLine()) != null) {
String[] splitedWords = line.split(", ");
swearWords.addAll(List.of(splitedWords));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private String clearWord(String word) {
Pattern pattern = Pattern.compile("[\\p{P}\\p{Mn}\\p{S}\\p{javaDigit}]");
Matcher matcher = pattern.matcher(word);
matcher.reset();
return matcher
.replaceAll("")
.trim()
.replace("\u200c", " ")
.replace("ي", "ی")
.replace("ك", "ک")
.replace("ـ", "");
}
public void addWord(String word) {
swearWords.add(word.trim());
}
public void addWords(String... words) {
swearWords.addAll(List.of(words));
}
public void removeWord(String word) {
swearWords.remove(word.trim());
}
public void removeWords(String... words) {
swearWords.removeAll(List.of(words));
}
public boolean isBad(String word) {
return swearWords.contains(clearWord(word));
}
public boolean hasSwear(String text) {
List<String> splitText = List.of(text.split(" "));
for (String word : splitText) {
if (swearWords.contains(clearWord(word))) {
return true;
}
}
return false;
}
public String filterWords(String text, String symbol) {
List<String> splitText = new ArrayList<>(List.of(text.split(" ")));
for (int i = 0; i < splitText.size(); i++) {
String word = splitText.get(i);
if (swearWords.contains(clearWord(word))) {
splitText.set(i, symbol);
}
}
return String.join(" ", splitText);
}
public String filterWords(String text) {
return filterWords(text, "*");
}
}