#!/usr/bin/env python3
"""
诊断BM25检索器加载问题
"""
import sys
import os
sys.path.append('.')

from src.storage.bm25_store import BM25StoreManager
from src.config import Config

def test_bm25_loader():
    """测试BM25加载器"""
    print("=" * 60)
    print("诊断BM25检索器加载问题")
    print("=" * 60)
    
    try:
        # 初始化配置
        config = Config()
        print(f"配置信息:")
        print(f"  USE_BM25: {config.USE_BM25}")
        print(f"  BM25_STORAGE_DIR: {config.BM25_STORAGE_DIR}")
        print(f"  目录是否存在: {config.BM25_STORAGE_DIR.exists()}")
        
        # 检查目录内容
        if config.BM25_STORAGE_DIR.exists():
            print(f"\nBM25目录内容:")
            for file in os.listdir(config.BM25_STORAGE_DIR):
                print(f"  - {file}")
        
        # 初始化BM25管理器
        print(f"\n初始化BM25管理器...")
        manager = BM25StoreManager(config)
        
        # 尝试加载BM25检索器
        print(f"\n尝试加载BM25检索器...")
        retriever = manager.setup_bm25()
        
        if retriever:
            print(f"✅ BM25检索器加载成功")
            print(f"检索器类型: {type(retriever)}")
            
            # 测试检索
            print(f"\n测试BM25检索...")
            try:
                # 尝试检索
                results = retriever.retrieve("ai")
                print(f"✅ BM25检索成功，返回 {len(results)} 个结果")
                
                # 显示前3个结果
                for i, node in enumerate(results[:3]):
                    print(f"\n结果 {i+1}:")
                    print(f"  分数: {node.score:.4f}")
                    print(f"  内容: {node.text[:100]}...")
            except Exception as e:
                print(f"❌ BM25检索失败: {e}")
                import traceback
                traceback.print_exc()
        else:
            print(f"❌ BM25检索器加载失败")
            
    except Exception as e:
        print(f"❌ 诊断失败: {e}")
        import traceback
        traceback.print_exc()

def test_bm25_import():
    """测试BM25模块导入"""
    print("\n" + "=" * 60)
    print("测试BM25模块导入")
    print("=" * 60)
    
    try:
        # 测试导入
        from llama_index.retrievers.bm25 import BM25Retriever
        print(f"✅ BM25Retriever导入成功")
        print(f"模块路径: {BM25Retriever.__module__}")
        
        # 检查是否有from_persist_dir方法
        if hasattr(BM25Retriever, 'from_persist_dir'):
            print(f"✅ BM25Retriever有from_persist_dir方法")
        else:
            print(f"❌ BM25Retriever没有from_persist_dir方法")
            
    except ImportError as e:
        print(f"❌ BM25Retriever导入失败: {e}")
    except Exception as e:
        print(f"❌ 其他错误: {e}")
        import traceback
        traceback.print_exc()

def test_tokenizer():
    """测试分词器"""
    print("\n" + "=" * 60)
    print("测试分词器")
    print("=" * 60)
    
    try:
        from src.tokenizers import create_tokenizer
        
        # 测试中文分词器
        tokenizer = create_tokenizer(language="chinese", use_stemmer=False)
        print(f"✅ 中文分词器创建成功")
        
        # 测试分词
        text = "人工智能和机器学习"
        tokens = tokenizer(text)
        print(f"文本: '{text}'")
        print(f"分词结果: {tokens}")
        
        # 测试英文分词器
        tokenizer_en = create_tokenizer(language="english", use_stemmer=False)
        print(f"\n✅ 英文分词器创建成功")
        
        text_en = "artificial intelligence and machine learning"
        tokens_en = tokenizer_en(text_en)
        print(f"文本: '{text_en}'")
        print(f"分词结果: {tokens_en}")
        
    except Exception as e:
        print(f"❌ 分词器测试失败: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    test_bm25_import()
    test_tokenizer()
    test_bm25_loader()