Text::Ngram(3pm)

πŸ’– Text::Ngram β€” ngram analysis of text, but make it make sense πŸ’–

okay bestie, sit down, hydrate, and let me tell you about Text::Ngram (that's the 3pm one, from the User Contributed Perl Documentation, obvi). this is n-gram analysis of text and it's lowkey iconic ✨


🌸 SYNOPSIS 🌸

here's the whole vibe in four lines, no cap:

use Text::Ngram qw(ngram_counts add_to_counts);
my $text   = "abcdefghijklmnop";
my $hash_r = ngram_counts($text, 3); # Window size = 3
# $hash_r => { abc => 1, bcd => 1, ... }

add_to_counts($more_text, 3, $hash_r);

see how easy that was?? slay πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

okay so n-Gram analysis is a whole field in textual analysis and the way that it works is it takes sliding window character sequences and uses them to help with topic analysis, language determination, and so on, periodt. the n-gram spectrum of a document can be used to compare and filter documents in multiple languages, prepare word prediction networks, and do spelling correction. it's giving multitasking queen πŸ‘‘

the neat thing about n-grams though β€” and I'm obsessed with this β€” is that they're really easy to determine. like, for n=3, for instance, we compute the n-gram counts like so:

the cat sat on the mat
---                     $counts{"the"}++;
 ---                    $counts{"he "}++;
  ---                   $counts{"e c"}++;
   ...

this module provides an efficient XS-based implementation of n-gram spectrum analysis. fast AND pretty, the duality of woman πŸŽ€

there are two functions which can be imported:

✨ ngram_counts

this first function returns a hash reference with the n-gram histogram of the text for the given window size. the default window size is 5.

$href = ngram_counts(\%config, $text, $window_size);

as of version 0.14, the %config may instead be passed in as named arguments:

$href = ngram_counts($text, $window_size, %config);

the only necessary parameter is $text. everything else can just chill πŸ’œ

the possible value for %config are:

🌈 flankbreaks

if set to 1 (default), breaks are flanked by spaces; if set to 0, they're not. breaks are punctuation and other non-alphabetic characters, which, unless you use punctuation => 0 in your configuration, do not make it into the returned hash.

here's an example, supposing you're using the default value for punctuation (1):

my $text = "Hello, world";
my $hash = ngram_counts($text, 5);

that produces the following ngrams:

{
  'Hello' => 1,
  'ello ' => 1,
  ' worl' => 1,
  'world' => 1,
}

on the other hand, this:

my $text = "Hello, world";
my $hash = ngram_counts({flankbreaks => 0}, $text, 5);

produces the following ngrams:

{
  'Hello' => 1,
  ' worl' => 1,
  'world' => 1,
}

see the difference?? one little flag, whole new world, iykyk πŸ¦‹

πŸŽ€ lowercase

if set to 0, casing is preserved. if set to 1, all letters are lowercased before counting ngrams. default is 1.

# Get all ngrams of size 4 preserving case
$href_p = ngram_counts( {lowercase => 0}, $text, 4 );

πŸ’… punctuation

if set to 0 (default), punctuation is removed before calculating the ngrams. set to 1 to preserve it.

# Get all ngrams of size 2 preserving punctuation
$href_p = ngram_counts( {punctuation => 1}, $text, 2 );

πŸ’– spaces

if set to 0 (default is 1), no ngrams containing spaces will be returned.

# Get all ngrams of size 3 that do not contain spaces
$href = ngram_counts( {spaces => 0}, $text, 3);

if you're going to request both types of ngrams, than the best way to avoid calculating the same thing twice is probably this β€” efficiency queen behavior fr fr:

$href_with_spaces = ngram_counts($text[, $window]);
$href_no_spaces = $href_with_spaces;
for (keys %$href_no_spaces) { delete $href->{$_} if / / }

✨ add_to_counts

this incrementally adds to the supplied hash; if $window is zero or undefined, then the window size is computed from the hash keys. convenient much? πŸ™ˆ

add_to_counts($more_text, $window, $href)

⭐ TO DO ⭐

  • Look further into the tests. Sort them and add more.

(she's manifesting. we love a girl with goals 🫢)


πŸ”₯ SEE ALSO πŸ”₯

  • Cavnar, W. B. (1993). N-gram-based text filtering for TREC-2. In D. Harman (Ed.), Proceedings of TREC-2: Text Retrieval Conference 2. Washington, DC: National Bureau of Standards.
  • Shannon, C. E. (1951). Predication and entropy of printed English. The Bell System Technical Journal, 30. 50-64.
  • Ullmann, J. R. (1977). Binary n-gram technique for automatic correction of substitution, deletion, insert and reversal errors in words. Computer Journal, 20. 141-147.

academic citations but make it cute πŸ“šβœ¨ the girlies were doing n-grams before it was cool


πŸ‘‘ AUTHOR πŸ‘‘

maintained by Alberto Simoes, ambs@cpan.org. πŸ’œ

previously maintained by Jose Castro, cog@cpan.org. originally created by Simon Cozens, simon@cpan.org.

generational talent pipeline honestly 🧚


Copyright 2006 by Alberto Simoes

Copyright 2004 by Jose Castro

Copyright 2003 by Simon Cozens

this library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


⚠️ bestie a lil reminder: all the facts up there are the facts. if something's marked default, it's the default. don't come for me if you flip a flag and get different ngrams, that's literally the point πŸ’…

and that's the whole page!! compiled under perl v5.42.3, dated 2026-08-20, manual page Text::Ngram(3pm) from the User Contributed Perl Documentation. go forth and count your ngrams, main character πŸŒΈβœ¨πŸ’–

πŸ“¦ where this page comes from