You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
164 lines
5.7 KiB
164 lines
5.7 KiB
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
新疆各政企单位机构名称匹配工具
|
|
主程序入口
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
import argparse
|
|
from datetime import datetime
|
|
|
|
# 添加src目录到Python路径
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
|
|
|
|
from config import Config
|
|
from database import DatabaseManager
|
|
from matcher import InstitutionMatcher
|
|
from output import OutputManager
|
|
|
|
|
|
def parse_arguments():
|
|
"""解析命令行参数"""
|
|
parser = argparse.ArgumentParser(description='新疆各政企单位机构名称匹配工具')
|
|
|
|
parser.add_argument('--config', '-c', default='config.yaml',
|
|
help='配置文件路径 (默认: config.yaml)')
|
|
parser.add_argument('--a-csv', default=None,
|
|
help='A_TREE CSV文件路径')
|
|
parser.add_argument('--b-csv', default=None,
|
|
help='B_TREE CSV文件路径')
|
|
parser.add_argument('--output-csv', default=None,
|
|
help='输出CSV文件路径')
|
|
parser.add_argument('--threshold', '-t', type=float, default=None,
|
|
help='相似度阈值 (默认: 0.8)')
|
|
parser.add_argument('--max-matches', '-m', type=int, default=None,
|
|
help='最大匹配数 (默认: 5)')
|
|
parser.add_argument('--verbose', '-v', action='store_true',
|
|
help='详细输出模式')
|
|
|
|
return parser.parse_args()
|
|
|
|
|
|
def main():
|
|
"""主函数"""
|
|
# 解析命令行参数
|
|
args = parse_arguments()
|
|
|
|
try:
|
|
# 初始化配置管理器
|
|
config_manager = Config(args.config)
|
|
config = config_manager.config
|
|
|
|
# 应用命令行参数覆盖配置
|
|
if args.threshold is not None:
|
|
config['matching']['similarity_threshold'] = args.threshold
|
|
if args.max_matches is not None:
|
|
config['matching']['max_matches'] = args.max_matches
|
|
if args.a_csv is not None:
|
|
config['csv']['a_csv_path'] = args.a_csv
|
|
if args.b_csv is not None:
|
|
config['csv']['b_csv_path'] = args.b_csv
|
|
if args.output_csv is not None:
|
|
config['csv']['output_csv_path'] = args.output_csv
|
|
|
|
# 设置日志级别
|
|
if args.verbose:
|
|
config['logging']['level'] = 'DEBUG'
|
|
|
|
# 初始化日志
|
|
config_manager._setup_logging()
|
|
|
|
print("="*60)
|
|
print("新疆各政企单位机构名称匹配工具")
|
|
print("="*60)
|
|
print(f"配置文件: {args.config}")
|
|
print(f"A_TREE CSV: {config['csv']['a_csv_path']}")
|
|
print(f"B_TREE CSV: {config['csv']['b_csv_path']}")
|
|
print(f"输出CSV: {config['csv']['output_csv_path']}")
|
|
print(f"相似度阈值: {config['matching']['similarity_threshold']}")
|
|
print(f"最大匹配数: {config['matching']['max_matches']}")
|
|
print(f"启用简称匹配: {config['matching']['enable_abbreviation']}")
|
|
print("="*60)
|
|
|
|
# 初始化CSV管理器
|
|
csv_manager = DatabaseManager(config['csv'])
|
|
|
|
# 读取CSV文件
|
|
print("正在读取A_TREE CSV文件...")
|
|
a_tree = csv_manager.read_a_csv()
|
|
print(f"A_TREE数据: {len(a_tree)} 条记录")
|
|
|
|
print("正在读取B_TREE CSV文件...")
|
|
b_tree = csv_manager.read_b_csv()
|
|
print(f"B_TREE数据: {len(b_tree)} 条记录")
|
|
|
|
if a_tree.empty or b_tree.empty:
|
|
print("错误: A_TREE或B_TREE数据为空")
|
|
return
|
|
|
|
# 初始化匹配器
|
|
print("正在初始化匹配器...")
|
|
matcher = InstitutionMatcher(config['matching'])
|
|
|
|
# 执行匹配
|
|
print("开始执行机构名称匹配...")
|
|
results = matcher.match_institutions(a_tree, b_tree)
|
|
|
|
if results.empty:
|
|
print("未找到任何匹配结果")
|
|
return
|
|
|
|
print(f"匹配完成,共找到 {len(results)} 个匹配结果")
|
|
|
|
# 生成匹配报告
|
|
print("正在生成匹配报告...")
|
|
report = matcher.generate_matching_report(results)
|
|
|
|
# 初始化输出管理器
|
|
output_manager = OutputManager(config['output'])
|
|
|
|
# 保存结果到CSV文件
|
|
print("正在保存匹配结果到CSV文件...")
|
|
csv_output_path = csv_manager.save_matching_results(results)
|
|
|
|
# 保存详细结果
|
|
print("正在保存详细结果...")
|
|
saved_files = output_manager.save_all_outputs(results, report)
|
|
saved_files['csv_simple'] = csv_output_path
|
|
|
|
# 打印摘要
|
|
print("\n匹配结果摘要:")
|
|
print(f"- 总匹配数: {len(results)}")
|
|
print(f"- 平均相似度: {results['similarity_score'].mean():.4f}")
|
|
print(f"- 最高相似度: {results['similarity_score'].max():.4f}")
|
|
print(f"- 最低相似度: {results['similarity_score'].min():.4f}")
|
|
|
|
# 显示匹配类型分布
|
|
match_types = results['match_type'].value_counts()
|
|
print("\n匹配类型分布:")
|
|
for match_type, count in match_types.items():
|
|
percentage = (count / len(results)) * 100
|
|
print(f" {match_type}: {count} ({percentage:.1f}%)")
|
|
|
|
print(f"\n结果文件已保存:")
|
|
for file_type, file_path in saved_files.items():
|
|
print(f" {file_type.upper()}: {file_path}")
|
|
|
|
print("\n程序执行完成!")
|
|
|
|
except KeyboardInterrupt:
|
|
print("\n程序被用户中断")
|
|
sys.exit(1)
|
|
except Exception as e:
|
|
print(f"\n程序执行出错: {e}")
|
|
sys.exit(1)
|
|
finally:
|
|
# 关闭CSV管理器
|
|
if 'csv_manager' in locals():
|
|
csv_manager.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|