All Products
Search
Document Center

Platform For AI:ngram-count

Last Updated:Apr 01, 2026

N-gram Counting generates and counts n-gram frequencies across an entire corpus. The component scans all input sentences, identifies every sequence of n consecutive words (an n-gram), and tallies how often each sequence appears. The result is a global frequency count over the full corpus — not per document — which you can use directly as input to language model training.

How it works

  1. Read each sentence from the input table column you specify.

  2. Slide a window of width 1 through order (maximum n-gram length) across each sentence, producing all n-grams up to that length. For example, with order=3, the component produces unigrams, bigrams, and trigrams.

  3. Accumulate counts across all sentences. If a bag-of-words table is provided, words outside the vocabulary are replaced with <unk> before counting.

  4. Write the accumulated n-gram counts to the output table. Each row contains one n-gram and its total frequency across the corpus.

Configure the component

Two methods are available: the visual pipeline editor in Machine Learning Designer, or PAI commands via the SQL Script component.

Method 1: Configure on the pipeline page

In Machine Learning Designer (formerly Machine Learning Studio) on Platform for AI (PAI), drag the N-gram Counting component onto the canvas and configure the parameters in the right panel.

Fields Setting

ParameterDescription
Column of Sentences in Input TableThe column that contains the sentences to process.
Column of Words in the Bag-of-WordsThe column that contains the vocabulary words. Words absent from this vocabulary are replaced with <unk> in the output.
Words Column in Input Counting Result TableThe word column in an existing n-gram count table to merge into the output.
Count Column in Input Counting Result TableThe count column in an existing n-gram count table to merge into the output.
Sentence Weight ColumnThe column that contains per-sentence weights. Leave blank to apply a uniform weight of 1 to all sentences.

Parameters Setting

ParameterDescriptionDefault
Maximum N-gram LengthThe maximum n-gram length to generate. For example, setting this to 3 produces unigrams, bigrams, and trigrams — all orders from 1 up to the value you set.3

Tuning

ParameterDescription
Optional. The number of cores.The number of cores to allocate. Leave blank to let the system decide.
Optional. Memory size per core.Memory per core, in MB. Leave blank to let the system decide.

Method 2: Use PAI commands

Run the ngram_count algorithm from the algo_public project using a SQL Script component. For more information, see SQL Script.

PAI -name ngram_count
    -project algo_public
    -DinputTableName=pai_ngram_input
    -DoutputTableName=pai_ngram_output
    -DinputSelectedColNames=col0
    -DweightColName=weight
    -DcoreNum=2
    -DmemSizePerCore=1000;
ParameterRequiredDefaultDescription
inputTableNameYesThe name of the input table.
outputTableNameYesThe name of the output table. Each row in the output contains one n-gram and its total frequency across the corpus.
inputSelectedColNamesNoFirst STRING columnThe columns to read from the input table.
weightColNameNo1The weight column. A value of 1 applies uniform weighting across all sentences.
inputTablePartitionsNoAll partitionsThe partitions to read from the input table.
countTableNameNoAn existing n-gram count table to merge into the output.
countWordColNameNoSecond columnThe word column in the count table specified by countTableName.
countCountColNameNoThird columnThe count column in the count table specified by countTableName.
countTablePartitionsNoThe partitions to read from the count table.
vocabTableNameNoThe bag-of-words table. Words absent from this table are replaced with <unk>.
vocabSelectedColNameNoFirst STRING columnThe vocabulary column in the bag-of-words table.
vocabTablePartitionsNoThe partitions to read from the bag-of-words table.
orderNo3The maximum n-gram length. For example, order=3 produces unigrams, bigrams, and trigrams — all orders from 1 up to the value you set.
lifecycleNoThe lifecycle of the output table, in days.
coreNumNoThe number of cores to allocate.
memSizePerCoreNoMemory per core, in MB.