File size: 1,691 Bytes
734b5b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Dict, Any
from .base_agent import LLMAgent


class LanguageDetectionAgent(LLMAgent):
    name = "language_detection_agent"
    description = (
        "Detects document language and handles multilingual content appropriately"
    )

    def build_prompt(self, text: str) -> str:
        sample = text[:500] if text else "No text available"
        return f"""Detect the language of this text sample:

{sample}

Respond with:
1. Primary language (name and ISO code)
2. Confidence level (high/medium/low)
3. Are there multiple languages present?
4. Any special characters or scripts detected?

Be specific about the detection basis."""

    def execute(self, text: str, reasoning: str) -> Dict[str, Any]:
        return {
            "language": self._extract_lang(reasoning),
            "confidence": self._extract_confidence(reasoning),
            "detected": True,
            "multilingual": "multiple" in reasoning.lower(),
            "reasoning": reasoning,
        }

    def _extract_lang(self, reasoning: str) -> str:
        reasoning_lower = reasoning.lower()
        langs = {
            "estonian": "et",
            "english": "en",
            "german": "de",
            "russian": "ru",
            "finnish": "fi",
            "swedish": "sv",
            "latvian": "lv",
            "lithuanian": "lt",
        }
        for name, code in langs.items():
            if name in reasoning_lower:
                return code
        return "en"

    def _extract_confidence(self, reasoning: str) -> float:
        if "high" in reasoning.lower():
            return 0.9
        if "medium" in reasoning.lower():
            return 0.7
        return 0.5