From edba4d5d8cdc59a712b7f3a30f26a33a160d9ef2 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 5 Nov 2021 17:59:26 +0100 Subject: [PATCH 1/3] Text.java index and createWordlist methode cleanup --- src/Text.java | 44 ++++++++++++++++++++++---------------------- test/TextTest.java | 9 ++++++--- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/Text.java b/src/Text.java index 1988e2b..1811f7b 100644 --- a/src/Text.java +++ b/src/Text.java @@ -1,5 +1,3 @@ -import java.io.File; -import java.io.FileNotFoundException; import java.util.*; public class Text { @@ -47,7 +45,7 @@ public class Text { /** * Method to get all paragraph of the existing instance. * - * @return returns a ArrayList with all text inside. + * @return returns a ArrayList with all paragraphs inside. */ public ArrayList getText() { @@ -135,20 +133,20 @@ public class Text { * * @return ArrayList */ - public ArrayList index() { wordbook = createWordlist(); + String input; ArrayList output = new ArrayList<>(); for (Map.Entry> entry : wordbook.entrySet()) { + input = ""; String key = entry.getKey(); ArrayList values = entry.getValue(); - String input = ""; if (values.get(0) >= 3) { input += key + " "; for (int i = 1; i < values.size(); i++) { - if(i+1 == values.size()){ - input += values.get(i)+""; - }else { + if (i + 1 == values.size()) { + input += values.get(i) + ""; + } else { input += values.get(i) + ", "; } } @@ -164,30 +162,32 @@ public class Text { * @return HashMap> */ private HashMap> createWordlist() { + int counter; + ArrayList zahlenListe; + String firstLetter; + String restLetters; for (int i = 0; i < text.size(); i++) { - String[] woerter = text.get(i).trim().toLowerCase().split("[ :;.,!?><'/]+"); - for (String wort : woerter) { - String firstLetter = wort.substring(0,1); - String restLetters = wort.substring(1); - wort = firstLetter.toUpperCase()+restLetters; - ArrayList zahlenListe; - int zähler = 1; - if (wordbook.containsKey(wort)) { - zahlenListe = wordbook.get(wort); - zähler = zahlenListe.get(0); + String[] words = text.get(i).trim().toLowerCase().split("[ :;.,!?><'/]+"); + for (String word : words) { + counter = 1; + firstLetter = word.substring(0, 1); + restLetters = word.substring(1); + word = firstLetter.toUpperCase() + restLetters; + if (wordbook.containsKey(word)) { + zahlenListe = wordbook.get(word); + counter = zahlenListe.get(0); zahlenListe.remove(0); - zahlenListe.add(0, zähler + 1); + zahlenListe.add(0, counter + 1); if (zahlenListe.get(zahlenListe.size() - 1) < i + 1) { zahlenListe.add(i + 1); } - wordbook.put(wort, zahlenListe); } else { zahlenListe = new ArrayList<>(); - zahlenListe.add(1); + zahlenListe.add(counter); zahlenListe.add(i + 1); - wordbook.put(wort, zahlenListe); } + wordbook.put(word, zahlenListe); } } return wordbook; diff --git a/test/TextTest.java b/test/TextTest.java index d7e78b1..d8e2190 100644 --- a/test/TextTest.java +++ b/test/TextTest.java @@ -16,10 +16,13 @@ public class TextTest { @Test void indexTest() { - txt.add("Hallo Hallo zusammen !test!"); + txt.add("Hallo ?Hallo> zusammen !test!"); txt.add("Hallo, wie zusammen"); - txt.add("Hallo,wie wie zusammen"); + txt.add("Hallo! Wie wie zusammen"); sListe= txt.index(); - Assertions.assertEquals("Hallo 1, 2, 3",sListe.get(0)); + for (String str : sListe){ + System.out.println(str); + } + Assertions.assertEquals("Hallo 1, 2, 3",sListe.get(0)); } } From caec2257e1f55a080ff4d056c152c4747b519876 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 5 Nov 2021 18:33:37 +0100 Subject: [PATCH 2/3] Text.java index and createWordlist methode cleanup --- src/Text.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Text.java b/src/Text.java index 1811f7b..d801203 100644 --- a/src/Text.java +++ b/src/Text.java @@ -137,10 +137,11 @@ public class Text { wordbook = createWordlist(); String input; ArrayList output = new ArrayList<>(); + ArrayList values; for (Map.Entry> entry : wordbook.entrySet()) { input = ""; String key = entry.getKey(); - ArrayList values = entry.getValue(); + values = entry.getValue(); if (values.get(0) >= 3) { input += key + " "; for (int i = 1; i < values.size(); i++) { @@ -167,7 +168,7 @@ public class Text { String firstLetter; String restLetters; for (int i = 0; i < text.size(); i++) { - String[] words = text.get(i).trim().toLowerCase().split("[ :;.,!?><'/]+"); + String[] words = text.get(i).trim().toLowerCase().split("[ :;.,!?><'/\n]+"); for (String word : words) { counter = 1; firstLetter = word.substring(0, 1); From c565fffbffc3bfa06a9efa290b1423bd1c68b5b2 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 5 Nov 2021 18:36:05 +0100 Subject: [PATCH 3/3] Text.java index and createWordlist methode cleanup --- src/Text.java | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Text.java b/src/Text.java index d801203..f26c4ed 100644 --- a/src/Text.java +++ b/src/Text.java @@ -158,13 +158,13 @@ public class Text { } /** - * This method counts all Words in text and adds them together with the paragraph where the word is been used to a Hashmap. + * This method counts all Words in text and adds the count together with the paragraph where the word is been used to a Hashmap. * * @return HashMap> */ private HashMap> createWordlist() { int counter; - ArrayList zahlenListe; + ArrayList numbersList; String firstLetter; String restLetters; for (int i = 0; i < text.size(); i++) { @@ -175,20 +175,20 @@ public class Text { restLetters = word.substring(1); word = firstLetter.toUpperCase() + restLetters; if (wordbook.containsKey(word)) { - zahlenListe = wordbook.get(word); - counter = zahlenListe.get(0); - zahlenListe.remove(0); - zahlenListe.add(0, counter + 1); + numbersList = wordbook.get(word); + counter = numbersList.get(0); + numbersList.remove(0); + numbersList.add(0, counter + 1); - if (zahlenListe.get(zahlenListe.size() - 1) < i + 1) { - zahlenListe.add(i + 1); + if (numbersList.get(numbersList.size() - 1) < i + 1) { + numbersList.add(i + 1); } } else { - zahlenListe = new ArrayList<>(); - zahlenListe.add(counter); - zahlenListe.add(i + 1); + numbersList = new ArrayList<>(); + numbersList.add(counter); + numbersList.add(i + 1); } - wordbook.put(word, zahlenListe); + wordbook.put(word, numbersList); } } return wordbook;