From ab9fa59d0a44a87d484c1850278beafc3085a074 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Tue, 9 May 2023 11:12:00 +0100 Subject: [PATCH] [POO] simplifications to WordPairCounter --- .../poo/src/aula11/ex1/WordPairCounter.java | 33 ++++--------------- 1 file changed, 7 insertions(+), 26 deletions(-) diff --git a/1ano/2semestre/poo/src/aula11/ex1/WordPairCounter.java b/1ano/2semestre/poo/src/aula11/ex1/WordPairCounter.java index 66241ad..1eee2b6 100644 --- a/1ano/2semestre/poo/src/aula11/ex1/WordPairCounter.java +++ b/1ano/2semestre/poo/src/aula11/ex1/WordPairCounter.java @@ -31,34 +31,15 @@ public class WordPairCounter { System.out.printf("Certifique-se que o ficheiro \"%s\" está na raiz da pasta do projeto", path); System.exit(1); } - String[] words = text.split("[\\s.,:'‘’;?!\\-*{}=+&/()\\[\\]”“\"]+"); + Object[] words = Arrays.stream(text.split("[\\s.,:'‘’;?!\\-*{}=+&/()\\[\\]”“\"]+")).filter(word -> word.length() > 2).map(String::toLowerCase).toArray(); - for (int i = 0; i < words.length; i++) { - String word1 = words[i]; - if (word1.length() < 3) continue; - word1 = word1.toLowerCase(); + for (int i = 1; i < words.length-1; i++) { + String word1 = (String) words[i-1]; + String word2 = (String) words[i]; - String word2 = null; - for (int j = i+1; j < words.length; j++) - if (words[j].length() > 2) { - word2 = words[j]; - break; - } - if (word2 == null) break; - word2 = word2.toLowerCase(); - - HashMap word1Pair; - if (wordPairs.containsKey(word1)) - word1Pair = wordPairs.get(word1); - else { - word1Pair = new HashMap<>(); - wordPairs.put(word1, word1Pair); - } - - if (word1Pair.containsKey(word2)) - word1Pair.put(word2, word1Pair.get(word2) + 1); - else - word1Pair.put(word2, 1); + HashMap word1Pair = wordPairs.getOrDefault(word1, new HashMap<>()); + wordPairs.put(word1, word1Pair); + word1Pair.put(word2, word1Pair.getOrDefault(word2, 0) + 1); } System.out.println(wordPairs);