()
| 35 | |
| 36 | |
| 37 | def main(): |
| 38 | with open(INDEX_PATH) as f: |
| 39 | data = json.load(f) |
| 40 | entries = data["entries"] |
| 41 | |
| 42 | no_slug = [e for e in entries if not e.get("slug")] |
| 43 | print(f"Entries without slug: {len(no_slug)}") |
| 44 | |
| 45 | added_slugs = 0 |
| 46 | for e in entries: |
| 47 | if e.get("slug"): |
| 48 | continue |
| 49 | t = e.get("title", {}) |
| 50 | zh = t.get("zh", "") if isinstance(t, dict) else str(t) |
| 51 | slug = title_to_slug(zh, e["id"]) |
| 52 | e["slug"] = slug |
| 53 | added_slugs += 1 |
| 54 | |
| 55 | print(f"Added slugs: {added_slugs}") |
| 56 | |
| 57 | with open(INDEX_PATH, "w") as f: |
| 58 | json.dump(data, f, ensure_ascii=False, indent=2) |
| 59 | print(f"Saved index") |
| 60 | |
| 61 | missing = [e for e in entries if not fulltext_exists(e)] |
| 62 | print(f"\nMissing fulltext: {len(missing)}") |
| 63 | |
| 64 | unmatched_files = sorted(UNMATCHED_DIR.glob("*.md")) |
| 65 | print(f"Unmatched files: {len(unmatched_files)}") |
| 66 | |
| 67 | recovered = 0 |
| 68 | for f_path in unmatched_files: |
| 69 | text = f_path.read_text(encoding="utf-8")[:3000] |
| 70 | um_title = extract_actual_title(text) or f_path.stem |
| 71 | |
| 72 | best_sim = 0 |
| 73 | best_entry = None |
| 74 | for e in missing: |
| 75 | t = e.get("title", {}) |
| 76 | zh = t.get("zh", "") if isinstance(t, dict) else str(t) |
| 77 | sim = max( |
| 78 | _title_similarity(um_title, zh), |
| 79 | _title_similarity(strip_year_revision(um_title), strip_year_revision(zh)) |
| 80 | ) |
| 81 | if sim > best_sim: |
| 82 | best_sim = sim |
| 83 | best_entry = e |
| 84 | |
| 85 | if best_sim >= 0.85 and best_entry: |
| 86 | slug = best_entry.get("slug", "") |
| 87 | if not slug: |
| 88 | continue |
| 89 | t = best_entry.get("title", {}) |
| 90 | zh = t.get("zh", "") if isinstance(t, dict) else str(t) |
| 91 | |
| 92 | dest = FULLTEXT_DIR / f"{slug}.zh.md" |
| 93 | if dest.exists(): |
| 94 | continue |
no test coverage detected