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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:44:26+00:00 2026-05-20T23:44:26+00:00

The goal is to be able to compare words in a document to the

  • 0

The goal is to be able to compare words in a document to the words in a set of documents as fast as possible (create a term-document matrix). If possible can this be done (and will it be fast) by using Lucene?

My thought (if done by me) is to create a tree of terms in each document and then combine the trees together to make the set. To create the trees, I would use nested dictionaries with each dictionary key being a character. Each position in the term would be a different level in the heirarchy

Positions

1
 2
  3
   4
    5
     6

For example, using a sample string “This is a test” the tree would look like

t
 h
  i
   s
 e
  s
   t
i
 s
a

Notice the ‘t’ in the first level is there only once. The first dictionary would contain the keys {‘t’,’i’,’a’}. There would be three second level dictionaries containing the keys {‘h’}{‘e’}{‘s’}.

This should make look up extremly fast. The max number of steps in a loop would be the number of characters in the longest word. The part that is making my brain fold in on itself, is how do I dynamically build a dictionary like this, specifically accessing the correct level

So far I have something to the effect of

def addTerm(self, term):
   current_level = 0;
   for character in list(term):
      character = character.lower()
      if re.match("[a-z]",character):
         self.tree[character] = {}
         current_level += 1 
  • 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-20T23:44:26+00:00Added an answer on May 20, 2026 at 11:44 pm

    I can see a few problems with your current implementation. How do you mark if a node in the trie is a word? A better implementation would be to initialize tree to something like tree = [{}, None] where None indicates if the current node is the end of a word.

    Your addTerm method could then be something like:

    def addTerm(self, term):
       node = self.tree
       for c in term:
          c = c.lower()
          if re.match("[a-z]",c):
             node = node[0].setdefault(c,[{},None])
       node[1] = term
    

    You could set node[1] to True if you don’t care about what word is at the node.

    Searching if a word is in the trie would be something like

    def findTerm(self, term):
        node = self.tree
        for c in term:
            c = c.lower()
            if re.match("[a-z]",c):
                if c in node[0]:
                    node = node[0][c]
                else:
                    return False
        return node[1] != None
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My goal is to be able to quickly type this: <%= %> Can anyone
My goal is to be able to create a soap request that can contain
The end goal of this part of my project is to be able to
The goal is to be able to pronounce something like wo3. System.Speech can handle
My goal is to be able to assign each tea their own ID, compare
Here is my goal: in a Google Document, I want to be able to
My goal is to be able to write this in XAML: <Grid> <Rectangle Fill=AliceBlue
i'm trying to make my own markdown-able textarea like Stackoverflow has done. The goal
My goal is to be able to call functions that are inside my JQuery
My goal is to be able to have the user select a range of

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.