import java.util.*; public class Text { private final ArrayList 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> 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 */ 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); return true; } /** * Method to get all paragraph of the existing instance. * * @return returns a ArrayList with all paragraphs inside. */ public ArrayList 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) { text.set((text.size() - 1), text.get(text.size() - 1).replace(oldChar, newChar)); 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) { 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() { text.remove(text.size() - 1); return true; } /** * This method creates a ArrayList. * 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 */ public ArrayList index() { wordbook = createWordlist(); String input; ArrayList output = new ArrayList<>(); for (Map.Entry> entry : wordbook.entrySet()) { input = ""; String key = entry.getKey(); ArrayList 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 them together with the paragraph where the word is been used to a Hashmap. * * @return HashMap> */ private HashMap> createWordlist() { int counter; ArrayList zahlenListe; String firstLetter; String restLetters; for (int i = 0; i < text.size(); i++) { String[] words = text.get(i).trim().toLowerCase().split("[ :;.,!?><'/]+"); for (String word : words) { counter = 1; firstLetter = word.substring(0, 1); restLetters = word.substring(1); word = firstLetter.toUpperCase() + restLetters; if (wordbook.containsKey(word)) { zahlenListe = wordbook.get(word); counter = zahlenListe.get(0); zahlenListe.remove(0); zahlenListe.add(0, counter + 1); if (zahlenListe.get(zahlenListe.size() - 1) < i + 1) { zahlenListe.add(i + 1); } } else { zahlenListe = new ArrayList<>(); zahlenListe.add(counter); zahlenListe.add(i + 1); } wordbook.put(word, zahlenListe); } } return wordbook; } }