gruppe06-hufflepuff-projekt.../src/Text.java

204 lines
8.2 KiB
Java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class Text {
private final 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?";
/**
* 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
*/
private boolean paragraphExists(int paragraphNumber) {
return ((paragraphNumber > 0) && (paragraphNumber <= this.text.size()));
}
/**
* 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.
* @return returns true if the given paragraph exists and is added successfully
*/
public boolean add(int paragraphNumber, String text) {
if (paragraphExists(paragraphNumber)) {
this.text.add((paragraphNumber - 1), text);
return true;
}
return false;
}
/**
* 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
*/
public boolean add(String text) {
this.text.add(text.replaceAll("[^A-Za-z0-9 .,:?!\"'-]",""));
return true;
}
/**
* Method to get all paragraph of the existing instance.
*
* @return returns a ArrayList<String> with all paragraphs inside.
*/
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
*/
public boolean dummy(int paragraphNumber) {
return add(paragraphNumber, dummyText);
}
/**
* 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() {
return add(dummyText);
}
/**
* 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.
* @return returns true if the given paragraph exists and is changed successfully
*/
public boolean replace(int paragraphNumber, String oldChar, String newChar) {
if (paragraphExists(paragraphNumber)) {
text.set((paragraphNumber - 1), text.get(paragraphNumber - 1).replace(oldChar, newChar));
return true;
}
return false;
}
/**
* 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
*/
public boolean replace(String oldChar, String newChar) {
if(paragraphExists((text.size()))) {
text.set((text.size() - 1), text.get(text.size() - 1).replace(oldChar, newChar));
return true;
}
return false;
}
/**
* 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 the Paragraph which should be deleted.
* @return False: If the int is not a valid paragraph. || True: If the int is a valid paragraph number
*/
public boolean del(int paragraphNumber) {
if (paragraphExists(paragraphNumber)) {
text.remove(paragraphNumber - 1);
return true;
}
return false;
}
/**
* This method deletes the last paragraph in the text Array.
*
* @return True: if paragraph has been deleted.
*/
public boolean del() {
if(paragraphExists(text.size())){
text.remove(text.size() - 1);
return true;
}
return false;
}
/**
* 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() {
HashMap<String,ArrayList<Integer>> wordbook = new HashMap<>();
createWordlist(wordbook);
String input;
ArrayList<String> output = new ArrayList<>();
ArrayList<Integer> values;
for (Map.Entry<String, ArrayList<Integer>> entry : wordbook.entrySet()) {
input = "";
String key = entry.getKey();
values = entry.getValue();
if (values.get(0) >= 3) {
input += key + " ";
for (int i = 1; i < values.size(); i++) {
if (i + 1 == values.size()) {
input += values.get(i) + "";
} else {
input += values.get(i) + ", ";
}
}
output.add(input);
}
}
return output;
}
/**
* 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>>
*/
private void createWordlist(HashMap<String,ArrayList<Integer>> wordbook) {
int counter;
ArrayList<Integer> numbersList;
String firstLetter;
String restLetters;
for (int i = 0; i < text.size(); i++) {
String[] words = text.get(i).trim().toLowerCase().split("[ :;.,!?><+*}{)('/\n]+");
for (String word : words) {
//Words get formatted consistently
counter = 1;
firstLetter = word.substring(0, 1);
restLetters = word.substring(1);
word = firstLetter.toUpperCase() + restLetters;
//Words are beeing counted
if (wordbook.containsKey(word)) {
numbersList = wordbook.get(word);
counter = numbersList.get(0);
numbersList.remove(0);
numbersList.add(0, counter + 1);
if (numbersList.get(numbersList.size() - 1) < i + 1) {
numbersList.add(i + 1);
}
} else {
numbersList = new ArrayList<>();
numbersList.add(counter);
numbersList.add(i + 1);
}
wordbook.put(word, numbersList);
}
}
}
}