src/main/java/eu/digitisation/ngram/NgramModel.java
public double logLikelihood(InputStream is, int contextLength) {
        int nchar = 0;
        double loglike = 0;
        try {
            BufferedReader reader
                    = new BufferedReader(new InputStreamReader(is));
            String context = String.valueOf(BOS);

            while (reader.ready()) {
                String line = StringNormalizer.reduceWS(reader.readLine());
                if (!line.isEmpty()) {
                    String input
                            = (context.charAt(0) == BOS)
                            ? line
                            : " " + line;

                    nchar += input.length();
                    loglike += logLikelihood(context, input);
                    if (input.length() > contextLength) {
                        context = input.substring(input.length() - contextLength);
                    } else {
                        String s = context + input;
                        context = s.substring(Math.max(0, s.length() - contextLength));
                    }
                }
                loglike += logLikelihood(context, String.valueOf(EOS));
                ++nchar;
            }
        } catch (IOException ex) {
            Messages.warning(NgramModel.class.getName() + ": " + ex.getMessage());
        }

        return loglike / nchar;
    }
/**
     * Compute the log-likelihood (per character) of the text contained in a
     * file
     *
     * @param is the InputStream containing the text
     * @param contextLength the length of the context for the evaluation of the
     * character probability
     * @return
     */
Compute the log-likelihood (per character) of the text contained in a
file

@param is the InputStream containing the text
@param contextLength the length of the context for the evaluation of the
character probability
@return
Compute the log - likelihood ( per character ) of the text contained in a file @param is the InputStream containing the text @param contextLength the length of the context for the evaluation of the character probability @return
public double logLikelihood(InputStream is, int contextLength) {
        int nchar = 0;
        double loglike = 0;
        try {
            BufferedReader reader
                    = new BufferedReader(new InputStreamReader(is));
            String context = String.valueOf(BOS);
            while (reader.ready()) {
                String line = StringNormalizer.reduceWS(reader.readLine());
                if (!line.isEmpty()) {
                    String input
                            = (context.charAt(0) == BOS)
                            ? line
                            : " " + line;
                    nchar += input.length();
                    loglike += logLikelihood(context, input);
                    if (input.length() > contextLength) {
                        context = input.substring(input.length() - contextLength);
                    } else {
                        String s = context + input;
                        context = s.substring(Math.max(0, s.length() - contextLength));
                    }
                }
                loglike += logLikelihood(context, String.valueOf(EOS));
                ++nchar;
            }
        } catch (IOException ex) {
            Messages.warning(NgramModel.class.getName() + ": " + ex.getMessage());
        }
        return loglike / nchar;
    }
public double logLikelihood ( InputStream is , int contextLength ) { int nchar = 0 ; double loglike = 0 ; try { BufferedReader reader = new BufferedReader ( new InputStreamReader ( is ) ) ; String context = String . valueOf ( BOS ) ; while ( reader . ready ( ) ) { String line = StringNormalizer . reduceWS ( reader . readLine ( ) ) ; if ( ! line . isEmpty ( ) ) { String input = ( context . charAt ( 0 ) == BOS ) ? line : " " + line ; nchar += input . length ( ) ; loglike += logLikelihood ( context , input ) ; if ( input . length ( ) > contextLength ) { context = input . substring ( input . length ( ) - contextLength ) ; } else { String s = context + input ; context = s . substring ( Math . max ( 0 , s . length ( ) - contextLength ) ) ; } } loglike += logLikelihood ( context , String . valueOf ( EOS ) ) ; ++ nchar ; } } catch ( IOException ex ) { Messages . warning ( NgramModel . class . getName ( ) + ": " + ex . getMessage ( ) ) ; } return loglike / nchar ; }
Compute the log-likelihood (per character) of the text contained in a
 file
Compute the log - likelihood ( per character ) of the text contained in a file