File size: 3,944 Bytes
46c096a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import requests
from bs4 import BeautifulSoup

SOURCE_URL = "https://aclanthology.org/events/coling-{year}/"
OUTPUT_FILE = "data/coling/coling{year}_papers.jsonl"
BASE_URL = "https://aclanthology.org"


def fetch_page(url):
    headers = {"User-Agent": "Mozilla/5.0 (ACL-parser/1.0; research)"}
    print(f"Fetching {url} ...", flush=True)
    resp = requests.get(url, headers=headers, timeout=120)
    resp.raise_for_status()
    return resp.text


def abs_url(href):
    if not href:
        return ""
    return href if href.startswith("http") else BASE_URL + href


def parse_papers(html):
    soup = BeautifulSoup(html, "html.parser")
    papers = []
    
    # Find all paper divs - each paper is in a div with class "d-sm-flex align-items-stretch mb-3"
    paper_divs = soup.find_all("div", class_="d-sm-flex align-items-stretch mb-3")
    
    for paper_div in paper_divs:
        paper = parse_paper_div(paper_div)
        if paper:
            papers.append(paper)
    
    return papers


def parse_paper_div(paper_div):
    # Extract the paper title and URL
    title_element = paper_div.find("strong")
    if not title_element:
        return None
    
    title_link = title_element.find("a")
    if not title_link:
        return None
    
    title = title_link.get_text(strip=False)
    href = title_link.get("href", "")
    paper_id = href.strip("/").split("/")[-1] if href else ""
    url = abs_url(href)
    
    # Skip if no paper_id found
    if not paper_id:
        return None
    
    # PDF / BIB links from the button-row div
    pdf_url = bib_url = ""
    btn_row = paper_div.find("div", class_="list-button-row")
    if btn_row:
        for a in btn_row.find_all("a", href=True):
            text = a.get_text(strip=True).lower()
            h = a["href"]
            if text == "pdf" or h.endswith(".pdf"):
                pdf_url = abs_url(h)
            elif text == "bib" or h.endswith(".bib"):
                bib_url = abs_url(h)
    
    # Authors: <a href="/people/..."> links inside the d-block span
    authors = []
    span = paper_div.find("span", class_="d-block")
    if span:
        for a in span.find_all("a", href=True):
            if "/people/" in a["href"]:
                authors.append(a.get_text(strip=True))
    
    # Abstract: look for the collapse div with abstract content
    abstract = ""
    # Find the abstract ID from the button
    abs_button = paper_div.find("a", class_="badge text-bg-info")
    if abs_button and "data-bs-target" in abs_button.attrs:
        abs_id = abs_button["data-bs-target"].lstrip("#")
        # Find the abstract div using this ID
        abs_div = soup.find("div", id=abs_id)
        if abs_div:
            body = abs_div.find("div", class_="card-body")
            if body:
                abstract = body.get_text(strip=True)
    
    # Determine volume from paper_id
    volume = ""
    if paper_id:
        parts = paper_id.split(".")
        if len(parts) >= 2:
            volume = parts[1]  # e.g., "acl-long" from "2023.acl-long.19"
    
    return {
        "paper_id": paper_id,
        "title": title,
        "authors": authors,
        "abstract": abstract,
        "url": url,
        "pdf_url": pdf_url,
        "bib_url": bib_url,
    }


def main():
    for year in range(0, 25, 2):
        year = f"20{year:02d}"
        try:
            html = fetch_page(SOURCE_URL.format(year=year))
            print("Parsing ...", flush=True)
            papers = parse_papers(html)
            
            print(f"Found {len(papers)} papers. Writing to {OUTPUT_FILE.format(year=year)} ...", flush=True)
            with open(OUTPUT_FILE.format(year=year), "w", encoding="utf-8") as f:
                for p in papers:
                    f.write(json.dumps(p, ensure_ascii=False) + "\n")
            
            print("Done")
        except Exception as e:
            print(f"Error: {e}")


if __name__ == "__main__":
    main()