From 812ec1d3885bf87ee48d4e45809aaa11b302516a Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 5 Nov 2021 09:58:40 +0100 Subject: [PATCH] index and get Methode in text.java done --- .gitignore | 1 + src/Text.java | 88 ++++++++++++++++++++++++++++++---------------- test/TextTest.java | 4 --- 3 files changed, 59 insertions(+), 34 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..981aeb8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/out \ No newline at end of file diff --git a/src/Text.java b/src/Text.java index d4cdc05..d4b75ce 100644 --- a/src/Text.java +++ b/src/Text.java @@ -2,16 +2,18 @@ import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.HashMap; +import java.util.Map; import java.util.Scanner; public class Text { private ArrayList text = new ArrayList<>(); private static final String dummyText = "The standard Lorem Ipsum passage, used since the 1500s \"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\" Section 1.10.32 of \"de Finibus Bonorum et Malorum\", written by Cicero in 45 BC \"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"; - private HashMap> wordbook = new HashMap<>(); + private HashMap> wordbook = new HashMap<>(); /** * Method to check if a specific Paragraph exists + * * @param paragraphNumber the number of the paragraph which should be checked * @return returns true if the paragraph exists */ @@ -21,12 +23,13 @@ public class Text { /** * Method to add a paragraph at a specific position. + * * @param paragraphNumber number of paragraph where the new text should be added - * @param text the Text which should be added. + * @param text the Text which should be added. * @return returns true if the given paragraph exists and is added successfully */ public boolean add(int paragraphNumber, String text) { - if(paragraphExists(paragraphNumber)) { + if (paragraphExists(paragraphNumber)) { this.text.add((paragraphNumber - 1), text); return true; } @@ -35,6 +38,7 @@ public class Text { /** * Method to add a paragraph at the end of the existing text. + * * @param text the Text which should be added. * @return returns true if the paragraph is added successfully */ @@ -43,8 +47,13 @@ public class Text { return true; } + public ArrayList getText() { + return text; + } + /** * Method to add a dummy text paragraph at a specific position. + * * @param paragraphNumber number of paragraph where the dummy text should be added * @return returns true if the given paragraph exists and is added successfully */ @@ -54,6 +63,7 @@ public class Text { /** * Method to add a dummy text paragraph at the end of the existing text. + * * @return returns true if the paragraph is added successfully */ public boolean dummy() { @@ -62,13 +72,14 @@ public class Text { /** * Method to replace characters in a specific paragraph + * * @param paragraphNumber number of paragraph which should be changed. - * @param oldChar the old character. - * @param newChar the new character. + * @param oldChar the old character. + * @param newChar the new character. * @return returns true if the given paragraph exists and is changed successfully */ public boolean replace(int paragraphNumber, String oldChar, String newChar) { - if(paragraphExists(paragraphNumber)) { + if (paragraphExists(paragraphNumber)) { text.set((paragraphNumber - 1), text.get(paragraphNumber - 1).replace(oldChar, newChar)); return true; } @@ -77,6 +88,7 @@ public class Text { /** * Method to replace characters in the last paragraph + * * @param oldChar the old character. * @param newChar the new character. * @return returns true if the paragraph is changed successfully @@ -88,43 +100,59 @@ public class Text { public boolean del(int paragraphNumber) { - if(paragraphExists(paragraphNumber)) { - text.remove(paragraphNumber-1); + if (paragraphExists(paragraphNumber)) { + text.remove(paragraphNumber - 1); return true; } return false; } + public boolean del() { - text.remove(text.size()-1); + text.remove(text.size() - 1); return true; } - public HashMap> index(ArrayList text) { - for (int i = 0;i < text.size();i++) { - String[] woerter = text.get(i).trim().toLowerCase().split("[ :;.,!?><\'/]+"); - for (String wort: woerter) { - ArrayList zahlenListe; - if(wordbook.containsKey(wort)) { - zahlenListe = wordbook.get(wort); - zahlenListe.add(0, zahlenListe.get(0)+1); - if(zahlenListe.size()(); - zahlenListe.add(1); - zahlenListe.add(i++); - wordbook.put(wort,zahlenListe); + public ArrayList index() { + wordbook = createWordlist(); + ArrayList output = new ArrayList<>(); + for (Map.Entry> entry : wordbook.entrySet()) { + String key = entry.getKey(); + ArrayList values = entry.getValue(); + if (values.get(0) >= 3) { + output.add(key); + for (int value : values) { + output.add(String.valueOf(value)); } } } + return output; + } + private HashMap> createWordlist() { + for (int i = 0; i < text.size(); i++) { + String[] woerter = text.get(i).trim().toLowerCase().split("[ :;.,!?><\'/]+"); + for (String wort : woerter) { + ArrayList zahlenListe; + int zähler = 1; + if (wordbook.containsKey(wort)) { + zahlenListe = wordbook.get(wort); + zähler = zahlenListe.get(0); + zahlenListe.remove(0); + zahlenListe.add(0, zähler + 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(zähler); + wordbook.put(wort, zahlenListe); + } + } + } return wordbook; } - private void updateWordbook() { - - } - } diff --git a/test/TextTest.java b/test/TextTest.java index a3018d1..5c6b081 100644 --- a/test/TextTest.java +++ b/test/TextTest.java @@ -21,9 +21,5 @@ public class TextTest { @Test void indexTest() { - hMap = txt.index(sListe); - for(Map.Entry> entry : hMap.entrySet()) { - System.out.println("Key: " + entry.getKey() + " Value: " + entry.getValue()); - } } }