"""
語彙分析
"""
from pathlib import Path
from typing import Any

from fugashi import UnidicNode
from pydantic import BaseModel, ConfigDict, Field

from src.pymirage.analysis.morphological import MorphologicalAnalysis


class Word(BaseModel):
    model_config = ConfigDict(arbitrary_types_allowed=True)
    surface: str
    count: int = 1
    positions: list[tuple[int, int]] = Field(default_factory=list)
    unidic_node: UnidicNode = None


class WordsAnalysis:
    def __init__(self, nodes: list[UnidicNode]):
        self.nodes = nodes

    def analyze(self):
        data = {}

        for i, node in enumerate(self.nodes, 1):
            surface = node.surface  # 缓存，减少属性访问

            if surface in data:
                # 已存在：更新统计
                word = data[surface]
                word.count += 1
                word.positions.append((i, node.posid))
            else:
                # 首次出现：创建新记录
                data[surface] = Word(
                    surface=surface,
                    count=1,
                    positions=[(i, node.posid)],
                    unidic_node=node
                )

        return data


if __name__ == '__main__':
    file_path = Path(__file__).parents[3] / "samples/蜃気楼/蜃気楼.txt"

    with open(file_path, "r", encoding='utf-8') as f:
        text = f.read()

    analysis = MorphologicalAnalysis(text)
    results = analysis.analyze()

    wa = WordsAnalysis(nodes=results)
    data = wa.analyze()

    print(f"Count: {len(data)}")
    print("Most Frequent Words (Top 100)")
    sorted_words = sorted(data.values(), key=lambda x: x.count, reverse=True)
    for word in sorted_words[:100]:
        print(f"  {word.surface}: {word.count}次")
