-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
158 lines (124 loc) · 5.08 KB
/
main.py
File metadata and controls
158 lines (124 loc) · 5.08 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""
主程序入口
处理流程控制、用户格式选择、调用模块、最终输出。
"""
import os
from datetime import datetime
from parser import parse_input_file
from retriever import search_and_retrieve_bibtex
from formatter import format_bibtex_to_style, FORMAT_STYLES
def display_format_options():
"""
显示可用的格式选项。
"""
print("\n可用的输出格式:")
print("-" * 50)
for code, description in FORMAT_STYLES.items():
print(f" {code}: {description}")
print("-" * 50)
def get_user_format_choice():
"""
获取用户选择的格式。
返回:
str: 用户选择的格式代码,如果无效则返回 None
"""
display_format_options()
while True:
choice = input("\n请选择输出格式(输入格式代码,如 GBT2015):").strip().upper()
if choice in FORMAT_STYLES:
return choice
else:
print(f"无效的格式代码:{choice}")
print("请从以下选项中选择:", ', '.join(FORMAT_STYLES.keys()))
def generate_output_filename(target_style):
"""
生成输出文件名。
参数:
target_style (str): 目标格式样式代码
返回:
str: 输出文件名
"""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
return f"output_refs_{target_style.lower()}_{timestamp}.txt"
def main():
"""
主函数:整合所有模块,执行完整的处理流程。
"""
print("=" * 60)
print("参考文献规范化与检索系统")
print("=" * 60)
# 1. 获取用户选择的格式
target_style = get_user_format_choice()
print(f"\n已选择格式:{FORMAT_STYLES[target_style]}")
# 2. 解析输入文件
input_file = 'input_refs.txt'
print(f"\n正在解析输入文件:{input_file}")
queries = parse_input_file(input_file)
if not queries:
print("错误:未能从输入文件中解析到任何文献查询。")
return
print(f"成功解析到 {len(queries)} 条文献查询。")
# 3. 处理每条查询
formatted_references = []
failed_queries = []
print("\n开始检索和格式化文献...")
print("-" * 60)
for idx, query in enumerate(queries, 1):
print(f"\n[{idx}/{len(queries)}] 处理查询:{query[:60]}...")
# 检索 BibTeX 并获取提取的关键信息
result = search_and_retrieve_bibtex(query, return_key_info=True)
if isinstance(result, tuple):
bibtex, key_info = result
else:
bibtex = result
key_info = None
if bibtex:
print(" ✓ BibTeX 检索成功")
print(" BibTeX 内容:")
print(" " + "-" * 56)
for line in bibtex.split('\n'):
print(f" {line}")
print(" " + "-" * 56)
# 如果输入包含卷期页信息,进行验证
if key_info and (key_info.get('volume') or key_info.get('pages')):
print(" 输入中的卷期页信息:")
if key_info.get('volume'):
print(f" 卷: {key_info['volume']}")
if key_info.get('number'):
print(f" 期: {key_info['number']}")
if key_info.get('pages'):
print(f" 页码: {key_info['pages']}")
print(" ⚠ 注意:请对比输入和检索结果的卷期页信息是否一致")
# 格式化
formatted_ref = format_bibtex_to_style(idx, bibtex, target_style, key_info)
formatted_references.append(formatted_ref)
print(f" ✓ 格式化完成")
else:
print(f" ✗ BibTeX 检索失败")
# 即使检索失败,也添加一个占位符
formatted_references.append(f"[{idx}] [检索失败] {query}")
failed_queries.append((idx, query))
# 4. 保存结果
output_file = generate_output_filename(target_style)
try:
with open(output_file, 'w', encoding='utf-8') as f:
f.write(f"参考文献列表 - {FORMAT_STYLES[target_style]}\n")
f.write(f"生成时间:{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
f.write("=" * 60 + "\n\n")
for ref in formatted_references:
f.write(ref + "\n")
if failed_queries:
f.write("\n" + "=" * 60 + "\n")
f.write("检索失败的文献:\n")
for idx, query in failed_queries:
f.write(f"[{idx}] {query}\n")
print("\n" + "=" * 60)
print(f"处理完成!结果已保存到:{output_file}")
print(f"成功格式化:{len(formatted_references) - len(failed_queries)} 条")
if failed_queries:
print(f"检索失败:{len(failed_queries)} 条")
print("=" * 60)
except Exception as e:
print(f"\n错误:保存输出文件时出错:{e}")
if __name__ == '__main__':
main()