A solution using split, array slices and shift. No idea if it is fast or slow as I haven't run any benchmarks.

use 5.026; use warnings; my $text = q{this is the text to play with}; for ( 1 .. 8 ) { say qq{$_-word ngrams of '$text'}; say for nGramWords( $_, $text ); say q{-} x 20; } sub nGramWords { my( $nWords, $string ) = @_; my @words = split m{\s+}, $string; my $start = 0; my @nGrams; while ( scalar @words >= $nWords ) { push @nGrams, join q{ }, qq{START INDEX: @{ [ $start ++ ] } : }, @words[ 0 .. $nWords - 1 ]; shift @words; } return @nGrams; }

The output.

1-word ngrams of 'this is the text to play with' START INDEX: 0 : this START INDEX: 1 : is START INDEX: 2 : the START INDEX: 3 : text START INDEX: 4 : to START INDEX: 5 : play START INDEX: 6 : with -------------------- 2-word ngrams of 'this is the text to play with' START INDEX: 0 : this is START INDEX: 1 : is the START INDEX: 2 : the text START INDEX: 3 : text to START INDEX: 4 : to play START INDEX: 5 : play with -------------------- 3-word ngrams of 'this is the text to play with' START INDEX: 0 : this is the START INDEX: 1 : is the text START INDEX: 2 : the text to START INDEX: 3 : text to play START INDEX: 4 : to play with -------------------- 4-word ngrams of 'this is the text to play with' START INDEX: 0 : this is the text START INDEX: 1 : is the text to START INDEX: 2 : the text to play START INDEX: 3 : text to play with -------------------- 5-word ngrams of 'this is the text to play with' START INDEX: 0 : this is the text to START INDEX: 1 : is the text to play START INDEX: 2 : the text to play with -------------------- 6-word ngrams of 'this is the text to play with' START INDEX: 0 : this is the text to play START INDEX: 1 : is the text to play with -------------------- 7-word ngrams of 'this is the text to play with' START INDEX: 0 : this is the text to play with -------------------- 8-word ngrams of 'this is the text to play with' --------------------

I hope this is of interest.

Cheers,

JohnGG


In reply to Re: improving speed in ngrams algorithm by johngg
in thread improving speed in ngrams algorithm by IB2017

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.