index and get Methode in text.java done

This commit is contained in:
Andrin Fassbind 2021-11-05 09:58:40 +01:00
parent 2d71212b05
commit 812ec1d388
3 changed files with 59 additions and 34 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/out

View File

@ -2,6 +2,7 @@ import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
import java.util.Scanner; import java.util.Scanner;
public class Text { public class Text {
@ -12,6 +13,7 @@ public class Text {
/** /**
* Method to check if a specific Paragraph exists * Method to check if a specific Paragraph exists
*
* @param paragraphNumber the number of the paragraph which should be checked * @param paragraphNumber the number of the paragraph which should be checked
* @return returns true if the paragraph exists * @return returns true if the paragraph exists
*/ */
@ -21,6 +23,7 @@ public class Text {
/** /**
* Method to add a paragraph at a specific position. * Method to add a paragraph at a specific position.
*
* @param paragraphNumber number of paragraph where the new text should be added * @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 * @return returns true if the given paragraph exists and is added successfully
@ -35,6 +38,7 @@ public class Text {
/** /**
* Method to add a paragraph at the end of the existing text. * Method to add a paragraph at the end of the existing text.
*
* @param text the Text which should be added. * @param text the Text which should be added.
* @return returns true if the paragraph is added successfully * @return returns true if the paragraph is added successfully
*/ */
@ -43,8 +47,13 @@ public class Text {
return true; return true;
} }
public ArrayList<String> getText() {
return text;
}
/** /**
* Method to add a dummy text paragraph at a specific position. * Method to add a dummy text paragraph at a specific position.
*
* @param paragraphNumber number of paragraph where the dummy text should be added * @param paragraphNumber number of paragraph where the dummy text should be added
* @return returns true if the given paragraph exists and is added successfully * @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. * Method to add a dummy text paragraph at the end of the existing text.
*
* @return returns true if the paragraph is added successfully * @return returns true if the paragraph is added successfully
*/ */
public boolean dummy() { public boolean dummy() {
@ -62,6 +72,7 @@ public class Text {
/** /**
* Method to replace characters in a specific paragraph * Method to replace characters in a specific paragraph
*
* @param paragraphNumber number of paragraph which should be changed. * @param paragraphNumber number of paragraph which should be changed.
* @param oldChar the old character. * @param oldChar the old character.
* @param newChar the new character. * @param newChar the new character.
@ -77,6 +88,7 @@ public class Text {
/** /**
* Method to replace characters in the last paragraph * Method to replace characters in the last paragraph
*
* @param oldChar the old character. * @param oldChar the old character.
* @param newChar the new character. * @param newChar the new character.
* @return returns true if the paragraph is changed successfully * @return returns true if the paragraph is changed successfully
@ -94,37 +106,53 @@ public class Text {
} }
return false; return false;
} }
public boolean del() { public boolean del() {
text.remove(text.size() - 1); text.remove(text.size() - 1);
return true; return true;
} }
public HashMap<String,ArrayList<Integer>> index(ArrayList<String> text) { public ArrayList<String> index() {
wordbook = createWordlist();
ArrayList<String> output = new ArrayList<>();
for (Map.Entry<String, ArrayList<Integer>> entry : wordbook.entrySet()) {
String key = entry.getKey();
ArrayList<Integer> values = entry.getValue();
if (values.get(0) >= 3) {
output.add(key);
for (int value : values) {
output.add(String.valueOf(value));
}
}
}
return output;
}
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) {
ArrayList<Integer> zahlenListe; ArrayList<Integer> zahlenListe;
int zähler = 1;
if (wordbook.containsKey(wort)) { if (wordbook.containsKey(wort)) {
zahlenListe = wordbook.get(wort); zahlenListe = wordbook.get(wort);
zahlenListe.add(0, zahlenListe.get(0)+1); zähler = zahlenListe.get(0);
if(zahlenListe.size()<i++){ zahlenListe.remove(0);
zahlenListe.add(i++); zahlenListe.add(0, zähler + 1);
if (zahlenListe.get(zahlenListe.size() - 1) < i + 1) {
zahlenListe.add(i + 1);
} }
wordbook.put(wort, zahlenListe); wordbook.put(wort, zahlenListe);
} else { } else {
zahlenListe = new ArrayList<>(); zahlenListe = new ArrayList<>();
zahlenListe.add(1); zahlenListe.add(1);
zahlenListe.add(i++); zahlenListe.add(zähler);
wordbook.put(wort, zahlenListe); wordbook.put(wort, zahlenListe);
} }
} }
} }
return wordbook; return wordbook;
} }
private void updateWordbook() {
}
} }

View File

@ -21,9 +21,5 @@ public class TextTest {
@Test @Test
void indexTest() { void indexTest() {
hMap = txt.index(sListe);
for(Map.Entry<String,ArrayList<Integer>> entry : hMap.entrySet()) {
System.out.println("Key: " + entry.getKey() + " Value: " + entry.getValue());
}
} }
} }