All pastes #812159 Raw Edit

Etwas

public text v1 · immutable
·published 2007-12-11 15:42 UTC
rendered paste body
/**
 * Die Klasse NGramm.
 * 
 * @author (Ihr Name) 
 * @version (eine Versionsnummer oder ein Datum)
 */
public class NGramm {
// 1. Methode a) Rueckgabewert ist boolean
// Aufruf NGramm.comparison(0, 2, new String[]{"a", "b", "c", "d"})
    static boolean comparison(int i, int j, int n, String[] text) {
        for(int x = 0; x < n; x++) {
            if (text[i+x].equals(text[j+x]) == false) {
                return false;
            }
        }
        return true;
    }

//2. Methode b)
    static int countComparison(int i, int n, String[] text, int sum) {
        for(int j = 0; j < (text.length - n + 1); j++) {
            if (comparison(i, j, n, text)) {
                sum++;
            }
        }
        return sum;
    }
        
//3. Methode c) liefert ein j aus der summe von b)
    static int giveIndex(int i, int n, String[] text, int m) {
        int j = 0;
        for(int check = 0; check < m; j++) {
            if (comparison(i, j, n, text)) {
                check++;    
            }
        }
        j = j -1;
        return j;
    }
//4. Methode d) 
    static void generateNew(int n, int i, int maxWords, String[] text) {
        for(int x = 0; x < maxWords; x++) {
            System.out.print(text[i]+" ");
            int sum = 0;
            sum = countComparison(i,n, text, sum);
            int zufall = (int) (Math.random() * sum +1);
            i = giveIndex(i, n, text, zufall) +1;
            if (i >= text.length - n + 1) {
                i = (int) (Math.random() * text.length - n);
            }
        }
    }
}