All pastes #812091 Raw Edit

Etwas

public text v1 · immutable
·published 2007-12-11 14:26 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) {
        boolean compare = true;
        for(int x = 0; x < n; x++) {
            if (text[i+x].equals(text[j+x]) == false) {
                compare = false;
            }
        }
        return compare;
    }

//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++;    
            }
        }
        return j-1;
    }
//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;
        }
    }
}