AI-powered Document Search
Abstract
Section titled “Abstract”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.
Prerequisites
Section titled “Prerequisites”- Python 3.8 or above
- A code editor or IDE
- Basic understanding of NLP and information retrieval
- Required libraries:
scikit-learn,numpy,pandas
Before you Start
Section titled “Before you Start”Install Python and the required libraries:
pip install scikit-learn numpy pandasGetting Started
Section titled “Getting Started”Create a Project
Section titled “Create a Project”- Create a folder named
ai-powered-document-search. - Open the folder in your code editor or IDE.
- Create a file named
ai_powered_document_search.py. - Copy the code below into your file.
Write the Code
Section titled “Write the Code”AI-powered Document Search
pch.viewSource"""
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) Example Usage
Section titled “Example Usage”python ai_powered_document_search.pyHow it fits together
Section titled “How it fits together”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.
flowchart TD RUN(["python ai_powered_document_search.py"]) DocumentSearch["DocumentSearch
class"] CLI["CLI
class"] RUN --> DocumentSearch CLI --> DocumentSearch
Explanation
Section titled “Explanation”Key Features
Section titled “Key Features”- 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.
Code Breakdown
Section titled “Code Breakdown”- What it imports (lines 11–11)
import sysDocumentSearch— the class (lines 19–34)
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 []CLI— the class (lines 36–63)
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.
Features
Section titled “Features”- 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
Next Steps
Section titled “Next Steps”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
Educational Value
Section titled “Educational Value”This project teaches:
- Information Retrieval: Semantic search and ranking
- Software Design: Modular, maintainable code
- Error Handling: Writing robust Python code
Real-World Applications
Section titled “Real-World Applications”- Enterprise Search Tools
- Content Management
- Educational Tools
Conclusion
Section titled “Conclusion”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.coffeeCtapch.feedbackHeading
pch.feedbackSubheading