"""Predict a failure label with the published dependency-free model.""" from __future__ import annotations import json import math import re import sys from collections import Counter from pathlib import Path TOKEN_RE = re.compile(r"[A-Za-zÀ-ÿ0-9_]+", re.UNICODE) def tokens(text: str) -> list[str]: words = [word.casefold() for word in TOKEN_RE.findall(text)] return words + [f"{words[i]}__{words[i + 1]}" for i in range(len(words) - 1)] def predict(model: dict, text: str) -> str: query = Counter(tokens(text)) labels = model["labels"] vocab_size = len(model["vocabulary"]) total = sum(model["class_counts"].values()) best = (None, float("-inf")) for label in labels: score = math.log(model["class_counts"][label] / total) denominator = model["token_totals"][label] + vocab_size for token, count in query.items(): score += count * math.log((model["token_counts"][label].get(token, 0) + 1) / denominator) if score > best[1]: best = (label, score) return best[0] if __name__ == "__main__": model = json.loads((Path(__file__).parent / "model.json").read_text(encoding="utf-8")) text = " ".join(sys.argv[1:]) if not text: raise SystemExit("usage: python predict.py 'texto da trajetória'") print(predict(model, text))