Initial: integrated 2025 LawGraph (graphrag_pipeline) + 2026 kg_project
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
# 使用示例
|
||||
|
||||
## 示例1: 从单个文档提取实体
|
||||
|
||||
```python
|
||||
from src.preprocessing.document_parser import DocumentParser
|
||||
from src.extraction.ner import NERExtractor
|
||||
from src.utils.config import load_config
|
||||
|
||||
# 加载配置
|
||||
config = load_config()
|
||||
|
||||
# 解析文档
|
||||
parser = DocumentParser()
|
||||
doc = parser.parse_docx("../data/1法律/3-中华人民共和国城乡规划法.docx")
|
||||
|
||||
# 提取实体
|
||||
ner_extractor = NERExtractor(config=config)
|
||||
entities = ner_extractor.extract(doc["text"])
|
||||
|
||||
print(f"识别到 {sum(len(v) for v in entities.values())} 个实体")
|
||||
```
|
||||
|
||||
## 示例2: 构建知识图谱
|
||||
|
||||
```python
|
||||
from src.preprocessing.document_parser import DocumentParser
|
||||
from src.kg_builder.indexer import GraphIndexer
|
||||
from src.utils.config import load_config
|
||||
|
||||
# 加载配置
|
||||
config = load_config()
|
||||
|
||||
# 解析文档
|
||||
parser = DocumentParser()
|
||||
docs = parser.parse_directory("../data/1法律")
|
||||
|
||||
# 切分为TextUnit
|
||||
all_textunits = []
|
||||
for doc in docs[:3]: # 只处理前3个文档
|
||||
textunits = parser.split_into_textunits(doc, max_length=500)
|
||||
all_textunits.extend(textunits)
|
||||
|
||||
# 构建知识图谱
|
||||
indexer = GraphIndexer(config=config)
|
||||
kg = indexer.index(all_textunits)
|
||||
|
||||
print(f"图谱包含 {kg.number_of_nodes()} 个节点,{kg.number_of_edges()} 条边")
|
||||
```
|
||||
|
||||
## 示例3: 查询知识图谱
|
||||
|
||||
```python
|
||||
from src.kg_builder.graph import KnowledgeGraph
|
||||
from src.query.global_search import GlobalSearcher
|
||||
from src.query.local_search import LocalSearcher
|
||||
|
||||
# 加载图谱
|
||||
kg = KnowledgeGraph()
|
||||
kg.load("output/kg.json")
|
||||
|
||||
# 全局搜索
|
||||
global_searcher = GlobalSearcher(kg.graph, communities={})
|
||||
result = global_searcher.search("城市更新的法规要求")
|
||||
print(result["answer"])
|
||||
|
||||
# 局部搜索
|
||||
local_searcher = LocalSearcher(kg.graph)
|
||||
result = local_searcher.search("城乡规划", depth=2)
|
||||
print(f"找到 {result['subgraph']['num_nodes']} 个相关节点")
|
||||
```
|
||||
|
||||
## 示例4: 法理结构提取
|
||||
|
||||
```python
|
||||
from src.analysis.legal_structure import LegalStructureExtractor
|
||||
|
||||
extractor = LegalStructureExtractor()
|
||||
|
||||
# 提取四要素
|
||||
text = "为了促进城市可持续发展,应当坚持生态优先、绿色发展原则..."
|
||||
elements = extractor.extract_four_elements(text)
|
||||
print(elements)
|
||||
|
||||
# 提取跨法规引用
|
||||
references = extractor.extract_cross_references(text)
|
||||
print(references)
|
||||
```
|
||||
|
||||
## 示例5: 三元组评估
|
||||
|
||||
```python
|
||||
from src.extraction.evaluator import TripletEvaluator
|
||||
|
||||
evaluator = TripletEvaluator()
|
||||
|
||||
# 评估多个模型的结果
|
||||
peer_groups = {
|
||||
"model_1": [{"head": "A", "relation": "管控", "tail": "B"}],
|
||||
"model_2": [{"head": "A", "relation": "涉及", "tail": "B"}],
|
||||
}
|
||||
|
||||
result = evaluator.evaluate_peer_groups(
|
||||
peer_groups,
|
||||
source_text="原始文本..."
|
||||
)
|
||||
|
||||
print(f"最佳模型: {result['best_group']}")
|
||||
print(f"评分: {result['scores']}")
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user