Skip to content

Commit f81a8e9

Browse files
committed
改进去除程序
1 parent 753ec06 commit f81a8e9

3 files changed

Lines changed: 75 additions & 20 deletions

File tree

ai/enhance.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,37 @@ def parse_args():
2424
parser.add_argument("--data", type=str, required=True, help="jsonline data file")
2525
return parser.parse_args()
2626

27+
def load_existing_ids(output_file):
28+
"""加载输出文件中已有的论文ID"""
29+
existing_ids = set()
30+
if os.path.exists(output_file):
31+
try:
32+
with open(output_file, "r", encoding='utf-8') as f:
33+
for line in f:
34+
line = line.strip()
35+
if line:
36+
try:
37+
data = json.loads(line)
38+
if 'id' in data:
39+
existing_ids.add(data['id'])
40+
except json.JSONDecodeError:
41+
continue
42+
except Exception as e:
43+
print(f"读取现有文件时出错: {e}", file=sys.stderr)
44+
return existing_ids
45+
2746
def main():
2847
args = parse_args()
2948
model_name = os.environ.get("MODEL_NAME", 'deepseek-chat')
3049
language = os.environ.get("LANGUAGE", 'Chinese')
3150

51+
# 构建输出文件名
52+
output_file = args.data.replace('.jsonl', f'_AI_enhanced_{language}.jsonl')
53+
54+
# 加载已有的论文ID
55+
existing_ids = load_existing_ids(output_file)
56+
print(f'已有论文数量: {len(existing_ids)}', file=sys.stderr)
57+
3258
data = []
3359
with open(args.data, "r") as f:
3460
for line in f:
@@ -37,11 +63,20 @@ def main():
3763
seen_ids = set()
3864
unique_data = []
3965
for item in data:
40-
if item['id'] not in seen_ids:
41-
seen_ids.add(item['id'])
66+
item_id = item['id']
67+
# 同时检查当前批次的重复和输出文件中的重复
68+
if item_id not in seen_ids and item_id not in existing_ids:
69+
seen_ids.add(item_id)
4270
unique_data.append(item)
71+
elif item_id in existing_ids:
72+
print(f'跳过已处理的论文: {item_id}', file=sys.stderr)
4373

4474
data = unique_data
75+
print(f'需要处理的新论文数量: {len(data)}', file=sys.stderr)
76+
77+
if not data:
78+
print('没有需要处理的新论文', file=sys.stderr)
79+
return
4580

4681
print('Open:', args.data, file=sys.stderr)
4782

@@ -70,8 +105,8 @@ def main():
70105
"result": "Error",
71106
"conclusion": "Error"
72107
}
73-
with open(args.data.replace('.jsonl', f'_AI_enhanced_{language}.jsonl'), "a") as f:
74-
f.write(json.dumps(d) + "\n")
108+
with open(output_file, "a", encoding='utf-8') as f:
109+
f.write(json.dumps(d, ensure_ascii=False) + "\n")
75110

76111
print(f"Finished {idx+1}/{len(data)}", file=sys.stderr)
77112

daily_arxiv/daily_arxiv/pipelines.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
class DuplicatesPipeline:
17-
"""去重Pipeline,支持跨天去重检查(最近15天)"""
17+
"""去重Pipeline,支持跨天去重检查(最近15天),从AI增强文件中加载已有论文ID"""
1818

1919
def __init__(self):
2020
self.seen_ids = set()
@@ -28,7 +28,7 @@ def open_spider(self, spider):
2828
self.load_recent_ids()
2929

3030
def get_recent_date_files(self):
31-
"""获取最近15天的数据文件路径"""
31+
"""获取最近15天的AI增强数据文件路径"""
3232
current_dir = os.path.dirname(os.path.abspath(__file__))
3333
data_path = os.path.join(current_dir, self.data_dir)
3434

@@ -44,27 +44,47 @@ def get_recent_date_files(self):
4444
date = today - timedelta(days=i)
4545
recent_dates.append(date.strftime("%Y-%m-%d"))
4646

