File size: 2,760 Bytes
c7db54a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
Docling integration — Match Decoded PDF match report analysis
"""
import os
import logging
from typing import Optional

logger = logging.getLogger(__name__)

# ─── Try to import Docling ─────────────────────────────
DOCLING_AVAILABLE = False
try:
    from docling.document_converter import DocumentConverter
    DOCLING_AVAILABLE = True
    logger.info("Docling available for PDF analysis")
except ImportError:
    logger.info("Docling not installed — using PyMuPDF fallback")


def parse_pdf(file_path: str) -> Optional[str]:
    """Parse a PDF file and extract text content.

    Uses IBM Docling if available, falls back to PyMuPDF.
    """
    if not os.path.exists(file_path):
        logger.error(f"File not found: {file_path}")
        return None

    try:
        if DOCLING_AVAILABLE:
            converter = DocumentConverter()
            doc = converter.convert(file_path)
            return doc.text
        else:
            return _fallback_extract_text(file_path)
    except Exception as e:
        logger.error(f"PDF parsing failed: {e}")
        try:
            return _fallback_extract_text(file_path)
        except Exception as e2:
            logger.error(f"Fallback also failed: {e2}")
            return None


def _fallback_extract_text(file_path: str) -> str:
    """Extract text from PDF using PyMuPDF (lightweight fallback)."""
    import fitz
    doc = fitz.open(file_path)
    text_parts = []
    for page in doc:
        text_parts.append(page.get_text())
    doc.close()
    return "\n".join(text_parts)


def extract_match_details(file_path: str) -> Optional[dict]:
    """Extract structured match details from a PDF match report.

    Returns a dict with keys: text, teams, score, tournament, date
    """
    text = parse_pdf(file_path)
    if not text:
        return None

    result = {
        "text": text,
        "teams": [],
        "score": None,
        "tournament": None,
        "date": None,
    }

    lines = text.strip().split("\n")
    for i, line in enumerate(lines):
        line = line.strip()
        lower = line.lower()
        if " vs " in line or " v " in line:
            parts = line.split(" vs ") if " vs " in line else line.split(" v ")
            if len(parts) == 2:
                result["teams"] = [parts[0].strip(), parts[1].strip()]
        if "tournament" in lower or "competition" in lower or "cup" in lower:
            result["tournament"] = line
        if any(c.isdigit() for c in line) and "-" in line and len(line) < 20:
            candidates = [s.strip() for s in line.split("-")]
            if all(c.isdigit() or c == "" for c in candidates):
                result["score"] = line

    return result