I'm attempting to write a simple search engine (which you might know if you read my previous question) that runs off of tags combined in a list. Currently it's just in a basic ASCII output, with things like print functions and the like.

def main():
     active = True
     done = False
     results = []
     OTags = ['Hi', 'hi', ... #Continues onward]
     OResult = ["Type 'Hello' to access this page!"]
     BTags = ['Bye', 'see ya', ...]
     BResult = ["Type 'Goodbye' to access this page!"]

     while active:
          searchPrompt = raw_input('Https://')
          if searchPrompt in Otags:
               results.append(OResult)
          if searchPrompt in BTags:
               results.append(BResult)
          done = True
          while done == True:
               print results
               done = False

Now what this should do is if the user input is in either or both of those tags, it should print the results, which contain instructions on how to access the page you may be looking for. Now besides a lot of polishing I plan on doing at a later date, this should essentially cover the entire 'search' component. However, it only accepts the OTags or the BTags seperately, if I were to type 'Hi Goodbye', it would print a blank set of brackets. I think this is because of the fact that they're if statements, but the same thing happens if I use elif statements. So I was thinking, is there a sort of ANIF statement, something that would allow both of the inputs to be picked up? Any help is appreciated.

Dani AI

Generated

The core problem ran into is that the code tests the entire input string for membership in a tag list. A multi-term entry like "Hi Goodbye" does not equal "Hi" or "Goodbye", so no match is found. is right to recommend parsing the query; building on that, a practical approach is to normalize the input and then search it for each known tag (matching whole words or phrases). That allows multiple tag groups to be detected in one line and avoids false partial matches.

import re

def build_index(tag_map):
    # tag_map: iterable of (list_of_tags, result)
    compiled = []
    for tags, result in tag_map:
        patterns = [re.compile(r'(?<!\w)' + re.escape(tag) + r'(?!\w)', re.IGNORECASE)
                    for tag in tags]
        compiled.append((patterns, result))
    return compiled

def query_results(query, compiled_index):
    found = []
    for patterns, result in compiled_index:
        for pat in patterns:
            if pat.search(query):
                if isinstance(result, list):
                    found.extend(result)   # merge list results
                else:
                    found.append(result)   # single string result
                break
    return found

Notes and caveats: the example above uses negative lookarounds to treat tags as whole words/phrases (safer than naive splitting), and it handles case insensitivity. Using extend versus append avoids nested lists when the stored result is itself a list. The original snippet used raw_input (Python 2); for modern Python use input() or run under a Python 2 environment if intended. For very large tag sets, precompiling patterns (shown) helps; for thousands of patterns consider an Aho–Corasick matcher for speed. Finally, test edge cases (punctuation, overlapping tags like "see" vs "see ya", and tokens with symbols such as "C++" or "C#") and adjust the matching rules to match the desired behavior.

It seems that you are trying to define a query language. What you must do is parse that query language. For example, you could allow the user to enter several search terms separated by commas. Then parsing this query means for example split the user request into a list of search terms. For example

>>> def parse(query):
...     return [x.strip() for x in query.strip().split(',')]
... 
>>> parse("Hi, Goodbye, See ya, hello")
['Hi', 'Goodbye', 'See ya', 'hello']

You would then write tests about the resulting list in order to choose proper action.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.