CoolFace
Apppublic

executor1389/modern-search-engine

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
ranker.py71 linesDownload Raw Back to root
1import numpy as np2import re3 4class Ranker:5    """6    Simulates a Stage 3/4 Ranker using feature extraction 7    and a simple weighted scoring model (mimicking LambdaMART/Neural).8    """9    def __init__(self, weights=None):10        if weights is None:11            # Default weights for features12            self.weights = {13                "retrieval_score": 0.4,14                "title_match": 0.3,15                "exact_match": 0.2,16                "length_penalty": 0.117            }18        else:19            self.weights = weights20 21    def extract_features(self, query, doc):22        features = {}23        24        # 1. Retrieval Score (already normalized RRF or BM25)25        features["retrieval_score"] = doc.get("score", 0.0)26        27        # 2. Title Match (does the query appear in the title?)28        title = doc.get("title", "").lower()29        query_words = query.lower().split()30        title_matches = sum(1 for word in query_words if word in title)31        features["title_match"] = title_matches / max(len(query_words), 1)32        33        # 3. Exact Phrase Match34        content = doc.get("content", "").lower()35        features["exact_match"] = 1.0 if query.lower() in content else 0.036        37        # 4. Length Penalty (Prefer shorter, more concise pages for certain queries)38        content_len = len(content)39        # Normalize: 1.0 if < 1000 chars, drops to 0.0 as it approaches 5000040        features["length_penalty"] = max(0.0, 1.0 - (content_len / 50000.0))41        42        return features43 44    def score(self, query, doc):45        features = self.extract_features(query, doc)46        final_score = 0.047        for feat, value in features.items():48            final_score += value * self.weights.get(feat, 0.0)49        return final_score50 51    def rank_results(self, query, results):52        # Add final ranker scores53        for res in results:54            res["rank_score"] = self.score(query, res)55            56        # Re-sort based on rank_score57        ranked = sorted(results, key=lambda x: x["rank_score"], reverse=True)58        return ranked59 60if __name__ == "__main__":61    ranker = Ranker()62    mock_query = "Python programming"63    mock_results = [64        {"title": "Intro to Python", "url": "url1", "score": 0.5, "content": "Learn python programming today."},65        {"title": "Advanced Python", "url": "url2", "score": 0.4, "content": "Complex coding in Python."},66    ]67    68    ranked = ranker.rank_results(mock_query, mock_results)69    for res in ranked:70        print(f"Title: {res['title']}, Final Score: {res['rank_score']:.4f}")71