Skip to main content

Basic code for search engine

 class SimpleSearchEngine:

    def __init__(self):

        self.documents = []

        self.index = {}


    def add_document(self, doc_id, content):

        self.documents.append({"id": doc_id, "content": content})

        self.index_document(doc_id, content)


    def index_document(self, doc_id, content):

        words = content.split()

        for word in words:

            word = word.lower()

            if word not in self.index:

                self.index[word] = [doc_id]

            else:

                self.index[word].append(doc_id)


    def search(self, query):

        query = query.lower()

        if query in self.index:

            result = [doc["id"] for doc in self.documents if doc["id"] in self.index[query]]

            return result

        else:

            return []


if __name__ == "__main__":

    search_engine = SimpleSearchEngine()


    # Add documents to the search engine

    search_engine.add_document(1, "Python is a programming language.")

    search_engine.add_document(2, "Search engines help find information.")

    search_engine.add_document(3, "Python and Java are popular languages.")


    # Perform a search

    user_query = input("Enter your search query: ")

    search_result = search_engine.search(user_query)


    if search_result:

        print("Search Results:")

        for doc_id in search_result:

            print(f"Document {doc_id}: {search_engine.documents[doc_id-1]['content']}")

    else:

        print("No results found.")


Comments

Popular posts from this blog

Basic python

  Python is a versatile and powerful programming language known for its readability and simplicity. Here are some basic concepts and syntax in Python: Print Statement: To display output, use the print statement: python Copy code print("Hello, World!") Variables: Variables store data. No need to declare the data type explicitly. python Copy code x = 5 message = "Hello" Data Types: Python has various data types, including integers, floats, strings, lists, tuples, and dictionaries. Indentation: Python uses indentation (whitespace) to define code blocks. This is a fundamental aspect of Python's syntax. python Copy code if x > 0:     print("Positive number") Comments: Use the # symbol for single-line comments. python Copy code # This is a comment Input: To take user input, use the input() function. python Copy code name = input("Enter your name: ") Conditional Statements: Python supports if, elif, and else statements for decision-making. python...

How To secure An Iphone

Securing your iPhone is crucial to protect your personal data, privacy, and device integrity. Here are some essential tips to enhance the security of your iPhone: Enable Passcode or Face ID/Touch ID: Set up a strong alphanumeric passcode or use Face ID/Touch ID for an added layer of security. Go to Settings > Face ID & Passcode (or Touch ID & Passcode) to configure these options. Update iOS regularly: Keep your iPhone's operating system up to date by installing the latest iOS updates. These updates often include security patches that address vulnerabilities. Activate Find My iPhone: Enable Find My iPhone under Settings > [your name] > Find My > Find My iPhone. This feature allows you to locate, lock, and remotely wipe your device if it's lost or stolen. Use Two-Factor Authentication (2FA): Enable two-factor authentication for your Apple ID. This adds an extra layer of protection by requiring a verification code in addition to your password when signing in. M...

How to install windows in a pc or laptop

 Installing Windows on a laptop involves several steps. Here's a general guide for installing Windows on a laptop: Before you begin: Ensure that you have a valid Windows installation media (USB drive or DVD) and a valid product key. Backup important data on your laptop as the installation process will involve formatting the drive. Installation Steps: Insert the Installation Media: If you have a USB drive, insert it into a USB port. If you have a DVD, insert it into the laptop's optical drive. Boot from Installation Media: Restart your laptop and access the boot menu or BIOS/UEFI settings. This is usually done by pressing a specific key during startup (e.g., Esc, F2, F10, or Del). Select the option to boot from the USB drive or DVD. Windows Setup: The Windows setup screen will appear. Select your language, time, and keyboard input preferences, then click "Next." Install Now: Click on "Install Now" to start the installation process. Enter the Product Key: Ente...