|
| 1 | +package week2; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | +import java.util.regex.Matcher; |
| 6 | +import java.util.regex.Pattern; |
| 7 | + |
| 8 | +public class TextAnalyzer { |
| 9 | + |
| 10 | + // Sõned, mis lõppevad tähtedega "ed" või mõni täht veel. |
| 11 | + // (Ülesanne lehel on pikemalt seletatud!) |
| 12 | + public static final String RE1 = "ed.?$"; |
| 13 | + |
| 14 | + // Paaritu pikkusega sõned. |
| 15 | + public static final String RE2 = "^.(..)*$"; |
| 16 | + |
| 17 | + // Sõned, mille esimene ja viimane täht on sama! |
| 18 | + public static final String RE3 = "^(.).*\\1$|^.$"; |
| 19 | + //Lihtsam variant: |
| 20 | + //public static final String RE3 = "^(.)(.*\\1)?$"; |
| 21 | + |
| 22 | + // Sõned, mis ülesanne nimede tingimustele vastavad. |
| 23 | + public static final String NAME = "\\p{Upper}\\p{Lower}*\\s*\\p{Upper}\\p{Lower}*"; |
| 24 | + |
| 25 | + // Sõned, mis ülesanne numbri tingimustele vastavad. |
| 26 | + public static final String NUMBER = "\\d{3,4}[\\s-]\\d{3,4}|\\d{4,8}"; |
| 27 | + |
| 28 | + private final String text; |
| 29 | + |
| 30 | + public TextAnalyzer(String text) { |
| 31 | + this.text = text; |
| 32 | + } |
| 33 | + |
| 34 | + public Map<String, String> getPhoneNumbers() { |
| 35 | + String regex = "(?<name>" + NAME + ").*?\\b(?<number>" + NUMBER + ")\\b"; |
| 36 | + Pattern pattern = Pattern.compile(regex); |
| 37 | + Matcher matcher = pattern.matcher(text); |
| 38 | + Map<String, String> phonebook = new HashMap<>(); |
| 39 | + while (matcher.find()){ |
| 40 | + String cleanNumber = matcher.group("number").replaceAll("[-\\s]", ""); |
| 41 | + phonebook.put(matcher.group("name"), cleanNumber); |
| 42 | + } |
| 43 | + return phonebook; |
| 44 | + } |
| 45 | + |
| 46 | + public String anonymize() { |
| 47 | + return text.replaceAll(NAME, "<nimi>").replaceAll(NUMBER, "<telefoninumber>"); |
| 48 | + } |
| 49 | + |
| 50 | + // Alternatiivne lahendus: kasutab getPhoneNumbers regex-it ja läbib sõne ainult ühe korra. |
| 51 | + public String anonymizeSingleReplace() { |
| 52 | + String regex = "(" + NAME + ")(?<middle>.*?)\\b(" + NUMBER + ")\\b"; |
| 53 | + return text.replaceAll(regex, "<nimi>${middle}<telefoninumber>"); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + static void main() { |
| 58 | + |
| 59 | + String input = |
| 60 | + """ |
| 61 | + Mina olen Kalle Kulbok ja mu telefoninumber on 5556 4272. |
| 62 | + Mina olen Peeter Peet ja mu telefoninumber on 5234 567. |
| 63 | + Mari Maasikas siin, mu number on 6723 3434. Tere, olen Jaan Jubin numbriga 45631643."""; |
| 64 | + |
| 65 | + TextAnalyzer ta = new TextAnalyzer(input); |
| 66 | + Map<String, String> phoneBook = ta.getPhoneNumbers(); |
| 67 | + System.out.println(phoneBook.get("Peeter Peet")); // peab väljastama 5234567 |
| 68 | + System.out.println(phoneBook.get("Jaan Jubin")); // peab väljastama 45631643 |
| 69 | + |
| 70 | + System.out.println(ta.anonymize()); |
| 71 | + |
| 72 | + /* peab väljastama: |
| 73 | + Mina olen <nimi> ja mu telefoninumber on <telefoninumber>. |
| 74 | + Mina olen <nimi> ja mu telefoninumber on <telefoninumber>. |
| 75 | + <nimi> siin, mu number on <telefoninumber>. Tere, olen <nimi> numbriga <telefoninumber>. |
| 76 | + */ |
| 77 | + } |
| 78 | +} |
0 commit comments