Skip to content

AI-powered Document Search

AI-powered Document Search is a Python project that uses AI to perform semantic search and ranking of documents. The application features NLP-based ranking, error handling, and a CLI interface, demonstrating information retrieval and text processing techniques.

  • Python 3.8 or above
  • A code editor or IDE
  • Basic understanding of NLP and information retrieval
  • Required libraries: scikit-learn, numpy, pandas

Install Python and the required libraries:

Install dependencies
pip install scikit-learn numpy pandas
  1. Create a folder named ai-powered-document-search.
  2. Open the folder in your code editor or IDE.
  3. Create a file named ai_powered_document_search.py.
  4. Copy the code below into your file.
AI-powered Document Search pch.viewSource
AI-powered Document Search
"""
AI-powered Document Search

Features:
- Semantic document search
- NLP-based ranking
- Modular design
- CLI interface
- Error handling
"""
import sys
try:
    from sklearn.feature_extraction.text import TfidfVectorizer
    from sklearn.metrics.pairwise import cosine_similarity
except ImportError:
    TfidfVectorizer = None
    cosine_similarity = None

class DocumentSearch:
    def __init__(self):
        self.vectorizer = TfidfVectorizer() if TfidfVectorizer else None
        self.documents = []
        self.vectors = None
    def add_documents(self, docs):
        self.documents.extend(docs)
        if self.vectorizer:
            self.vectors = self.vectorizer.fit_transform(self.documents)
    def search(self, query):
        if self.vectorizer and self.vectors is not None:
            query_vec = self.vectorizer.transform([query])
            scores = cosine_similarity(query_vec, self.vectors).flatten()
            ranked = sorted(zip(self.documents, scores), key=lambda x: x[1], reverse=True)
            return ranked[:5]
        return []

class CLI:
    @staticmethod
    def run():
        print("AI-powered Document Search")
        searcher = DocumentSearch()
        while True:
            cmd = input('> ')
            if cmd.startswith('add'):
                parts = cmd.split(maxsplit=1)
                if len(parts) < 2:
                    print("Usage: add <doc1|doc2|...>")
                    continue
                docs = parts[1].split('|')
                searcher.add_documents(docs)
                print(f"Added {len(docs)} documents.")
            elif cmd.startswith('search'):
                parts = cmd.split(maxsplit=1)
                if len(parts) < 2:
                    print("Usage: search <query>")
                    continue
                query = parts[1]
                results = searcher.search(query)
                for doc, score in results:
                    print(f"Score: {score:.2f} | Doc: {doc}")
            elif cmd == 'exit':
                break
            else:
                print("Unknown command")

if __name__ == "__main__":
    try:
        CLI.run()
    except Exception as e:
        print(f"Error: {e}")
        sys.exit(1)
Run document search
python ai_powered_document_search.py

Read from the top: this is what runs when you execute the file, and which function calls which. It is generated from the code, so it cannot drift from it.

diagram Diagram mermaid
  • Semantic Search: Uses NLP for document ranking.
  • Information Retrieval: Finds relevant documents based on queries.
  • Error Handling: Validates inputs and manages exceptions.
  • CLI Interface: Interactive command-line usage.
  1. What it imports (lines 11–11)
ai_powered_document_search.py
import sys
  1. DocumentSearch — the class (lines 19–34)
ai_powered_document_search.py
class DocumentSearch:
    def __init__(self):
        self.vectorizer = TfidfVectorizer() if TfidfVectorizer else None
        self.documents = []
        self.vectors = None
    def add_documents(self, docs):
        self.documents.extend(docs)
        if self.vectorizer:
            self.vectors = self.vectorizer.fit_transform(self.documents)
    def search(self, query):
        if self.vectorizer and self.vectors is not None:
            query_vec = self.vectorizer.transform([query])
            scores = cosine_similarity(query_vec, self.vectors).flatten()
            ranked = sorted(zip(self.documents, scores), key=lambda x: x[1], reverse=True)
            return ranked[:5]
        return []
  1. CLI — the class (lines 36–63)
ai_powered_document_search.py
class CLI:
    @staticmethod
    def run():
        print("AI-powered Document Search")
        searcher = DocumentSearch()
        while True:
            cmd = input('> ')
            if cmd.startswith('add'):
                parts = cmd.split(maxsplit=1)
                if len(parts) < 2:
                    print("Usage: add <doc1|doc2|...>")
                    continue
                docs = parts[1].split('|')
                searcher.add_documents(docs)
                print(f"Added {len(docs)} documents.")
            elif cmd.startswith('search'):
                parts = cmd.split(maxsplit=1)
                if len(parts) < 2:
                # ... 4 more lines in the file ...
                for doc, score in results:
                    print(f"Score: {score:.2f} | Doc: {doc}")
            elif cmd == 'exit':
                break
            else:
                print("Unknown command")

The file defines 2 top-level symbols in all; the whole thing is above under Write the Code.

  • AI-Based Document Search: High-accuracy semantic ranking
  • Modular Design: Separate functions for search and ranking
  • Error Handling: Manages invalid inputs and exceptions
  • Production-Ready: Scalable and maintainable code

Enhance the project by:

  • Integrating with real-world document datasets
  • Supporting batch search
  • Creating a GUI with Tkinter or a web app with Flask
  • Adding evaluation metrics (precision, recall)
  • Unit testing for reliability

This project teaches:

  • Information Retrieval: Semantic search and ranking
  • Software Design: Modular, maintainable code
  • Error Handling: Writing robust Python code
  • Enterprise Search Tools
  • Content Management
  • Educational Tools

AI-powered Document Search demonstrates how to build a scalable and accurate semantic search tool using Python. With modular design and extensibility, this project can be adapted for real-world applications in enterprise, education, and more. For more advanced projects, visit Python Central Hub.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading