Skip to content

Commit

Permalink
Update PersianSwear.java
Browse files Browse the repository at this point in the history
  • Loading branch information
amirshnll committed Jun 22, 2023
1 parent 6fc9260 commit 2ec08d6
Showing 1 changed file with 70 additions and 60 deletions.
130 changes: 70 additions & 60 deletions PersianSwear.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,73 +7,83 @@
import java.util.regex.Pattern;

public class PersianSwear {
private final ArrayList<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());
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);
}

public void addWords(String[] words) {
swearWords.addAll(List.of(words));
}

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;
}
}

public void removeWord(String word) {
swearWords.remove(word.trim());
}
return false;
}

public void removeWords(String[] words) {
swearWords.removeAll(List.of(words));
}
public String filterWords(String text, String symbol) {
List<String> splitText = new ArrayList<>(List.of(text.split(" ")));

public boolean isBad(String word) {
return swearWords.contains(clearWord(word));
for (int i = 0; i < splitText.size(); i++) {
String word = splitText.get(i);
if (swearWords.contains(clearWord(word))) {
splitText.set(i, symbol);
}
}

public boolean hasSwear(String text) {
List<String> splitedText = List.of(text.split(" "));

for (String word : splitedText)
if (swearWords.contains(clearWord(word)))
return true;
return String.join(" ", splitText);
}

return false;
}

public String filterWords(String text, String symbol) {
List<String> splitedText = new ArrayList<>(List.of(text.split(" ")));

for (String word : splitedText)
if (swearWords.contains(clearWord(word)))
splitedText.set(splitedText.indexOf(word), symbol);

return String.join(" ", splitedText);
}

public String filterWords(String text) {
return filterWords(text, "*");
}
public String filterWords(String text) {
return filterWords(text, "*");
}
}

0 comments on commit 2ec08d6

Please sign in to comment.