47-
# 查找对应日期的.jsonl文件
47+
# 查找对应日期的AI增强文件,支持多种语言
4848
recent_files = []
4949
for date_str in recent_dates:
50-
file_pattern = os.path.join(data_path, f"{date_str}.jsonl")
51-
if os.path.exists(file_pattern):
52-
recent_files.append(file_pattern)
50+
# 查找所有可能的AI增强文件模式
51+
ai_enhanced_patterns = [
52+
f"{date_str}_AI_enhanced_Chinese.jsonl",
53+
f"{date_str}_AI_enhanced_English.jsonl",
54+
f"{date_str}_AI_enhanced_*.jsonl"
55+
]
56+
57+
for pattern in ai_enhanced_patterns:
58+
if '*' in pattern:
59+
# 使用glob匹配通配符模式
60+
file_pattern = os.path.join(data_path, pattern)
61+
matching_files = glob.glob(file_pattern)
62+
recent_files.extend(matching_files)
63+
else:
64+
# 直接检查文件是否存在
65+
file_path = os.path.join(data_path, pattern)
66+
if os.path.exists(file_path):
67+
recent_files.append(file_path)
68+
69+
# 去重(因为可能有重复的文件路径)
70+
recent_files = list(set(recent_files))
5371

5472
return recent_files
5573

5674
def load_recent_ids(self):
57-
"""加载最近15天数据中的所有论文ID"""
75+
"""加载最近15天AI增强数据中的所有论文ID"""
5876
try:
5977
recent_files = self.get_recent_date_files()
6078

6179
if not recent_files:
6280
if self.logger:
63-
self.logger.info("未找到最近15天的历史数据文件,将进行首次运行去重检查")
81+
self.logger.info("未找到最近15天的AI增强历史数据文件,将进行首次运行去重检查")
6482
return
6583

6684
if self.logger:
67-
self.logger.info(f"找到 {len(recent_files)} 个最近15天的数据文件")
85+
self.logger.info(f"找到 {len(recent_files)} 个最近15天的AI增强数据文件")
86+
for file_path in recent_files:
87+
self.logger.debug(f"加载文件: {os.path.basename(file_path)}")
6888

6989
for file_path in recent_files:
7090
try:
@@ -84,11 +104,11 @@ def load_recent_ids(self):
84104
self.logger.error(f"读取文件错误 {os.path.basename(file_path)}: {e}")
85105

86106
if self.logger:
87-
self.logger.info(f"已加载 {len(self.seen_ids)} 个论文ID用于去重检查")
107+
self.logger.info(f"已从AI增强文件中加载 {len(self.seen_ids)} 个论文ID用于去重检查")
88108

89109
except Exception as e:
90110
if self.logger:
91-
self.logger.error(f"加载最近15天ID时出错: {e}")
111+
self.logger.error(f"加载最近15天AI增强数据ID时出错: {e}")
92112

93113
def process_item(self, item, spider):
94114
"""检查论文ID是否重复"""

run.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ cd daily_arxiv
55
if [ -f "data/${today}.jsonl" ]; then
66
line_count=$(wc -l < "data/${today}.jsonl")
77
if [ "$line_count" -gt 20 ]; then
8-
echo "文件 data/${today}.jsonl 已存在,共 $line_count 行 (>20),跳过爬虫程序..."
8+
echo "文件 data/${today}_AI_enhanced_${LANGUAGE}.jsonl 已存在,共 $line_count 行 (>20),跳过爬虫程序..."
99
else
10-
echo "文件 data/${today}.jsonl 已存在但仅有 $line_count 行 (<=20),重新运行爬虫..."
11-
scrapy crawl arxiv -o ../data/${today}.jsonl
10+
echo "文件 data/${today}_AI_enhanced_${LANGUAGE}.jsonl 已存在但仅有 $line_count 行 (<=20),重新运行爬虫..."
11+
scrapy crawl arxiv -o ../data/${today}_AI_enhanced_${LANGUAGE}.jsonl
1212
fi
1313
else
14-
echo "文件 data/${today}.jsonl 不存在,开始运行爬虫..."
15-
scrapy crawl arxiv -o ../data/${today}.jsonl
14+
echo "文件 data/${today}_AI_enhanced_${LANGUAGE}.jsonl 不存在,开始运行爬虫..."
15+
scrapy crawl arxiv -o ../data/${today}_AI_enhanced_${LANGUAGE}.jsonl
1616
fi
1717

1818
cd ../ai

0 commit comments

Comments
 (0)