MedResearcher/info_extractor.py
iomgaa c4037325ed feat: 实现基于LangExtract框架的MIMIC论文信息提取系统
- 新增info_extractor.py主文件,支持命令行参数和测试模式
- 实现src/extractor.py核心MIMICLangExtractBuilder类
- 集成vllm API服务(OpenAI兼容格式)进行结构化信息提取
- 支持5大模块提取:数据集、模型、训练、评估、环境配置
- 实现源文本定位和交互式HTML可视化
- 添加langextract和httpx[socks]依赖
- 支持个性化论文子目录结果保存
- 清理过时的experiment_runner.py和number_extraction_models.py文件
2025-08-25 20:51:30 +08:00

139 lines
4.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
基于LangExtract的MIMIC论文信息提取器
从医学论文中提取结构化的复现任务信息
作者MedResearcher项目
创建时间2025-01-25
"""
import argparse
import logging
from src.extractor import MIMICLangExtractBuilder
# 配置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
def setup_args():
"""设置命令行参数解析
Returns:
argparse.Namespace: 解析后的命令行参数
"""
parser = argparse.ArgumentParser(
description='MIMIC论文信息提取工具 - 基于LangExtract从医学论文中提取结构化复现信息',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog='''
使用示例:
%(prog)s # 使用默认参数
%(prog)s --papers_dir dataset/markdowns # 指定论文目录
%(prog)s --output_file results/dataset.json # 指定输出文件
%(prog)s --test_mode --max_papers 5 # 测试模式只处理5篇论文
'''
)
parser.add_argument(
'--papers_dir',
type=str,
default='dataset/markdowns',
help='markdown论文文件目录 (默认: dataset/markdowns)'
)
parser.add_argument(
'--output_file',
type=str,
default='dataset/reproduction_tasks/mimic_langextract_dataset.json',
help='输出数据集文件路径 (默认: dataset/reproduction_tasks/mimic_langextract_dataset.json)'
)
parser.add_argument(
'--test_mode',
action='store_true',
help='测试模式,只处理少量论文进行验证'
)
parser.add_argument(
'--max_papers',
type=int,
default=None,
help='最大处理论文数量,用于测试 (默认: 处理所有论文)'
)
parser.add_argument(
'--log_level',
type=str,
default='INFO',
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR'],
help='日志级别 (默认: INFO)'
)
return parser.parse_args()
def main():
"""主函数 - 执行MIMIC论文信息提取任务"""
try:
# 解析命令行参数
args = setup_args()
# 设置日志级别
logging.getLogger().setLevel(getattr(logging, args.log_level))
# 初始化信息提取器
builder = MIMICLangExtractBuilder()
print(f"=== MIMIC论文信息提取工具启动 ===")
print(f"论文目录: {args.papers_dir}")
print(f"输出文件: {args.output_file}")
print(f"测试模式: {'' if args.test_mode else ''}")
if args.max_papers:
print(f"最大论文数: {args.max_papers}")
print(f"日志级别: {args.log_level}")
print(f"========================")
# 构建复现数据集
print("\n开始构建MIMIC复现数据集...")
dataset = builder.build_reproduction_dataset(
papers_dir=args.papers_dir,
output_file=args.output_file,
max_papers=args.max_papers if args.test_mode or args.max_papers else None
)
# 统计结果
total_papers = dataset['metadata']['total_papers']
successful_extractions = sum(
1 for paper in dataset['papers'].values()
if any(module.get('extraction_count', 0) > 0
for module in paper.get('modules', {}).values())
)
print(f"\n=== 构建完成 ===")
print(f"总论文数: {total_papers}")
print(f"成功提取: {successful_extractions}/{total_papers}")
print(f"成功率: {successful_extractions/total_papers*100:.1f}%")
print(f"结果保存至: {args.output_file}")
print(f"交互式报告: {args.output_file.replace('.json', '.html')}")
print(f"===============")
return 0
except FileNotFoundError as e:
print(f"错误: 找不到指定的文件或目录 - {e}")
return 1
except ValueError as e:
print(f"错误: 参数值无效 - {e}")
return 1
except Exception as e:
print(f"错误: 程序执行异常 - {e}")
logging.exception("详细错误信息:")
return 1
if __name__ == "__main__":
exit_code = main()
exit(exit_code)