diff --git a/PersianSwear.java b/PersianSwear.java index 9e480e1..9bfda18 100644 --- a/PersianSwear.java +++ b/PersianSwear.java @@ -7,73 +7,83 @@ import java.util.regex.Pattern; public class PersianSwear { - private final ArrayList 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 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 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 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 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 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, "*"); + } }