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 7501273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:28:28+00:00 2026-05-29T20:28:28+00:00

Getting started with python. I am trying to implement positional index using nested dictionary.

  • 0

Getting started with python. I am trying to implement positional index using nested dictionary. However I am not sure if thats the way to go. Index should contain term/term frequency/doc id/term position.

Example:

dict = {term: {termfreq: {docid: {[pos1,pos2,...]}}}}

My question is: am i on the right track here or is there a better solution to my problem. If nested dictionary is the way to go i have one additional question: how do I get single items out of the dictionary: for example term frequency for a term (without all the additional infromation about the term).
Help on this is greatly appreciated.

  • 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-29T20:28:30+00:00Added an answer on May 29, 2026 at 8:28 pm

    Each term seems to have a term frequency, a doc id, and a list of positions. Is that right? If so, you could use a dict of dicts:

    dct = { 'wassup' : {
                'termfreq' : 'daily',
                'docid' : 1,
                'pos' : [3,4] }}
    

    Then, given a term, like ‘wassup’, you could look up the term frequency with

    dct['wassup']['termfreq']
    # 'daily'
    

    Think of a dict as being like a telephone book. It is great at looking up values (phone numbers) given keys (names). It is not so hot at looking up keys given values. Use a dict when you know you need to look things up in a one-way direction. You may need some other data structure (a database perhaps?) if your lookup patterns is more complex.


    You might also want to check out the Natural Language Toolkit (nltk). It has a method for calculating tf_idf built in:

    import nltk
    
    # Given a corpus of texts
    text1 = 'Lorem ipsum FOO dolor BAR sit amet'
    text2 = 'Ut enim ad FOO minim veniam, '
    text3 = 'Duis aute irure dolor BAR in reprehenderit '
    text4 = 'Excepteur sint occaecat BAR cupidatat non proident'
    
    # We split the texts into tokens, and form a TextCollection
    mytexts = (
        [nltk.word_tokenize(text) for text in [text1, text2, text3, text4]])
    mycollection = nltk.TextCollection(mytexts)
    
    # Given a new text
    text = 'et FOO tu BAR Brute'
    tokens = nltk.word_tokenize(text)
    
    # for each token (roughly, word) in the new text, we compute the tf_idf
    for word in tokens:
        print('{w}: {s}'.format(w = word,
                                s = mycollection.tf_idf(word,tokens)))
    

    yields

    et: 0.0
    FOO: 0.138629436112
    tu: 0.0
    BAR: 0.0575364144904
    Brute: 0.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just getting started with flex and am using the SDK (not Flex
I'm getting started with using python's mrjob to convert some of my long running
I'm just getting started with Tk and using it in python. I set up
So I'm just getting started with Python, and currently working my way through http://diveintopython3.ep.io/
I'm just getting started with Python, and am trying to figure out the Right
I'm kind of just getting started with Python, and I'm trying to match the
Getting started with NHibernate How can I generate identity fields in nHibernate using Hilo
Getting started with TDD and I want to ground up a Repository-driven Model. However,
Just getting started with Linq to SQL so forgive the newbie question. I'm trying
Just getting started using MVC in ASP.NET, I'm going to have it so users

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.