From 7a9f336569d25ff2f4f77eef5800cfd14c59b6f4 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Mon, 8 May 2023 17:34:43 +0100 Subject: [PATCH] [POO] aula11 ex1 added --- .../poo/src/aula11/ex1/WordPairCounter.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 1ano/2semestre/poo/src/aula11/ex1/WordPairCounter.java diff --git a/1ano/2semestre/poo/src/aula11/ex1/WordPairCounter.java b/1ano/2semestre/poo/src/aula11/ex1/WordPairCounter.java new file mode 100644 index 0000000..66241ad --- /dev/null +++ b/1ano/2semestre/poo/src/aula11/ex1/WordPairCounter.java @@ -0,0 +1,66 @@ +package aula11.ex1; + +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; + +public class WordPairCounter { + public static void main(String[] args) { + TreeMap> wordPairs = new TreeMap<>() { + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + for (String word1 : this.keySet()) { + sb.append(word1).append("={"); + for (String word2 : this.get(word1).keySet()) { + sb.append(word2).append("=").append(this.get(word1).get(word2)).append(", "); + } + sb.delete(sb.length()-2, sb.length()).append("}\n"); + } + return sb.toString(); + } + }; + + String text = ""; + Path path = Paths.get((new Scanner(System.in)).nextLine()); + try { + text = Files.readString(path); + } catch (IOException e) { + 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.,:'‘’;?!\\-*{}=+&/()\\[\\]”“\"]+"); + + for (int i = 0; i < words.length; i++) { + String word1 = words[i]; + if (word1.length() < 3) continue; + word1 = word1.toLowerCase(); + + 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); + } + + System.out.println(wordPairs); + } +}