Java String Extract extractCharNgram(String content, int n)

Here you can find the source of extractCharNgram(String content, int n)

Description

Calculates character n-grams from a String.

License

Open Source License

Parameter

Parameter Description
content the input String
n the size of the character n-gram

Return

a list with the character n-grams

Declaration

public static List<String> extractCharNgram(String content, int n) 

Method Source Code


//package com.java2s;
/*/*from w w  w .j  ava 2s  . c  o m*/
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Calculates character n-grams from a String.
     * 
     * @param content the input String
     * @param n the size of the character n-gram
     * @return a list with the character n-grams
     */
    public static List<String> extractCharNgram(String content, int n) {
        List<String> charNgram = new ArrayList<String>();
        if (content.length() >= n) {
            for (int i = 0; i < content.length() - n; i++) {
                String cgram = "";
                for (int j = i; j < i + n; j++) {
                    cgram += content.charAt(j);
                }
                charNgram.add(cgram);

            }
        }

        return charNgram;
    }
}

Related

  1. extract(Properties p, String prefix)
  2. extract_tokens(String in_string, String delimiters)
  3. extractAggregationPrimKeysFromSql( final String sql)
  4. extractArrayData(String input, StringBuilder output)
  5. extractArrayData(String input, StringBuilder output)
  6. extractCommands(String fileContents)
  7. extractData(String message)
  8. extractField(int position, String regex, String source)
  9. extractGenericTypeNames(String sig)