Skip to content

Latest commit

 

History

History
108 lines (77 loc) · 2.65 KB

integration.rst

File metadata and controls

108 lines (77 loc) · 2.65 KB

Integration Guide

Flask Application

from flask import Flask
from src.indexer.inverted_index import InvertedIndex

app = Flask(__name__)
index = InvertedIndex.load_from_disk("my_index.pkl")

@app.route('/search')
def search():
    query = request.args.get('q', '').split()
    results = index.search(query)
    return jsonify(results)

Django Integration

# models.py
from django.db import models
from src.indexer.inverted_index import InvertedIndex

class SearchIndex(models.Model):
    name = models.CharField(max_length=100)
    file_path = models.CharField(max_length=255)

    def get_index(self):
        return InvertedIndex.load_from_disk(self.file_path)

# views.py
def search_view(request):
    index_obj = SearchIndex.objects.get(name='main')
    index = index_obj.get_index()
    query = request.GET.get('q', '').split()
    results = index.search(query)
    return JsonResponse(results)

FastAPI Integration

from fastapi import FastAPI, Query
from src.indexer.inverted_index import InvertedIndex

app = FastAPI()
index = InvertedIndex.load_from_disk("index.pkl")

@app.get("/search")
async def search(q: str = Query(...)):
    query_terms = q.split()
    results = index.search(query_terms)
    return {"results": results}

C++ Extension

# Utiliser l'extension C++ pour de meilleures performances
try:
    import velocity_inverted_index
    index = velocity_inverted_index.InvertedIndex()
except ImportError:
    from src.indexer.inverted_index import InvertedIndex
    index = InvertedIndex()

# L'API reste identique
index.add_document("doc1", ["python", "fast"])

Custom Preprocessor

from src.preprocessor.text_preprocessor import TextPreprocessor

class CustomPreprocessor(TextPreprocessor):
    def custom_normalize(self, text):
        # Logique personnalisée
        return super().normalize(text)

# Utilisation
preprocessor = CustomPreprocessor()
terms = preprocessor.preprocess("Your custom text")
index.add_document("doc1", terms)

Batch Processing

from src.indexer.batch_indexer import BatchIndexer

# Traitement par lots avec multiprocessing
indexer = BatchIndexer(n_workers=4)
documents = [("doc1", ["term1", "term2"]), ...]

result = indexer.index_documents(documents)
index = result['index']