test_classification_optimization.py 4.3 KB

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