Merge remote-tracking branch 'origin/main'

This commit is contained in:
Leonardo Brandenberger 2021-11-05 12:04:06 +01:00
commit 9afb8458c3
2 changed files with 52 additions and 14 deletions

View File

@ -1,9 +1,6 @@
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Text { public class Text {
@ -47,6 +44,12 @@ public class Text {
return true; return true;
} }
/**
* Method to get all paragraph of the existing instance.
*
* @return returns a ArrayList<String> with all text inside.
*/
public ArrayList<String> getText() { public ArrayList<String> getText() {
return text; return text;
} }
@ -98,6 +101,14 @@ public class Text {
return true; return true;
} }
/**
* This method deletes a specific Paragraph. As a Parameter it uses a int.
* If the Parameter is valid it deletes the specific Pararaph otherwise returns false
*
* @param paragraphNumber
* @return False: If the int is not a valid paragraph.
* @return True: If the int is a valid paragraph number
*/
public boolean del(int paragraphNumber) { public boolean del(int paragraphNumber) {
if (paragraphExists(paragraphNumber)) { if (paragraphExists(paragraphNumber)) {
@ -107,31 +118,58 @@ public class Text {
return false; return false;
} }
/**
* This method deletes the last paragraph in the text Array.
*
* @return True: if paragraph has been deleted.
*/
public boolean del() { public boolean del() {
text.remove(text.size() - 1); text.remove(text.size() - 1);
return true; return true;
} }
/**
* This method creates a ArrayList<String>.
* Every word which is used in the text more than 3 times
* will be added to the ArrayList with the paragraphes where the word is been used.
*
* @return ArrayList<String>
*/
public ArrayList<String> index() { public ArrayList<String> index() {
wordbook = createWordlist(); wordbook = createWordlist();
ArrayList<String> output = new ArrayList<>(); ArrayList<String> output = new ArrayList<>();
for (Map.Entry<String, ArrayList<Integer>> entry : wordbook.entrySet()) { for (Map.Entry<String, ArrayList<Integer>> entry : wordbook.entrySet()) {
String key = entry.getKey(); String key = entry.getKey();
ArrayList<Integer> values = entry.getValue(); ArrayList<Integer> values = entry.getValue();
String input = "";
if (values.get(0) >= 3) { if (values.get(0) >= 3) {
output.add(key); input += key + " ";
for (int value : values) { for (int i = 1; i < values.size(); i++) {
output.add(String.valueOf(value)); if(i+1 == values.size()){
input += values.get(i)+"";
}else {
input += values.get(i) + ", ";
} }
} }
output.add(input);
}
} }
return output; return output;
} }
/**
* This method counts all Words in text and adds them together with the paragraph where the word is been used to a Hashmap.
*
* @return HashMap<String, ArrayList < Integer>>
*/
private HashMap<String, ArrayList<Integer>> createWordlist() { private HashMap<String, ArrayList<Integer>> createWordlist() {
for (int i = 0; i < text.size(); i++) { for (int i = 0; i < text.size(); i++) {
String[] woerter = text.get(i).trim().toLowerCase().split("[ :;.,!?><\'/]+"); String[] woerter = text.get(i).trim().toLowerCase().split("[ :;.,!?><\'/]+");
for (String wort : woerter) { for (String wort : woerter) {
String firstLetter = wort.substring(0,1);
String restLetters = wort.substring(1);
wort = firstLetter.toUpperCase()+restLetters;
ArrayList<Integer> zahlenListe; ArrayList<Integer> zahlenListe;
int zähler = 1; int zähler = 1;
if (wordbook.containsKey(wort)) { if (wordbook.containsKey(wort)) {
@ -147,7 +185,7 @@ public class Text {
} else { } else {
zahlenListe = new ArrayList<>(); zahlenListe = new ArrayList<>();
zahlenListe.add(1); zahlenListe.add(1);
zahlenListe.add(zähler); zahlenListe.add(i + 1);
wordbook.put(wort, zahlenListe); wordbook.put(wort, zahlenListe);
} }
} }

View File

@ -1,25 +1,25 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
public class TextTest { public class TextTest {
Text txt; Text txt;
HashMap<String, ArrayList<Integer>> hMap;
ArrayList<String> sListe; ArrayList<String> sListe;
@BeforeEach @BeforeEach
void setup() { void setup() {
txt = new Text(); txt = new Text();
hMap = new HashMap<>();
sListe = new ArrayList<>();
sListe.add("Hallo hallo zusammen");
sListe.add("Hallo wie gehts");
} }
@Test @Test
void indexTest() { void indexTest() {
txt.add("Hallo Hallo zusammen !test!");
txt.add("Hallo, wie zusammen");
txt.add("Hallo,wie wie zusammen");
sListe= txt.index();
Assertions.assertEquals("Hallo 1, 2, 3",sListe.get(0));
} }
} }