test_classification_optimization.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. 三级分类优化效果测试
  5. 测试优化后的分类器对"验收内容"等内容的分类效果
  6. """
  7. import asyncio
  8. import sys
  9. from pathlib import Path
  10. project_root = Path(__file__).parent.parent.parent
  11. sys.path.insert(0, str(project_root))
  12. from core.construction_review.component.reviewers.utils.llm_content_classifier_v2 import (
  13. ContentClassifierClient,
  14. SectionContent,
  15. CategoryStandardLoader,
  16. SecondCategoryStandardLoader
  17. )
  18. async def test_classification_optimization():
  19. """测试分类优化效果"""
  20. print("=" * 60)
  21. print("三级分类优化效果测试")
  22. print("=" * 60)
  23. # 加载标准分类
  24. csv_path = project_root / "core/construction_review/component/doc_worker/config/StandardCategoryTable.csv"
  25. loader = CategoryStandardLoader(csv_path)
  26. second_csv_path = project_root / "core/construction_review/component/doc_worker/config/construction_plan_standards.csv"
  27. second_loader = SecondCategoryStandardLoader(second_csv_path) if second_csv_path.exists() else None
  28. # 创建分类客户端
  29. classifier = ContentClassifierClient(
  30. model="qwen3.5-122b-a10b",
  31. semaphore=asyncio.Semaphore(5),
  32. embedding_client=None,
  33. second_category_loader=second_loader
  34. )
  35. # 测试用例1: 验收内容
  36. print("\n" + "-" * 60)
  37. print("测试用例1: 验收内容分类")
  38. print("-" * 60)
  39. test_content_1 = """<1> 三、验收内容
  40. <2> 针对项目认定的关键工序进行验收,按照相关规范进行验收,其检验方法:检
  41. <3> 查质量证明文件、观察、尺量、测量、砼强度检测等。验收内容及合格标准如下:
  42. <4> 1、材料、机具检查验收对所有材料和机械进行进场登记验收,清退不合格材料和机械。
  43. <5> 2、钢筋、预应力钢绞线、锚夹具、波纹管、压浆料、起吊钢绳、吊具等材料应具有出厂质量证明书和试验报告单。
  44. <6> 3、进场时除检查其外观和标志外,按不同的品种、等级、牌号、规格及生产厂家分批抽取试样进行性能检验。
  45. <7> 4、检验试验方法应符合现行国家标准的规定。
  46. <8> 5、施工工艺验收包括模板安装工艺验收、混凝土浇筑工艺验收等。
  47. <9> 6、机械设备验收包括塔式起重机验收、混凝土泵车验收等。
  48. <10> 7、临时支撑结构验收包括脚手架验收、满堂支架验收等。"""
  49. lines = test_content_1.strip().split('\n')
  50. standards = loader.get_standards_by_second_code("Content") # 验收内容
  51. section = SectionContent(
  52. section_key="test_acceptance",
  53. section_name="验收内容",
  54. lines=lines,
  55. numbered_content=test_content_1,
  56. category_standards=standards,
  57. line_number_map=list(range(1, len(lines) + 1))
  58. )
  59. print(f"二级分类: 验收内容 (Content)")
  60. print(f"三级分类标准数量: {len(standards)}")
  61. print(f"测试内容行数: {len(lines)}")
  62. if standards:
  63. print(f"\n三级分类标准列表:")
  64. for std in standards[:5]:
  65. print(f" - {std.third_name} ({std.third_code})")
  66. # 调用分类器
  67. result = await classifier.classify_content(section)
  68. print(f"\n分类结果:")
  69. print(f" 成功: {result.error is None}")
  70. print(f" 耗时: {result.latency:.2f}s")
  71. print(f" 分类数量: {len(result.classified_contents)}")
  72. print(f" 分类率: {result.coverage_rate:.1f}%")
  73. if result.classified_contents:
  74. print(f"\n识别到的三级分类:")
  75. for content in result.classified_contents:
  76. print(f" - {content.third_category_name} ({content.third_category_code})")
  77. print(f" 行号: {content.start_line} - {content.end_line}")
  78. # 检查是否误分类为"非标准项"
  79. no_standard_count = sum(1 for c in result.classified_contents if c.third_category_code == "no_standard")
  80. standard_count = len(result.classified_contents) - no_standard_count
  81. print(f"\n分类统计:")
  82. print(f" 标准分类: {standard_count}")
  83. print(f" 非标准项: {no_standard_count}")
  84. if no_standard_count > standard_count:
  85. print(f" [警告] 非标准项分类过多!")
  86. else:
  87. print(f" [OK] 标准分类占多数")
  88. print("\n" + "=" * 60)
  89. print("测试完成")
  90. print("=" * 60)
  91. if __name__ == "__main__":
  92. asyncio.run(test_classification_optimization())