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,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<String> 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<String,ArrayList<Integer>> wordbook = new HashMap<>();
private HashMap<String, ArrayList<Integer>> 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<String> 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<String,ArrayList<Integer>> index(ArrayList<String> text) {
for (int i = 0;i < text.size();i++) {
String[] woerter = text.get(i).trim().toLowerCase().split("[ :;.,!?><\'/]+");
for (String wort: woerter) {
ArrayList<Integer> zahlenListe;
if(wordbook.containsKey(wort)) {
zahlenListe = wordbook.get(wort);
zahlenListe.add(0, zahlenListe.get(0)+1);
if(zahlenListe.size()<i++){
zahlenListe.add(i++);
}
wordbook.put(wort,zahlenListe);
}else{
zahlenListe = new ArrayList<>();
zahlenListe.add(1);
zahlenListe.add(i++);
wordbook.put(wort,zahlenListe);
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++) {
String[] woerter = text.get(i).trim().toLowerCase().split("[ :;.,!?><\'/]+");
for (String wort : woerter) {
ArrayList<Integer> 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() {
}
}

View File

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