publications / scripts /coling /coling.py
yangwang825's picture
Upload folder using huggingface_hub
46c096a verified
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()