Upload folder using huggingface_hub
Browse files- src/generate_report.py +313 -22
- src/scp_dataset.py +30 -4
src/generate_report.py
CHANGED
|
@@ -36,7 +36,36 @@ DEFAULT_DB = "../data/scp_dataset.duckdb"
|
|
| 36 |
DEFAULT_IMAGES = "../images"
|
| 37 |
DEFAULT_REPORT = "../STATISTICS.md"
|
| 38 |
|
| 39 |
-
CAPTION = "SCP Wiki content pages · 2008–2026 · n = 19,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
# Palette.
|
| 42 |
INK, SUBINK, MUTED, GRID = "#222222", "#555555", "#9a9a9a", "#ececec"
|
|
@@ -381,8 +410,7 @@ def chart_author_pareto(con: duckdb.DuckDBPyConnection, images: Path) -> str:
|
|
| 381 |
sorted(
|
| 382 |
r[0]
|
| 383 |
for r in con.execute(
|
| 384 |
-
"SELECT count(*) FROM
|
| 385 |
-
"GROUP BY created_by_wikidot_id"
|
| 386 |
).fetchall()
|
| 387 |
)
|
| 388 |
)
|
|
@@ -390,6 +418,7 @@ def chart_author_pareto(con: duckdb.DuckDBPyConnection, images: Path) -> str:
|
|
| 390 |
frac_authors = np.arange(1, len(counts) + 1) / len(counts)
|
| 391 |
desc = counts[::-1]
|
| 392 |
top2 = desc[: max(1, len(desc) // 50)].sum() / counts.sum()
|
|
|
|
| 393 |
|
| 394 |
fig, ax = plt.subplots(figsize=(7.5, 6.5))
|
| 395 |
|
|
@@ -412,9 +441,9 @@ def chart_author_pareto(con: duckdb.DuckDBPyConnection, images: Path) -> str:
|
|
| 412 |
_style_axes(ax, "Authorship is a power law")
|
| 413 |
|
| 414 |
takeaway = (
|
| 415 |
-
|
| 416 |
-
f"{top2:.0%} of all
|
| 417 |
-
"A small core sustains the wiki."
|
| 418 |
)
|
| 419 |
|
| 420 |
return _img(
|
|
@@ -565,9 +594,10 @@ def chart_rating_by_class(con: duckdb.DuckDBPyConnection, images: Path) -> str:
|
|
| 565 |
def chart_prolific_vs_acclaimed(con: duckdb.DuckDBPyConnection, images: Path) -> str:
|
| 566 |
"""Bubble scatter of authors: output vs acclaim, sized by total rating."""
|
| 567 |
rows = con.execute("""
|
| 568 |
-
SELECT
|
| 569 |
-
FROM
|
| 570 |
-
|
|
|
|
| 571 |
""").fetchall()
|
| 572 |
n = np.array([r[1] for r in rows])
|
| 573 |
ar = np.array([r[2] for r in rows])
|
|
@@ -605,9 +635,10 @@ def chart_prolific_vs_acclaimed(con: duckdb.DuckDBPyConnection, images: Path) ->
|
|
| 605 |
_style_axes(ax, "Prolific vs. acclaimed authors")
|
| 606 |
|
| 607 |
takeaway = (
|
| 608 |
-
"
|
| 609 |
-
"
|
| 610 |
-
"size (total score) shows a few
|
|
|
|
| 611 |
)
|
| 612 |
|
| 613 |
return _img(
|
|
@@ -617,6 +648,169 @@ def chart_prolific_vs_acclaimed(con: duckdb.DuckDBPyConnection, images: Path) ->
|
|
| 617 |
)
|
| 618 |
|
| 619 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 620 |
def chart_tag_cooccurrence(con: duckdb.DuckDBPyConnection, images: Path) -> str:
|
| 621 |
"""Heatmap of Jaccard association between the top theme tags."""
|
| 622 |
tags = [t for t, _ in _theme_tags(con, 14)]
|
|
@@ -739,18 +933,40 @@ def table_top_pages(con: duckdb.DuckDBPyConnection) -> str:
|
|
| 739 |
|
| 740 |
|
| 741 |
def table_top_authors(con: duckdb.DuckDBPyConnection) -> str:
|
| 742 |
-
"""Markdown table of the 15 most
|
| 743 |
rows = con.execute("""
|
| 744 |
-
SELECT
|
| 745 |
-
|
| 746 |
-
|
| 747 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 748 |
""").fetchall()
|
| 749 |
|
| 750 |
table = _md_table(
|
| 751 |
-
["#", "Author", "Pages", "Avg rating", "Total rating", "Best-rated work"],
|
| 752 |
[
|
| 753 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 754 |
for i, r in enumerate(rows, 1)
|
| 755 |
],
|
| 756 |
)
|
|
@@ -758,10 +974,74 @@ def table_top_authors(con: duckdb.DuckDBPyConnection) -> str:
|
|
| 758 |
takeaway = (
|
| 759 |
"The most prolific authors are not the highest-scoring on average — volume and acclaim "
|
| 760 |
"rarely coincide — but their cumulative totals show how much of the wiki rests on a few "
|
| 761 |
-
"dozen people."
|
| 762 |
)
|
| 763 |
|
| 764 |
-
return _section("Top 15
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 765 |
|
| 766 |
|
| 767 |
# --- Orchestration --------------------------------------------------------------------
|
|
@@ -777,9 +1057,19 @@ _CHARTS = (
|
|
| 777 |
chart_rating_by_year,
|
| 778 |
chart_rating_by_class,
|
| 779 |
chart_prolific_vs_acclaimed,
|
|
|
|
|
|
|
| 780 |
chart_tag_cooccurrence,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 781 |
)
|
| 782 |
-
_TABLES = (table_per_year, table_top_pages, table_top_authors)
|
| 783 |
|
| 784 |
|
| 785 |
def main(argv: list[str] | None = None) -> int:
|
|
@@ -807,6 +1097,7 @@ def main(argv: list[str] | None = None) -> int:
|
|
| 807 |
setup_style()
|
| 808 |
|
| 809 |
con = duckdb.connect(args.db, read_only=True)
|
|
|
|
| 810 |
sections: list[str] = []
|
| 811 |
try:
|
| 812 |
for fn in _CHARTS:
|
|
|
|
| 36 |
DEFAULT_IMAGES = "../images"
|
| 37 |
DEFAULT_REPORT = "../STATISTICS.md"
|
| 38 |
|
| 39 |
+
CAPTION = "SCP Wiki content pages · 2008–2026 · n = 19,438 · via Crom"
|
| 40 |
+
|
| 41 |
+
# Placeholder/institutional credit names to drop from author stats.
|
| 42 |
+
_PLACEHOLDERS = "('Anonymous', 'Unknown Author', 'Staff', 'Site News Team')"
|
| 43 |
+
|
| 44 |
+
# Shared TEMP views (registered in main()): `credits` is one row per attribution credit;
|
| 45 |
+
# `page_authors` applies the all-authors rule — AUTHOR credits where present, else the
|
| 46 |
+
# page's submitter/poster — so co-authors count and every page is attributed.
|
| 47 |
+
_VIEWS_SQL = f"""
|
| 48 |
+
CREATE TEMP VIEW credits AS
|
| 49 |
+
SELECT p.url, p.rating, p.title, p.created_at,
|
| 50 |
+
p.created_by_display_name AS poster,
|
| 51 |
+
a.type AS role, a.user_display_name AS name
|
| 52 |
+
FROM pages p,
|
| 53 |
+
unnest(from_json(p.attributions,
|
| 54 |
+
'[{{"type":"VARCHAR","user_display_name":"VARCHAR","date":"VARCHAR","order":"INTEGER"}}]'
|
| 55 |
+
)) AS t(a);
|
| 56 |
+
|
| 57 |
+
CREATE TEMP VIEW page_authors AS
|
| 58 |
+
WITH ac AS (
|
| 59 |
+
SELECT DISTINCT url, name AS author FROM credits
|
| 60 |
+
WHERE role = 'AUTHOR' AND name NOT IN {_PLACEHOLDERS}
|
| 61 |
+
)
|
| 62 |
+
SELECT url, author FROM ac
|
| 63 |
+
UNION
|
| 64 |
+
SELECT p.url, p.created_by_display_name FROM pages p
|
| 65 |
+
WHERE p.created_by_display_name IS NOT NULL
|
| 66 |
+
AND p.created_by_display_name NOT IN {_PLACEHOLDERS}
|
| 67 |
+
AND p.url NOT IN (SELECT url FROM ac);
|
| 68 |
+
"""
|
| 69 |
|
| 70 |
# Palette.
|
| 71 |
INK, SUBINK, MUTED, GRID = "#222222", "#555555", "#9a9a9a", "#ececec"
|
|
|
|
| 410 |
sorted(
|
| 411 |
r[0]
|
| 412 |
for r in con.execute(
|
| 413 |
+
"SELECT count(*) FROM page_authors GROUP BY author"
|
|
|
|
| 414 |
).fetchall()
|
| 415 |
)
|
| 416 |
)
|
|
|
|
| 418 |
frac_authors = np.arange(1, len(counts) + 1) / len(counts)
|
| 419 |
desc = counts[::-1]
|
| 420 |
top2 = desc[: max(1, len(desc) // 50)].sum() / counts.sum()
|
| 421 |
+
solo = int((counts == 1).sum())
|
| 422 |
|
| 423 |
fig, ax = plt.subplots(figsize=(7.5, 6.5))
|
| 424 |
|
|
|
|
| 441 |
_style_axes(ax, "Authorship is a power law")
|
| 442 |
|
| 443 |
takeaway = (
|
| 444 |
+
"Counting every credited author (not just whoever posted the page), contribution is "
|
| 445 |
+
f"steeply unequal: the most prolific 2% hold {top2:.0%} of all author credits, while "
|
| 446 |
+
f"{solo:,} authors have a single credit. A small core sustains the wiki."
|
| 447 |
)
|
| 448 |
|
| 449 |
return _img(
|
|
|
|
| 594 |
def chart_prolific_vs_acclaimed(con: duckdb.DuckDBPyConnection, images: Path) -> str:
|
| 595 |
"""Bubble scatter of authors: output vs acclaim, sized by total rating."""
|
| 596 |
rows = con.execute("""
|
| 597 |
+
SELECT pa.author, count(*) n, avg(p.rating) ar, sum(p.rating) total
|
| 598 |
+
FROM page_authors pa JOIN pages p ON p.url = pa.url
|
| 599 |
+
WHERE p.rating IS NOT NULL
|
| 600 |
+
GROUP BY pa.author HAVING count(*) >= 10
|
| 601 |
""").fetchall()
|
| 602 |
n = np.array([r[1] for r in rows])
|
| 603 |
ar = np.array([r[2] for r in rows])
|
|
|
|
| 635 |
_style_axes(ax, "Prolific vs. acclaimed authors")
|
| 636 |
|
| 637 |
takeaway = (
|
| 638 |
+
"Counting every credited author (co-authors included), output and acclaim remain "
|
| 639 |
+
"different games: the most prolific cluster at modest average ratings, while the "
|
| 640 |
+
"highest-rated are comparatively selective — bubble size (total score) shows a few "
|
| 641 |
+
"writers manage both volume and quality."
|
| 642 |
)
|
| 643 |
|
| 644 |
return _img(
|
|
|
|
| 648 |
)
|
| 649 |
|
| 650 |
|
| 651 |
+
def chart_coauthorship_over_time(con: duckdb.DuckDBPyConnection, images: Path) -> str:
|
| 652 |
+
"""Line: share of pages with two or more credited authors, by year."""
|
| 653 |
+
rows = con.execute(f"""
|
| 654 |
+
WITH ac AS (
|
| 655 |
+
SELECT url, count(DISTINCT name) AS k FROM credits
|
| 656 |
+
WHERE role = 'AUTHOR' AND name NOT IN {_PLACEHOLDERS}
|
| 657 |
+
GROUP BY url
|
| 658 |
+
)
|
| 659 |
+
SELECT year(p.created_at) AS yr, count(*) AS total,
|
| 660 |
+
count(*) FILTER (WHERE coalesce(ac.k, 0) >= 2) AS co
|
| 661 |
+
FROM pages p LEFT JOIN ac ON ac.url = p.url
|
| 662 |
+
GROUP BY yr ORDER BY yr
|
| 663 |
+
""").fetchall()
|
| 664 |
+
years = [r[0] for r in rows]
|
| 665 |
+
pct = [100.0 * r[2] / r[1] for r in rows]
|
| 666 |
+
|
| 667 |
+
fig, ax = plt.subplots(figsize=(10, 5.2))
|
| 668 |
+
ax.plot(years, pct, color=ACCENT, linewidth=2.4, marker="o", markersize=4)
|
| 669 |
+
ax.fill_between(years, pct, color=ACCENT, alpha=0.08)
|
| 670 |
+
ax.set_ylabel("% of pages with ≥2 credited authors")
|
| 671 |
+
ax.set_ylim(0, max(pct) * 1.15)
|
| 672 |
+
ax.set_xticks(range(min(years), max(years) + 1, 2))
|
| 673 |
+
_style_axes(ax, "Co-authorship over time")
|
| 674 |
+
|
| 675 |
+
peak = max(pct)
|
| 676 |
+
takeaway = (
|
| 677 |
+
f"Co-authorship has climbed from near zero to about {peak:.0f}% of pages in recent years "
|
| 678 |
+
"— modern SCP is increasingly a team effort (2026 is a partial year). Early collaboration "
|
| 679 |
+
"is undercounted: the attribution metadata recording co-authors is a later convention."
|
| 680 |
+
)
|
| 681 |
+
return _img(
|
| 682 |
+
"Co-authorship over time",
|
| 683 |
+
_save(fig, images, "coauthorship_over_time"),
|
| 684 |
+
takeaway,
|
| 685 |
+
)
|
| 686 |
+
|
| 687 |
+
|
| 688 |
+
def chart_rating_by_team(con: duckdb.DuckDBPyConnection, images: Path) -> str:
|
| 689 |
+
"""Bar: median rating by number of credited authors."""
|
| 690 |
+
rows = con.execute("""
|
| 691 |
+
WITH per_page AS (
|
| 692 |
+
SELECT pa.url, any_value(p.rating) AS rating, count(*) AS n
|
| 693 |
+
FROM page_authors pa JOIN pages p ON p.url = pa.url
|
| 694 |
+
WHERE p.rating IS NOT NULL GROUP BY pa.url
|
| 695 |
+
),
|
| 696 |
+
bucketed AS (SELECT rating, least(n, 4) AS team FROM per_page)
|
| 697 |
+
SELECT team, count(*) AS pages, median(rating) AS med
|
| 698 |
+
FROM bucketed GROUP BY team ORDER BY team
|
| 699 |
+
""").fetchall()
|
| 700 |
+
names = {1: "1 (solo)", 2: "2", 3: "3", 4: "4+"}
|
| 701 |
+
labels = [names[r[0]] for r in rows]
|
| 702 |
+
med = [r[2] for r in rows]
|
| 703 |
+
counts = [r[1] for r in rows]
|
| 704 |
+
|
| 705 |
+
fig, ax = plt.subplots(figsize=(9, 5))
|
| 706 |
+
bars = ax.bar(labels, med, color=ACCENT, width=0.62)
|
| 707 |
+
ax.bar_label(bars, fmt="{:.0f}", padding=3, color=SUBINK, fontsize=10)
|
| 708 |
+
for i, c in enumerate(counts):
|
| 709 |
+
ax.text(i, max(med) * 0.04, f"n={c:,}", ha="center", color="white", fontsize=8)
|
| 710 |
+
ax.set_ylabel("median rating")
|
| 711 |
+
ax.set_xlabel("number of credited authors")
|
| 712 |
+
_style_axes(ax, "Bigger teams, higher ratings")
|
| 713 |
+
|
| 714 |
+
takeaway = (
|
| 715 |
+
f"Median rating rises with team size — from {med[0]:.0f} for solo pages to {med[-1]:.0f} "
|
| 716 |
+
"for the largest teams. Collaboration correlates with a warmer reception, though the "
|
| 717 |
+
"most ambitious projects also tend to attract co-authors."
|
| 718 |
+
)
|
| 719 |
+
return _img(
|
| 720 |
+
"Bigger teams, higher ratings",
|
| 721 |
+
_save(fig, images, "rating_by_team"),
|
| 722 |
+
takeaway,
|
| 723 |
+
)
|
| 724 |
+
|
| 725 |
+
|
| 726 |
+
def chart_collaboration_network(con: duckdb.DuckDBPyConnection, images: Path) -> str:
|
| 727 |
+
"""Circular network of co-authorship among the most-connected authors."""
|
| 728 |
+
edges = con.execute("""
|
| 729 |
+
WITH a AS (SELECT url, author FROM page_authors)
|
| 730 |
+
SELECT x.author, y.author, count(*) AS w
|
| 731 |
+
FROM a x JOIN a y ON x.url = y.url AND x.author < y.author
|
| 732 |
+
GROUP BY x.author, y.author
|
| 733 |
+
""").fetchall()
|
| 734 |
+
pages = dict(
|
| 735 |
+
con.execute(
|
| 736 |
+
"SELECT author, count(*) FROM page_authors GROUP BY author"
|
| 737 |
+
).fetchall()
|
| 738 |
+
)
|
| 739 |
+
# Rank authors by *recurring* collaboration (≥2 shared pages) so the named hub matches the
|
| 740 |
+
# drawn edges; one-off links from mass-collaboration pages would otherwise dominate.
|
| 741 |
+
recurring = [(a, b, w) for a, b, w in edges if w >= 2]
|
| 742 |
+
degree: dict[str, int] = {}
|
| 743 |
+
for a1, a2, w in recurring:
|
| 744 |
+
degree[a1] = degree.get(a1, 0) + w
|
| 745 |
+
degree[a2] = degree.get(a2, 0) + w
|
| 746 |
+
top = [
|
| 747 |
+
a for a, _ in sorted(degree.items(), key=lambda kv: kv[1], reverse=True)[:26]
|
| 748 |
+
]
|
| 749 |
+
rank = set(top)
|
| 750 |
+
sub = [(a, b, w) for a, b, w in recurring if a in rank and b in rank]
|
| 751 |
+
|
| 752 |
+
angles = np.linspace(0, 2 * np.pi, len(top), endpoint=False)
|
| 753 |
+
xy = {
|
| 754 |
+
a: (float(np.cos(t)), float(np.sin(t)))
|
| 755 |
+
for a, t in zip(top, angles, strict=True)
|
| 756 |
+
}
|
| 757 |
+
maxw = max(e[2] for e in sub)
|
| 758 |
+
|
| 759 |
+
fig, ax = plt.subplots(figsize=(9.5, 9.5))
|
| 760 |
+
for a, b, w in sub:
|
| 761 |
+
(x1, y1), (x2, y2) = xy[a], xy[b]
|
| 762 |
+
ax.plot(
|
| 763 |
+
[x1, x2],
|
| 764 |
+
[y1, y2],
|
| 765 |
+
color=ACCENT,
|
| 766 |
+
solid_capstyle="round",
|
| 767 |
+
zorder=1,
|
| 768 |
+
alpha=min(0.75, 0.10 + 0.65 * w / maxw),
|
| 769 |
+
linewidth=0.4 + 3.0 * w / maxw,
|
| 770 |
+
)
|
| 771 |
+
sizes = [float(np.clip(pages.get(a, 1) * 2.0, 30, 600)) for a in top]
|
| 772 |
+
ax.scatter(
|
| 773 |
+
[xy[a][0] for a in top],
|
| 774 |
+
[xy[a][1] for a in top],
|
| 775 |
+
s=sizes,
|
| 776 |
+
color=INK,
|
| 777 |
+
edgecolors="white",
|
| 778 |
+
linewidth=0.6,
|
| 779 |
+
zorder=2,
|
| 780 |
+
)
|
| 781 |
+
for a, t in zip(top, angles, strict=True):
|
| 782 |
+
deg = float(np.degrees(t))
|
| 783 |
+
flip = 90 < deg < 270
|
| 784 |
+
ax.text(
|
| 785 |
+
1.06 * np.cos(t),
|
| 786 |
+
1.06 * np.sin(t),
|
| 787 |
+
a,
|
| 788 |
+
fontsize=7.5,
|
| 789 |
+
color=INK,
|
| 790 |
+
ha="right" if flip else "left",
|
| 791 |
+
va="center",
|
| 792 |
+
rotation=deg + 180 if flip else deg,
|
| 793 |
+
rotation_mode="anchor",
|
| 794 |
+
)
|
| 795 |
+
ax.set_xlim(-1.5, 1.5)
|
| 796 |
+
ax.set_ylim(-1.5, 1.5)
|
| 797 |
+
ax.set_aspect("equal")
|
| 798 |
+
ax.axis("off")
|
| 799 |
+
ax.set_title("How authors collaborate", loc="left", pad=12)
|
| 800 |
+
ax.figure.text(0.005, 0.005, CAPTION, fontsize=7.5, color=MUTED, ha="left")
|
| 801 |
+
|
| 802 |
+
takeaway = (
|
| 803 |
+
f"Co-authorship forms tight clusters around a few hubs — {top[0]} is the most connected. "
|
| 804 |
+
"Node size is total pages, edge weight is shared pages; the modern wiki is a densely "
|
| 805 |
+
"woven collaborative network, not a crowd of soloists."
|
| 806 |
+
)
|
| 807 |
+
return _img(
|
| 808 |
+
"How authors collaborate",
|
| 809 |
+
_save(fig, images, "collaboration_network"),
|
| 810 |
+
takeaway,
|
| 811 |
+
)
|
| 812 |
+
|
| 813 |
+
|
| 814 |
def chart_tag_cooccurrence(con: duckdb.DuckDBPyConnection, images: Path) -> str:
|
| 815 |
"""Heatmap of Jaccard association between the top theme tags."""
|
| 816 |
tags = [t for t, _ in _theme_tags(con, 14)]
|
|
|
|
| 933 |
|
| 934 |
|
| 935 |
def table_top_authors(con: duckdb.DuckDBPyConnection) -> str:
|
| 936 |
+
"""Markdown table of the 15 most-credited authors (all-authors rule)."""
|
| 937 |
rows = con.execute("""
|
| 938 |
+
WITH team AS (SELECT url, count(*) AS n FROM page_authors GROUP BY url)
|
| 939 |
+
SELECT pa.author, count(*) AS pages, round(avg(p.rating)) AS ar,
|
| 940 |
+
round(sum(p.rating)) AS tot,
|
| 941 |
+
round(100.0 * count(*) FILTER (WHERE team.n >= 2) / count(*)) AS collab,
|
| 942 |
+
arg_max(p.title, p.rating) AS best
|
| 943 |
+
FROM page_authors pa
|
| 944 |
+
JOIN pages p ON p.url = pa.url
|
| 945 |
+
JOIN team ON team.url = pa.url
|
| 946 |
+
WHERE p.rating IS NOT NULL
|
| 947 |
+
GROUP BY pa.author ORDER BY pages DESC LIMIT 15
|
| 948 |
""").fetchall()
|
| 949 |
|
| 950 |
table = _md_table(
|
|
|
|
| 951 |
[
|
| 952 |
+
"#",
|
| 953 |
+
"Author",
|
| 954 |
+
"Pages",
|
| 955 |
+
"Avg rating",
|
| 956 |
+
"Total rating",
|
| 957 |
+
"Co-authored",
|
| 958 |
+
"Best-rated work",
|
| 959 |
+
],
|
| 960 |
+
[
|
| 961 |
+
[
|
| 962 |
+
str(i),
|
| 963 |
+
r[0],
|
| 964 |
+
f"{r[1]:,}",
|
| 965 |
+
f"{r[2]:.0f}",
|
| 966 |
+
f"{r[3]:,.0f}",
|
| 967 |
+
f"{r[4]:.0f}%",
|
| 968 |
+
r[5],
|
| 969 |
+
]
|
| 970 |
for i, r in enumerate(rows, 1)
|
| 971 |
],
|
| 972 |
)
|
|
|
|
| 974 |
takeaway = (
|
| 975 |
"The most prolific authors are not the highest-scoring on average — volume and acclaim "
|
| 976 |
"rarely coincide — but their cumulative totals show how much of the wiki rests on a few "
|
| 977 |
+
"dozen people. The co-authored share shows how collaboratively each writer works."
|
| 978 |
)
|
| 979 |
|
| 980 |
+
return _section("Top 15 authors (all credited authors)", table, takeaway)
|
| 981 |
+
|
| 982 |
+
|
| 983 |
+
def table_coauthor_duos(con: duckdb.DuckDBPyConnection) -> str:
|
| 984 |
+
"""Markdown table of the most frequent co-author pairs."""
|
| 985 |
+
rows = con.execute("""
|
| 986 |
+
WITH a AS (SELECT url, author FROM page_authors)
|
| 987 |
+
SELECT x.author, y.author, count(*) AS n, round(avg(p.rating)) AS ar
|
| 988 |
+
FROM a x JOIN a y ON x.url = y.url AND x.author < y.author
|
| 989 |
+
JOIN pages p ON p.url = x.url
|
| 990 |
+
WHERE p.rating IS NOT NULL
|
| 991 |
+
GROUP BY x.author, y.author ORDER BY n DESC LIMIT 12
|
| 992 |
+
""").fetchall()
|
| 993 |
+
|
| 994 |
+
table = _md_table(
|
| 995 |
+
["#", "Author A", "Author B", "Shared pages", "Avg rating"],
|
| 996 |
+
[
|
| 997 |
+
[str(i), r[0], r[1], f"{r[2]:,}", f"{r[3]:.0f}"]
|
| 998 |
+
for i, r in enumerate(rows, 1)
|
| 999 |
+
],
|
| 1000 |
+
)
|
| 1001 |
+
takeaway = (
|
| 1002 |
+
"The wiki's tightest writing partnerships — recurring duos that have co-authored many "
|
| 1003 |
+
"pages together, several rating well above the site median."
|
| 1004 |
+
)
|
| 1005 |
+
return _section("Top co-author duos", table, takeaway)
|
| 1006 |
+
|
| 1007 |
+
|
| 1008 |
+
def table_most_collaborative(con: duckdb.DuckDBPyConnection) -> str:
|
| 1009 |
+
"""Markdown table of authors with the most distinct co-authors."""
|
| 1010 |
+
rows = con.execute("""
|
| 1011 |
+
WITH a AS (SELECT url, author FROM page_authors)
|
| 1012 |
+
SELECT x.author, count(DISTINCT y.author) AS partners, count(DISTINCT x.url) AS pages
|
| 1013 |
+
FROM a x JOIN a y ON x.url = y.url AND x.author <> y.author
|
| 1014 |
+
GROUP BY x.author ORDER BY partners DESC LIMIT 15
|
| 1015 |
+
""").fetchall()
|
| 1016 |
+
|
| 1017 |
+
table = _md_table(
|
| 1018 |
+
["#", "Author", "Distinct co-authors", "Co-authored pages"],
|
| 1019 |
+
[[str(i), r[0], f"{r[1]:,}", f"{r[2]:,}"] for i, r in enumerate(rows, 1)],
|
| 1020 |
+
)
|
| 1021 |
+
takeaway = (
|
| 1022 |
+
"The community's connectors — authors who have written with the widest circle of "
|
| 1023 |
+
"collaborators, knitting otherwise separate clusters together."
|
| 1024 |
+
)
|
| 1025 |
+
return _section("Most collaborative authors", table, takeaway)
|
| 1026 |
+
|
| 1027 |
+
|
| 1028 |
+
def table_top_rewriters(con: duckdb.DuckDBPyConnection) -> str:
|
| 1029 |
+
"""Markdown table of authors credited with the most rewrites."""
|
| 1030 |
+
rows = con.execute(f"""
|
| 1031 |
+
SELECT name, count(*) AS n, round(avg(rating)) AS ar
|
| 1032 |
+
FROM credits WHERE role = 'REWRITE' AND name NOT IN {_PLACEHOLDERS}
|
| 1033 |
+
GROUP BY name ORDER BY n DESC LIMIT 12
|
| 1034 |
+
""").fetchall()
|
| 1035 |
+
|
| 1036 |
+
table = _md_table(
|
| 1037 |
+
["#", "Rewriter", "Pages rewritten", "Avg rating"],
|
| 1038 |
+
[[str(i), r[0], f"{r[1]:,}", f"{r[2]:.0f}"] for i, r in enumerate(rows, 1)],
|
| 1039 |
+
)
|
| 1040 |
+
takeaway = (
|
| 1041 |
+
"The canon's caretakers: a small group does most of the rewriting that keeps the early, "
|
| 1042 |
+
"heavily-trafficked articles current."
|
| 1043 |
+
)
|
| 1044 |
+
return _section("Top rewriters", table, takeaway)
|
| 1045 |
|
| 1046 |
|
| 1047 |
# --- Orchestration --------------------------------------------------------------------
|
|
|
|
| 1057 |
chart_rating_by_year,
|
| 1058 |
chart_rating_by_class,
|
| 1059 |
chart_prolific_vs_acclaimed,
|
| 1060 |
+
chart_coauthorship_over_time,
|
| 1061 |
+
chart_rating_by_team,
|
| 1062 |
chart_tag_cooccurrence,
|
| 1063 |
+
chart_collaboration_network,
|
| 1064 |
+
)
|
| 1065 |
+
_TABLES = (
|
| 1066 |
+
table_per_year,
|
| 1067 |
+
table_top_pages,
|
| 1068 |
+
table_top_authors,
|
| 1069 |
+
table_coauthor_duos,
|
| 1070 |
+
table_most_collaborative,
|
| 1071 |
+
table_top_rewriters,
|
| 1072 |
)
|
|
|
|
| 1073 |
|
| 1074 |
|
| 1075 |
def main(argv: list[str] | None = None) -> int:
|
|
|
|
| 1097 |
setup_style()
|
| 1098 |
|
| 1099 |
con = duckdb.connect(args.db, read_only=True)
|
| 1100 |
+
con.execute(_VIEWS_SQL) # register the `credits` and `page_authors` views
|
| 1101 |
sections: list[str] = []
|
| 1102 |
try:
|
| 1103 |
for fn in _CHARTS:
|
src/scp_dataset.py
CHANGED
|
@@ -8,12 +8,13 @@ GOI formats, essays, art) created in a given year on the English SCP wiki
|
|
| 8 |
Captured per page: url, wikidot id, title, rating, vote count, category, created_at
|
| 9 |
(UTC), revision count, comment count, thumbnail url, parent url, created_by
|
| 10 |
(crom id / display name / unix name / wikidot id), source (raw Wikidot text), summary,
|
| 11 |
-
tags,
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
The DuckDB file is the checkpoint: re-running a year resumes from the last saved page
|
| 15 |
(a `created_at` watermark) and re-fetched rows upsert in place, so interruptions and
|
| 16 |
-
Ctrl+C are safe.
|
| 17 |
`page_tags` and `page_alternate_titles` child tables.
|
| 18 |
|
| 19 |
Usage (run from the src/ directory):
|
|
@@ -30,6 +31,7 @@ from __future__ import annotations
|
|
| 30 |
import argparse
|
| 31 |
import asyncio
|
| 32 |
import contextlib
|
|
|
|
| 33 |
import logging
|
| 34 |
import signal
|
| 35 |
import sys
|
|
@@ -164,6 +166,7 @@ CREATE TABLE IF NOT EXISTS pages (
|
|
| 164 |
created_by_wikidot_id VARCHAR,
|
| 165 |
source VARCHAR, -- raw Wikidot source text
|
| 166 |
summary VARCHAR, -- author summary, often null
|
|
|
|
| 167 |
fetched_at TIMESTAMP NOT NULL -- UTC (naive); when saved
|
| 168 |
);
|
| 169 |
|
|
@@ -202,6 +205,7 @@ _PAGE_COLUMNS = (
|
|
| 202 |
"created_by_wikidot_id",
|
| 203 |
"source",
|
| 204 |
"summary",
|
|
|
|
| 205 |
"fetched_at",
|
| 206 |
)
|
| 207 |
|
|
@@ -213,12 +217,30 @@ _INSERT_PAGE = (
|
|
| 213 |
|
| 214 |
|
| 215 |
def connect(db_path: str) -> duckdb.DuckDBPyConnection:
|
| 216 |
-
"""Open the DuckDB database and ensure the schema exists."""
|
| 217 |
con = duckdb.connect(db_path)
|
| 218 |
con.execute(_SCHEMA)
|
|
|
|
|
|
|
| 219 |
return con
|
| 220 |
|
| 221 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
def _page_row(page: Page, fetched_at: datetime) -> tuple[object, ...]:
|
| 223 |
"""Flatten a page into a row tuple matching `_PAGE_COLUMNS`."""
|
| 224 |
author = page.created_by
|
|
@@ -240,6 +262,7 @@ def _page_row(page: Page, fetched_at: datetime) -> tuple[object, ...]:
|
|
| 240 |
author.wikidot_id if author else None,
|
| 241 |
page.source,
|
| 242 |
page.summary,
|
|
|
|
| 243 |
fetched_at,
|
| 244 |
)
|
| 245 |
|
|
@@ -362,6 +385,7 @@ def _log_plan(
|
|
| 362 |
source=True,
|
| 363 |
summary=True,
|
| 364 |
alternate_titles=True,
|
|
|
|
| 365 |
)
|
| 366 |
remaining = max(0, expected - already)
|
| 367 |
est = remaining * per_page
|
|
@@ -422,6 +446,7 @@ async def crawl_year(args: argparse.Namespace) -> int: # noqa: C901, PLR0915
|
|
| 422 |
source=True,
|
| 423 |
summary=True,
|
| 424 |
alternate_titles=True,
|
|
|
|
| 425 |
)
|
| 426 |
|
| 427 |
try:
|
|
@@ -471,6 +496,7 @@ async def crawl_year(args: argparse.Namespace) -> int: # noqa: C901, PLR0915
|
|
| 471 |
source=True,
|
| 472 |
summary=True,
|
| 473 |
alternate_titles=True,
|
|
|
|
| 474 |
)
|
| 475 |
)
|
| 476 |
if batch.pages:
|
|
|
|
| 8 |
Captured per page: url, wikidot id, title, rating, vote count, category, created_at
|
| 9 |
(UTC), revision count, comment count, thumbnail url, parent url, created_by
|
| 10 |
(crom id / display name / unix name / wikidot id), source (raw Wikidot text), summary,
|
| 11 |
+
tags, alternate titles, and attributions (a JSON array of {type, user_display_name,
|
| 12 |
+
date, order}). Only the rendered text content is skipped; hidden pages and user/author
|
| 13 |
+
pages are filtered out server-side.
|
| 14 |
|
| 15 |
The DuckDB file is the checkpoint: re-running a year resumes from the last saved page
|
| 16 |
(a `created_at` watermark) and re-fetched rows upsert in place, so interruptions and
|
| 17 |
+
Ctrl+C are safe. A `pages` table (with an `attributions` JSON column) plus normalized
|
| 18 |
`page_tags` and `page_alternate_titles` child tables.
|
| 19 |
|
| 20 |
Usage (run from the src/ directory):
|
|
|
|
| 31 |
import argparse
|
| 32 |
import asyncio
|
| 33 |
import contextlib
|
| 34 |
+
import json
|
| 35 |
import logging
|
| 36 |
import signal
|
| 37 |
import sys
|
|
|
|
| 166 |
created_by_wikidot_id VARCHAR,
|
| 167 |
source VARCHAR, -- raw Wikidot source text
|
| 168 |
summary VARCHAR, -- author summary, often null
|
| 169 |
+
attributions JSON, -- [{type, user_display_name, ...}]
|
| 170 |
fetched_at TIMESTAMP NOT NULL -- UTC (naive); when saved
|
| 171 |
);
|
| 172 |
|
|
|
|
| 205 |
"created_by_wikidot_id",
|
| 206 |
"source",
|
| 207 |
"summary",
|
| 208 |
+
"attributions",
|
| 209 |
"fetched_at",
|
| 210 |
)
|
| 211 |
|
|
|
|
| 217 |
|
| 218 |
|
| 219 |
def connect(db_path: str) -> duckdb.DuckDBPyConnection:
|
| 220 |
+
"""Open the DuckDB database and ensure the schema (with migrations) exists."""
|
| 221 |
con = duckdb.connect(db_path)
|
| 222 |
con.execute(_SCHEMA)
|
| 223 |
+
# Migration: add `attributions` to databases created before the column existed.
|
| 224 |
+
con.execute("ALTER TABLE pages ADD COLUMN IF NOT EXISTS attributions JSON")
|
| 225 |
return con
|
| 226 |
|
| 227 |
|
| 228 |
+
def _attributions_json(page: Page) -> str:
|
| 229 |
+
"""Serialize a page's attributions to a JSON array string for the JSON column."""
|
| 230 |
+
return json.dumps(
|
| 231 |
+
[
|
| 232 |
+
{
|
| 233 |
+
"type": a.type.value,
|
| 234 |
+
"user_display_name": a.user_display_name,
|
| 235 |
+
"date": a.date.isoformat() if a.date is not None else None,
|
| 236 |
+
"order": a.order,
|
| 237 |
+
}
|
| 238 |
+
for a in page.attributions or ()
|
| 239 |
+
],
|
| 240 |
+
ensure_ascii=False,
|
| 241 |
+
)
|
| 242 |
+
|
| 243 |
+
|
| 244 |
def _page_row(page: Page, fetched_at: datetime) -> tuple[object, ...]:
|
| 245 |
"""Flatten a page into a row tuple matching `_PAGE_COLUMNS`."""
|
| 246 |
author = page.created_by
|
|
|
|
| 262 |
author.wikidot_id if author else None,
|
| 263 |
page.source,
|
| 264 |
page.summary,
|
| 265 |
+
_attributions_json(page),
|
| 266 |
fetched_at,
|
| 267 |
)
|
| 268 |
|
|
|
|
| 385 |
source=True,
|
| 386 |
summary=True,
|
| 387 |
alternate_titles=True,
|
| 388 |
+
attributions=True,
|
| 389 |
)
|
| 390 |
remaining = max(0, expected - already)
|
| 391 |
est = remaining * per_page
|
|
|
|
| 446 |
source=True,
|
| 447 |
summary=True,
|
| 448 |
alternate_titles=True,
|
| 449 |
+
attributions=True,
|
| 450 |
)
|
| 451 |
|
| 452 |
try:
|
|
|
|
| 496 |
source=True,
|
| 497 |
summary=True,
|
| 498 |
alternate_titles=True,
|
| 499 |
+
attributions=True,
|
| 500 |
)
|
| 501 |
)
|
| 502 |
if batch.pages:
|