test_entity_linker_cli.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. EntityLinker extra tests + CLI
  5. """
  6. import sys
  7. import pytest
  8. from data_modules.entity_linker import EntityLinker, main as linker_main
  9. from data_modules.index_manager import IndexManager, EntityMeta
  10. @pytest.fixture
  11. def temp_project(tmp_path):
  12. from data_modules.config import DataModulesConfig
  13. cfg = DataModulesConfig.from_project_root(tmp_path)
  14. cfg.ensure_dirs()
  15. return cfg
  16. def test_process_extraction_and_register_new_entities(temp_project):
  17. linker = EntityLinker(temp_project)
  18. idx = IndexManager(temp_project)
  19. idx.upsert_entity(
  20. EntityMeta(
  21. id="xiaoyan",
  22. type="角色",
  23. canonical_name="萧炎",
  24. current={},
  25. first_appearance=1,
  26. last_appearance=1,
  27. )
  28. )
  29. results, warnings = linker.process_extraction_result(
  30. [
  31. {
  32. "mention": "萧炎",
  33. "candidates": ["xiaoyan"],
  34. "suggested": "xiaoyan",
  35. "confidence": 0.7,
  36. },
  37. {
  38. "mention": "宗主",
  39. "candidates": ["zongzhu"],
  40. "suggested": "zongzhu",
  41. "confidence": 0.4,
  42. },
  43. ]
  44. )
  45. assert len(results) == 2
  46. assert len(warnings) == 2
  47. registered = linker.register_new_entities(
  48. [
  49. {
  50. "suggested_id": "hongyi",
  51. "name": "红衣女子",
  52. "type": "角色",
  53. "mentions": ["红衣", "女子"],
  54. }
  55. ]
  56. )
  57. assert registered == ["hongyi"]
  58. aliases = idx.get_entity_aliases("hongyi")
  59. assert "红衣女子" in aliases
  60. def test_entity_linker_cli(temp_project, monkeypatch, capsys):
  61. idx = IndexManager(temp_project)
  62. idx.upsert_entity(
  63. EntityMeta(
  64. id="xiaoyan",
  65. type="角色",
  66. canonical_name="萧炎",
  67. current={},
  68. first_appearance=1,
  69. last_appearance=1,
  70. )
  71. )
  72. def run_cli(args):
  73. monkeypatch.setattr(sys, "argv", ["entity_linker"] + args)
  74. linker_main()
  75. root = str(temp_project.project_root)
  76. run_cli(["--project-root", root, "register-alias", "--entity", "xiaoyan", "--alias", "炎帝"])
  77. run_cli(["--project-root", root, "lookup", "--mention", "炎帝"])
  78. run_cli(["--project-root", root, "lookup", "--mention", "不存在"])
  79. run_cli(["--project-root", root, "lookup-all", "--mention", "炎帝"])
  80. run_cli(["--project-root", root, "list-aliases", "--entity", "xiaoyan"])
  81. capsys.readouterr()