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

  • SEARCH
  • Home
  • 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 3599950
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:25:57+00:00 2026-05-18T20:25:57+00:00

Just for fun I would like to count the conditional probabilities that a word

  • 0

Just for fun I would like to count the conditional probabilities that a word (from a natural language) appears in a text, depending on the last and next to last word. I.e. I would take a huge bunch of e.g. English texts and count how often each combination n(i|jk) and n(jk) appears (where j,k,i are sucsessive words).

The naive approach would be to use a 3-D array (for n(i|jk)), using a mapping of words to position in 3 dimensions. The position look-up could be done efficiently using tries (at least that’s my best guess), but already for O(1000) words I would run into memory constraints. But I guess that this array would be only sparsely filled, most entries being zero, and I would thus waste lots of memory. So no 3-D array.

What data structure would be suited better for such a use case and still be efficient to do a lot of small updates like I do them when counting the appearances of the words? (Maybe there is a completely different way of doing this?)

(Of course I also need to count n(jk), but that’s easy, because it’s only 2-D 🙂
The language of choice is C++ I guess.

  • 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-05-18T20:25:58+00:00Added an answer on May 18, 2026 at 8:25 pm

    C++ code:

    struct bigram_key{
        int i, j;// words - indexes of the words in a dictionary
    
        // a constructor to be easily constructible
        bigram_key(int a_i, int a_j):i(a_i), j(a_j){}
    
        // you need to sort keys to be used in a map container
        bool operator<(bigram_key const &other) const{
            return i<other.i || (i==other.i && j<other.j);
        }
    };
    
    struct bigram_data{
        int count;// n(ij)
        map<int, int> trigram_counts;// n(k|ij) = trigram_counts[k]
    }
    
    map<bigram_key, bigram_data> trigrams;
    

    The dictionary could be a vector of all found words like:

    vector<string> dictionary;
    

    but for better lookup word->index it could be a map:

    map<string, int> dictionary;
    

    When you read a new word. You add it to the dictionary and get its index k, you already have i and j indexes of the previous two words so then you just do:

    trigrams[bigram_key(i,j)].count++;
    trigrams[bigram_key(i,j)].trigram_counts[k]++;
    

    For better performance you may search for bigram only once:

    bigram_data &bigram = trigrams[bigram_key(i,j)];
    bigram.count++;
    bigram.trigram_counts[k]++;
    

    Is it understandable? Do you need more details?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing an application just for fun that consists of capturing images from a
I'm doing a personal, just for fun, project that is using screen scraping to
I would like to add a typing speed indicator just below the textarea we
I am (just for fun) trying to implement a High Score web-service. I would
I would like to validate and extract the hours and minutes from a string
I'm someone who writes code just for fun and haven't really delved into it
I've been developing a Smalltalk variant for just the fun of it and I
I want to develop simple Serverless LAN Chat program just for fun. How can
I've read the whole Dragon Book recently (just for fun, I'm not really planning
I'm writing a basic planet viewer OpenGL Perl application, just for fun. I have

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.