Text.java index and createWordlist methode cleanup
This commit is contained in:
		
							parent
							
								
									7ac12a8ad5
								
							
						
					
					
						commit
						edba4d5d8c
					
				| 
						 | 
				
			
			@ -1,5 +1,3 @@
 | 
			
		|||
import java.io.File;
 | 
			
		||||
import java.io.FileNotFoundException;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
 | 
			
		||||
public class Text {
 | 
			
		||||
| 
						 | 
				
			
			@ -47,7 +45,7 @@ public class Text {
 | 
			
		|||
    /**
 | 
			
		||||
     * 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() {
 | 
			
		||||
| 
						 | 
				
			
			@ -135,20 +133,20 @@ public class Text {
 | 
			
		|||
     *
 | 
			
		||||
     * @return ArrayList<String>
 | 
			
		||||
     */
 | 
			
		||||
 | 
			
		||||
    public ArrayList<String> index() {
 | 
			
		||||
        wordbook = createWordlist();
 | 
			
		||||
        String input;
 | 
			
		||||
        ArrayList<String> output = new ArrayList<>();
 | 
			
		||||
        for (Map.Entry<String, ArrayList<Integer>> entry : wordbook.entrySet()) {
 | 
			
		||||
            input = "";
 | 
			
		||||
            String key = entry.getKey();
 | 
			
		||||
            ArrayList<Integer> values = entry.getValue();
 | 
			
		||||
            String input = "";
 | 
			
		||||
            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 {
 | 
			
		||||
                    if (i + 1 == values.size()) {
 | 
			
		||||
                        input += values.get(i) + "";
 | 
			
		||||
                    } else {
 | 
			
		||||
                        input += values.get(i) + ", ";
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
| 
						 | 
				
			
			@ -164,30 +162,32 @@ public class Text {
 | 
			
		|||
     * @return HashMap<String, ArrayList < Integer>>
 | 
			
		||||
     */
 | 
			
		||||
    private HashMap<String, ArrayList<Integer>> createWordlist() {
 | 
			
		||||
        int counter;
 | 
			
		||||
        ArrayList<Integer> zahlenListe;
 | 
			
		||||
        String firstLetter;
 | 
			
		||||
        String restLetters;
 | 
			
		||||
        for (int i = 0; i < text.size(); i++) {
 | 
			
		||||
            String[] woerter = text.get(i).trim().toLowerCase().split("[ :;.,!?><'/]+");
 | 
			
		||||
            for (String wort : woerter) {
 | 
			
		||||
                String firstLetter = wort.substring(0,1);
 | 
			
		||||
                String restLetters = wort.substring(1);
 | 
			
		||||
                wort = firstLetter.toUpperCase()+restLetters;
 | 
			
		||||
                ArrayList<Integer> zahlenListe;
 | 
			
		||||
                int zähler = 1;
 | 
			
		||||
                if (wordbook.containsKey(wort)) {
 | 
			
		||||
                    zahlenListe = wordbook.get(wort);
 | 
			
		||||
                    zähler = zahlenListe.get(0);
 | 
			
		||||
            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, zähler + 1);
 | 
			
		||||
                    zahlenListe.add(0, counter + 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(counter);
 | 
			
		||||
                    zahlenListe.add(i + 1);
 | 
			
		||||
                    wordbook.put(wort, zahlenListe);
 | 
			
		||||
                }
 | 
			
		||||
                wordbook.put(word, zahlenListe);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return wordbook;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -16,10 +16,13 @@ public class TextTest {
 | 
			
		|||
 | 
			
		||||
    @Test
 | 
			
		||||
    void indexTest() {
 | 
			
		||||
        txt.add("Hallo Hallo zusammen !test!");
 | 
			
		||||
        txt.add("Hallo ?Hallo> zusammen !test!");
 | 
			
		||||
        txt.add("Hallo,    wie zusammen");
 | 
			
		||||
        txt.add("Hallo,wie wie zusammen");
 | 
			
		||||
        txt.add("Hallo! Wie wie zusammen");
 | 
			
		||||
        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));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue