Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8843825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:19:51+00:00 2026-06-14T11:19:51+00:00

I needed to compute the Unigrams, BiGrams and Trigrams for a text file containing

  • 0

I needed to compute the Unigrams, BiGrams and Trigrams for a text file containing text like:

“Cystic fibrosis affects 30,000 children and young adults in the US alone
Inhaling the mists of salt water can reduce the pus and infection that fills the airways of cystic fibrosis sufferers, although side effects include a nasty coughing fit and a harsh taste.
That’s the conclusion of two studies published in this week’s issue of The New England Journal of Medicine.”

I started in Python and used the following code:

#!/usr/bin/env python
# File: n-gram.py
def N_Gram(N,text):
NList = []                      # start with an empty list
if N> 1:
    space = " " * (N-1)         # add N - 1 spaces
    text = space + text + space # add both in front and back
# append the slices [i:i+N] to NList
for i in range( len(text) - (N - 1) ):
    NList.append(text[i:i+N])
return NList                    # return the list
# test code
for i in range(5):
print N_Gram(i+1,"text")
# more test code
nList = N_Gram(7,"Here is a lot of text to print")
for ngram in iter(nList):
print '"' + ngram + '"'

http://www.daniweb.com/software-development/python/threads/39109/generating-n-grams-from-a-word

But it works for all the n-grams within a word, when I want it from between words as in CYSTIC and FIBROSIS or CYSTIC FIBROSIS. Can someone help me out as to how I can get this done?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:19:53+00:00Added an answer on June 14, 2026 at 11:19 am

    Assuming input is a string contains space separated words, like x = "a b c d" you can use the following function (edit: see the last function for a possibly more complete solution):

    def ngrams(input, n):
        input = input.split(' ')
        output = []
        for i in range(len(input)-n+1):
            output.append(input[i:i+n])
        return output
    
    ngrams('a b c d', 2) # [['a', 'b'], ['b', 'c'], ['c', 'd']]
    

    If you want those joined back into strings, you might call something like:

    [' '.join(x) for x in ngrams('a b c d', 2)] # ['a b', 'b c', 'c d']
    

    Lastly, that doesn’t summarize things into totals, so if your input was 'a a a a', you need to count them up into a dict:

    for g in (' '.join(x) for x in ngrams(input, 2)):
        grams.setdefault(g, 0)
        grams[g] += 1
    

    Putting that all together into one final function gives:

    def ngrams(input, n):
       input = input.split(' ')
       output = {}
       for i in range(len(input)-n+1):
           g = ' '.join(input[i:i+n])
           output.setdefault(g, 0)
           output[g] += 1
       return output
    
    ngrams('a a a a', 2) # {'a a': 3}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Needed to write a server text file as the output of a business process
I would like to compute time that spans between two particular moments: - start
I was using haystack on my Windows computer, but I realized I needed other
Needed Finfo but deleted msi package, so uninstalled php 5.3.0, downloaded 5.3.2 and installed.
I needed to store various strings in a map but I wanted to keep
I needed a background image for a searchbar, something similar to fotolia.com. so I
I needed to change a work item field from PlainText -> String. As I
I needed to retain the words enclosed in brackets and delete the others in
I needed to write a custom module in drupal to help out with my
i needed help. How do i get domain from a string? For example: Hi

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.