Repository navigation
Expand file tree
/
Copy pathannotate.py
More file actions
103 lines (81 loc) · 3.61 KB
/
Copy pathannotate.py
File metadata and controls
103 lines (81 loc) · 3.61 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
"""
AI 标注辅助脚本
用法(由 /annotate skill 驱动,也可直接调用):
# 列出待标注行(输出 JSON,供 Claude 阅读)
python3 annotate.py --batch [--limit 30] [--platform reddit]
# 保存标注结果(Claude 生成 JSON 后调用)
python3 annotate.py --save annotations.json
# 查看标注进度
python3 annotate.py --stats
"""
import sys
import json
import argparse
from pathlib import Path
sys.path.insert(0, 'src')
from src.database import DatabaseManager
DEFAULT_DB_PATH = 'data/discussions.db'
def cmd_batch(db: DatabaseManager, limit: int, platform: str):
"""获取一批未标注行,格式化输出供 Claude 阅读"""
rows = db.get_unannotated_rows(limit=limit, platform=platform)
if not rows:
print(json.dumps({'status': 'done', 'message': '所有行已标注完毕', 'rows': []}))
return
output = {
'status': 'pending',
'count': len(rows),
'rows': []
}
for row in rows:
output['rows'].append({
'platform_id': row['platform_id'],
'search_keywords': row.get('search_keywords', ''),
'content_type': row.get('content_type', ''),
'depth': row.get('depth', 0),
'post_title': row.get('post_title', ''),
'parent_content': row.get('parent_content', ''),
'content': row.get('content', ''),
})
print(json.dumps(output, ensure_ascii=False, indent=2))
def cmd_save(db: DatabaseManager, json_file: str):
"""从 JSON 文件读取标注结果并写入 DB"""
path = Path(json_file)
if not path.exists():
print(f'错误:找不到文件 {json_file}', file=sys.stderr)
sys.exit(1)
data = json.loads(path.read_text())
annotations = data if isinstance(data, list) else data.get('annotations', [])
if not annotations:
print('错误:JSON 中没有找到 annotations 列表', file=sys.stderr)
sys.exit(1)
db.save_annotations(annotations, model=data.get('model', 'claude-sonnet-4-6') if isinstance(data, dict) else 'claude-sonnet-4-6')
print(f'✓ 已保存 {len(annotations)} 条标注')
def cmd_stats(db: DatabaseManager):
"""打印标注进度"""
stats = db.get_annotation_stats()
total = stats['total']
annotated = stats['annotated']
unannotated= stats['unannotated']
relevant = stats['relevant']
irrelevant = stats['irrelevant']
pct = f"{annotated/total*100:.1f}%" if total else "0%"
print(f"标注进度: {annotated}/{total} ({pct})")
print(f" 相关: {relevant} 无关: {irrelevant} 未标注: {unannotated}")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--batch', action='store_true', help='获取一批未标注行(JSON 输出)')
group.add_argument('--save', metavar='FILE', help='从 JSON 文件保存标注结果')
group.add_argument('--stats', action='store_true', help='查看标注进度')
parser.add_argument('--limit', type=int, default=30, help='--batch 每批数量(默认30)')
parser.add_argument('--platform', type=str, default='reddit', help='平台(默认 reddit)')
parser.add_argument('--db', type=str, default=DEFAULT_DB_PATH, help=f'数据库路径(默认 {DEFAULT_DB_PATH})')
args = parser.parse_args()
db = DatabaseManager(db_path=args.db)
if args.batch:
cmd_batch(db, args.limit, args.platform)
elif args.save:
cmd_save(db, args.save)
elif args.stats:
cmd_stats(db)