How can I get all the bigrams within a given window size?
<p>Let's say I have this string</p>

<pre><code>my_string = "This is an example string"
</code></pre>

<p>And I was wondering if there's a fast way to compute all the bigrams within a given "window".</p>

<p>For instance, if the window is two words, all the possible bigrams are</p>

<pre><code>["This is","is This","is an","an is","an example","example an","example string","string example"]
</code></pre>

<p>But if the window if three words we have these bigrams for the first three-word window</p>

<pre><code>["This is","is an","This an","an this",...]
</code></pre>

<p>it's easy to get bigrams using sklearn. For instance one can do</p>

<pre><code>bigrams = CountVectorizer(analyzer = "word",
                  strip_accents = "ascii",
                  lowercase = True,
                  ngram_range = (2,2))

bigrams_counts = bigrams.fit_transform(my_string)
</code></pre>

<p>and will give you the list (and even the counts) of all bigrams, but it will only include the bigrams present in the string, not the other combinations (i.e. "This an" and "an this" won't be there).</p>

<p>So, do you know if there's a way to get all the bigrams within a given window?</p>