-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
155 lines (133 loc) · 4.89 KB
/
data.py
File metadata and controls
155 lines (133 loc) · 4.89 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
import csv
import os
import re
import logging
from dataclasses import dataclass
from typing import List
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('ConfBot-Data')
logger.setLevel(logging.DEBUG)
@dataclass
class PaperMeta:
title: str
authors: str
abstract: str
@dataclass
class PaperRecord:
id: int
conference: str
year: str
title: str
authors: str
abstract: str
keyword: str = ""
def from_meta_to_csv(path, url, result: List[PaperMeta]):
logger.info("Analysis the conference and year...")
year_match = re.search(r'20\d{2}', url)
current_year = year_match.group(0) if year_match else "Unknown"
url_lower = url.lower()
current_conf = "Unknown"
valid_confs = ['icse', 'ase', 'fse', 'issta']
for conf in valid_confs:
if conf in url_lower:
current_conf = conf
break
logger.info(f"Conf: {current_conf}, Year: {current_year}")
fieldnames = ['id', 'conference', 'year', 'title', 'authors', 'abstract', 'keywords']
all_rows = []
existing_titles = set()
next_id = 1
if os.path.exists(path):
try:
with open(path, mode='r', encoding='utf-8', newline='') as f:
reader = csv.DictReader(f)
for row in reader:
all_rows.append(row)
existing_titles.add(row['title'])
if row['id'].isdigit():
next_id = max(next_id, int(row['id']) + 1)
except Exception as e:
logger.info(f"Failed to read file {e}. Create a new file.")
logger.info("Add the new data...")
new_count = 0
for paper in result:
if paper.title not in existing_titles:
new_row = {
'id': next_id,
'conference': current_conf,
'year': current_year,
'title': paper.title,
'authors': paper.authors,
'abstract': paper.abstract,
'keywords': ''
}
all_rows.append(new_row)
existing_titles.add(paper.title)
next_id += 1
new_count += 1
logger.info("Write back to file...")
try:
with open(path, mode='w', encoding='utf-8', newline='') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(all_rows)
logger.info(f"Success. Original {len(all_rows) - new_count}. Add {new_count}")
logger.info(f"Conference: {current_conf} Year: {current_year}")
except IOError as e:
logger.info(f"Failed to write into file {e}")
def read_papers_from_csv(path: str) -> List[PaperRecord]:
results = []
if not os.path.exists(path):
logger.info(f"File not found: {path}")
return results
try:
# Use utf-8-sig to handle potential BOM
with open(path, mode='r', encoding='utf-8', newline='') as f:
reader = csv.DictReader(f)
for row in reader:
paper = PaperRecord(
id=int(row['id']),
conference=row['conference'],
year=row['year'],
title=row['title'],
authors=row['authors'],
keyword=row.get('keywords', ""),
abstract=row['abstract']
)
results.append(paper)
return results
except Exception as e:
logger.error(f"Error reading file: {e}")
return []
def save_papers_to_csv(path: str, papers: List[PaperRecord]):
# Define header order
fieldnames = ['id', 'conference', 'year', 'title', 'authors', 'abstract', 'keywords']
try:
# Use 'w' mode to overwrite
with open(path, mode='w', encoding='utf-8', newline='') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
for paper in papers:
row = {
'id': paper.id,
'conference': paper.conference,
'year': paper.year,
'title': paper.title,
'authors': paper.authors,
'keywords': paper.keyword,
'abstract': paper.abstract
}
writer.writerow(row)
except Exception as e:
logger.error(f"Failed to save file: {e}")
# --- 测试代码 ---
if __name__ == "__main__":
# 模拟数据
papers = [
PaperMeta(title="AI for SE", authors="Alice, Bob", abstract="Abstract 1"),
PaperMeta(title="Testing LLMs", authors="Charlie", abstract="Abstract 2"),
PaperMeta(title="AI for SE", authors="Alice, Bob", abstract="Duplicate Title Test") # 测试重复
]
csv_path = "papers.csv"
test_url = "https://conf.org/issta/2023/index.html"
from_meta_to_csv(csv_path, test_url, papers)