Create Basic "Search Engine" in Python

The following script is a very basic search engine in python. It takes user input, checks it against the so-called indexed content and returns the results in decreasing order of match cases.

   def matches(userQuery, indexedData):
    count = 0
    for wordQ in userQuery.split(" "):
        for wordS in indexedData.split(" "):
            if wordQ == wordS.lower():
                count += 1      
    # return f"{count} Result(s) found!"
    return count
    # print(f"{count} Result(s) found!")

if __name__ == "__main__":
    
    sentences = ["python is a beginner-friendly programming language", 
                "it is good to start with python your coding journey",
                "There are many programming languages",
                "youtube shorts is a great way to create videos instantly",
                "car is goood for convenience, bike is better when you are solo"]
    
    query = input("Plz input your query >>> ").lower()

    scores = [matches(query, sentence) for sentence in sentences]
    
    # print(scores)
    zipped = sorted(zip(scores, sentences), reverse=True)
    
    matchedCase = [sentScore for sentScore in zipped if sentScore[0] != 0]
    # print(zipped)
    
    print(f"{len(matchedCase)} Results found!")
    # print("Results found are: ")
    for index, item in matchedCase:
        # if index != 0:
        print(f"\"{item}\" with {index} matching(s)")

    

Comments

Popular posts from this blog

Quotation marks to wrap an element in HTML

The Basic Structure of a Full-Stack Web App

Unlocking Web Design: A Guide to Mastering CSS Layout Modes