44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
"""
|
|
测试语义缓存的相似度计算
|
|
"""
|
|
import sys
|
|
import os
|
|
|
|
# 添加项目路径
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from utils.semantic_cache_service import SemanticCacheService
|
|
|
|
def test_similarity():
|
|
"""测试相似度计算"""
|
|
service = SemanticCacheService()
|
|
|
|
# 测试问题对
|
|
test_cases = [
|
|
("公司有什么产品?", "公司有什么产品?"),
|
|
("公司有什么产品?", "公司有哪些产品?"),
|
|
("公司有什么产品?", "你们公司有哪些产品?"),
|
|
("公司有什么产品?", "今天天气怎么样?"),
|
|
]
|
|
|
|
print("🔍 相似度计算测试")
|
|
print("=" * 60)
|
|
|
|
for q1, q2 in test_cases:
|
|
similarity = service._calculate_text_similarity(q1, q2)
|
|
keywords1 = service._extract_keywords(q1)
|
|
keywords2 = service._extract_keywords(q2)
|
|
|
|
threshold = service.SIMILARITY_THRESHOLD
|
|
status = "✅ 命中" if similarity >= threshold else "❌ 未命中"
|
|
|
|
print(f"\n问题1: {q1}")
|
|
print(f"问题2: {q2}")
|
|
print(f"关键词1: {keywords1}")
|
|
print(f"关键词2: {keywords2}")
|
|
print(f"相似度: {similarity:.3f} (阈值: {threshold})")
|
|
print(f"状态: {status}")
|
|
|
|
if __name__ == "__main__":
|
|
test_similarity()
|