N-grams
N-grams is a probabilistic model used for predicting the next word, text, or letter. It captures language in a statistical structure as machines are better at dealing with numbers instead of text. Many companies use this approach in spelling correction and suggestions, breaking words, or summarizing text. Let's try to understand it. N-grams are simply a sequence of words or letters, mostly words. Consider the sentence "This is n-gram model"
It has four words or tokens, so it's a 4-gram; 3-grams from the same text will be "This is n-gram" and "is n-gram model". Two words are a bigram, and one word is a unigram. Let's try this using Java with OpenNLP:
String sampletext = "This is n-gram model"; System.out.println(sampletext); StringList tokens = new StringList(WhitespaceTokenizer.INSTANCE.tokenize(sampletext)); System.out.println("Tokens " + tokens); NGramModel nGramModel = new NGramModel(); nGramModel.add(tokens,3,4); ...