AI Document Search Engine Project

Artificial Intelligence project 09

AI Document Search Engine Project

Build a complete document search engine workflow with reproducible Python code, transparent inputs, reviewable output, and responsible-use limits.

Explore AI training in VizagView all project ideas

AI objective

Rank documents for a natural-language query and return only results with positive similarity.

Data or knowledge source

Four synthetic course descriptions covering Python, Data Analytics, Artificial Intelligence, and Cloud.

Requirements

  • Python 3.10 or later
  • A terminal or command prompt
  • python -m pip install scikit-learn
  • About 45-60 minutes to build and review

How the system works

Build sublinear TF-IDF unigram and bigram vectors, transform the query with the same vocabulary, calculate cosine similarity, and rank descending.

Validation checklist

  • Query and documents share one fitted vectorizer
  • Results exclude zero-score documents
  • The requested result limit is respected
  • Document ID, text, and score are returned together

Complete Python code

Save the program as ai_document_search_engine.py. The code runs locally and requires no paid API key or model download.

"""Rank a small document collection for a natural-language query."""

from __future__ import annotations

from dataclasses import dataclass
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity


DOCUMENTS = [
    ("python", "Python training covers programming foundations, functions, files, testing, and portfolio projects."),
    ("data", "Data analytics training covers SQL, Excel, dashboards, metrics, Power BI, and business reporting."),
    ("ai", "Artificial intelligence training covers natural language processing, retrieval, model evaluation, automation, and responsible AI."),
    ("cloud", "Cloud training covers compute, storage, identity, networking, monitoring, and deployment practice."),
]


@dataclass(frozen=True)
class SearchResult:
    document_id: str
    text: str
    score: float


class DocumentSearch:
    def __init__(self, documents=DOCUMENTS):
        self.documents = list(documents)
        self.vectorizer = TfidfVectorizer(stop_words="english", ngram_range=(1, 2), sublinear_tf=True)
        self.matrix = self.vectorizer.fit_transform([text for _, text in self.documents])

    def search(self, query: str, limit: int = 3) -> list[SearchResult]:
        if limit < 1:
            return []
        scores = cosine_similarity(self.vectorizer.transform([query]), self.matrix)[0]
        order = scores.argsort()[::-1][:limit]
        return [SearchResult(self.documents[i][0], self.documents[i][1], float(scores[i])) for i in order if scores[i] > 0]


def main() -> None:
    engine = DocumentSearch()
    for result in engine.search("learn SQL and Power BI dashboards"):
        print(result)


if __name__ == "__main__":
    main()

Run the project

  1. Create and activate a virtual environment.
  2. Install dependencies with python -m pip install scikit-learn when packages are required.
  3. Run python ai_document_search_engine.py.
  4. Review confidence, fallbacks, sources, or error metrics rather than accepting output automatically.
  5. Test additional normal, edge, unsupported, and adversarial inputs.

Expected output

A ranked list of document identifiers, snippets, and similarity scores.

Accuracy, privacy, and responsible-use limits

Lexical search matches words rather than full meaning. Evaluate synonyms, spelling, language, long documents, access permissions, and stale content before deployment.

Ways to extend the project

Add chunking, metadata filters, BM25, embeddings, hybrid ranking, access-control filtering, and relevance judgements.

Continue learning Artificial Intelligence

Try the next project, return to the Softenant project library, or explore the AI training in Vizag for guided NLP, retrieval, evaluation, automation, and responsible AI practice.