Merge remote-tracking branch 'origin/main'

This commit is contained in:
Leonardo Brandenberger 2021-11-08 10:59:42 +01:00
commit a491e627da
2 changed files with 36 additions and 32 deletions

View File

@ -1,5 +1,3 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*; import java.util.*;
public class Text { public class Text {
@ -47,7 +45,7 @@ public class Text {
/** /**
* Method to get all paragraph of the existing instance. * Method to get all paragraph of the existing instance.
* *
* @return returns a ArrayList<String> with all text inside. * @return returns a ArrayList<String> with all paragraphs inside.
*/ */
public ArrayList<String> getText() { public ArrayList<String> getText() {
@ -135,20 +133,21 @@ public class Text {
* *
* @return ArrayList<String> * @return ArrayList<String>
*/ */
public ArrayList<String> index() { public ArrayList<String> index() {
wordbook = createWordlist(); wordbook = createWordlist();
String input;
ArrayList<String> output = new ArrayList<>(); ArrayList<String> output = new ArrayList<>();
ArrayList<Integer> values;
for (Map.Entry<String, ArrayList<Integer>> entry : wordbook.entrySet()) { for (Map.Entry<String, ArrayList<Integer>> entry : wordbook.entrySet()) {
input = "";
String key = entry.getKey(); String key = entry.getKey();
ArrayList<Integer> values = entry.getValue(); values = entry.getValue();
String input = "";
if (values.get(0) >= 3) { if (values.get(0) >= 3) {
input += key + " "; input += key + " ";
for (int i = 1; i < values.size(); i++) { for (int i = 1; i < values.size(); i++) {
if(i+1 == values.size()){ if (i + 1 == values.size()) {
input += values.get(i)+""; input += values.get(i) + "";
}else { } else {
input += values.get(i) + ", "; input += values.get(i) + ", ";
} }
} }
@ -159,35 +158,37 @@ 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<String, ArrayList < Integer>> * @return HashMap<String, ArrayList < Integer>>
*/ */
private HashMap<String, ArrayList<Integer>> createWordlist() { private HashMap<String, ArrayList<Integer>> createWordlist() {
int counter;
ArrayList<Integer> numbersList;
String firstLetter;
String restLetters;
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[] words = text.get(i).trim().toLowerCase().split("[ :;.,!?><'/\n]+");
for (String wort : woerter) { for (String word : words) {
String firstLetter = wort.substring(0,1); counter = 1;
String restLetters = wort.substring(1); firstLetter = word.substring(0, 1);
wort = firstLetter.toUpperCase()+restLetters; restLetters = word.substring(1);
ArrayList<Integer> zahlenListe; word = firstLetter.toUpperCase() + restLetters;
int zähler = 1; if (wordbook.containsKey(word)) {
if (wordbook.containsKey(wort)) { numbersList = wordbook.get(word);
zahlenListe = wordbook.get(wort); counter = numbersList.get(0);
zähler = zahlenListe.get(0); numbersList.remove(0);
zahlenListe.remove(0); numbersList.add(0, counter + 1);
zahlenListe.add(0, zähler + 1);
if (zahlenListe.get(zahlenListe.size() - 1) < i + 1) { if (numbersList.get(numbersList.size() - 1) < i + 1) {
zahlenListe.add(i + 1); numbersList.add(i + 1);
} }
wordbook.put(wort, zahlenListe);
} else { } else {
zahlenListe = new ArrayList<>(); numbersList = new ArrayList<>();
zahlenListe.add(1); numbersList.add(counter);
zahlenListe.add(i + 1); numbersList.add(i + 1);
wordbook.put(wort, zahlenListe);
} }
wordbook.put(word, numbersList);
} }
} }
return wordbook; return wordbook;

View File

@ -16,10 +16,13 @@ public class TextTest {
@Test @Test
void indexTest() { void indexTest() {
txt.add("Hallo Hallo zusammen !test!"); txt.add("Hallo ?Hallo> zusammen !test!");
txt.add("Hallo, wie zusammen"); txt.add("Hallo, wie zusammen");
txt.add("Hallo,wie wie zusammen"); txt.add("Hallo! Wie wie zusammen");
sListe= txt.index(); 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));
} }
} }