-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathratio.py
More file actions
62 lines (47 loc) · 1.87 KB
/
Copy pathratio.py
File metadata and controls
62 lines (47 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
度数统计与保存脚本
该脚本用于计算知识图谱中实体的度数,并将计算结果保存为JSON文件。具体功能包括:
- 计算每个实体的度数
- 将度数统计结果按顺序保存为JSON文件
- 使用进度条显示计算和保存过程
主要函数和变量:
- calculate_and_save_degree_counts(): 计算度数并保存结果
- progress: 全局变量,用于跟踪计算进度
使用示例:
```python
if __name__ == '__main__':
calculate_and_save_degree_counts()
"""
import json # 用于处理 JSON 数据
import os # 用于文件和目录操作
import data_preprocess # 导入数据预处理模块
from tqdm import tqdm # 用于显示进度条
progress = {
'current': 0,
'total': 0
}
def calculate_and_save_degree_counts():
"""
计算知识图谱中每个实体的度数,并将结果保存为 JSON 文件
"""
global progress
# 计算度数
degree_count = data_preprocess.knowledge_graph.calculate_degree_count()
sorted_degree_count = dict(sorted(degree_count.items()))
# 更新进度
total_progress = len(degree_count)
progress['total'] = total_progress
progress['current'] = 0 # 确保从0开始
# 将结果存入 JSON 文件
output_file = 'Data/degree/degree_counts.json'
os.makedirs(os.path.dirname(output_file), exist_ok=True)
with tqdm(total=total_progress, desc="计算并保存度数", unit="item") as pbar:
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(sorted_degree_count, f, ensure_ascii=False, indent=4)
for i in range(total_progress):
progress['current'] = i + 1
pbar.update(1)
progress['current'] = total_progress
print("成功保存文件到", output_file)
if __name__ == '__main__':
calculate_and_save_degree_counts()