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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:12:41+00:00 2026-05-16T22:12:41+00:00

def boolean_search_and(self, text): results = [] and_tokens = self.tokenize(text) tokencount = len(and_tokens) term1 =

  • 0


def boolean_search_and(self, text):

    results = []
    and_tokens = self.tokenize(text)
    tokencount = len(and_tokens)

    term1 = and_tokens[0]
    print ' term 1:', term1

    term2 = and_tokens[1]
    print ' term 2:', term2

    #for term in and_tokens:
    if term1 in self._inverted_index.keys():
        resultlist1 = self._inverted_index[term1]
        print resultlist1
    if term2 in self._inverted_index.keys():
        resultlist2 = self._inverted_index[term2]
        print resultlist2
    #intersection of two sets casted into a list                
    results = list(set(resultlist1) & set(resultlist2)) 
    print 'results:', results

    return str(results)

This code works great for two tokens, ex: text= “Hello World” and so, tokens = [‘hello’, ‘world’]. I want to generalize it for multiple tokens, so the text can be a sentence, or an entire text file.

self._inverted_index is a dictionary that saves the tokens as keys and the values are the DocIDs in which the keys/tokens occur.

hello -> [1,2,5,6]
world -> [1,3,5,7,8]
result:
hello AND world -> [1,5]

I want to achieve result for:
say,
(((hello AND computer) AND science) AND world)

I am working on making this work for multiple words, not just two. I started working in python this mornin’, so I’m unaware of a lot of features it has to offer.

Any ideas?

  • 1 1 Answer
  • 2 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-16T22:12:42+00:00Added an answer on May 16, 2026 at 10:12 pm

    I want to generalize it for multiple
    tokens

    def boolean_search_and_multi(self, text):
        and_tokens = self.tokenize(text)
        results = set(self._inverted_index[and_tokens[0]])
        for tok in and_tokens[1:]:
            results.intersection_update(self._inverted_index[tok])
        return list(results)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

def download_if_dne(href, filename): if os.path.isfile(filename): # print 'already downloaded:', href return False else: if
def self.jq_column_models COLUMN_NAME.collect {|x| {:name => x.to_s, :width => 80, :format => 'integer' if
def myFunc( a, b ): def innerFunc( c ): print c innerFunc( 2 )
def self.get(server) return unless server server = server.to_s if klass = @handlers[server] obj =
I have a function foo that accepts a Boolean function def foo( f:(_)=>Boolean )
def On_Instrumentation_StartAnimation(): Syntax : On_Instrumentation_StartAnimation() Purpose : Fired if the animation is started Parameters
def update @album = Album.find(params[:id]) if @album.update_attributes(params[:album]) redirect_to(:action=>'list') else render(:action=>'edit') end end A Rails
def partial(template, *args) options = args.extract_options! options.merge!(:layout => false) if collection = options.delete(:collection) then
def test_method [a, b, c].map {|i| yield(i) } end If I call test_method like
def any? if block_given? method_missing(:any?) { |*block_args| yield(*block_args) } else !empty? end end In

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.