File size: 11,307 Bytes
22b7d63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
"""
build_corpus.py — Reproducible corpus builder for CineMatch.

Merges three public, no-auth sources into one clean corpus:

  1. Pablinho "9000plus" TMDB dump  -> base catalog. Every row has a REAL
     TMDB poster URL, overview, genre, release date, rating, popularity.
  2. TMDB-5000 (rashida048 mirror)  -> enriches marquee films with structured
     cast / director / keywords / tagline / runtime / tmdb id.
  3. The repo's own movies_dataframe.json (45k pre-tokenized bags) -> adds
     extra lexical tokens (plot/cast/keyword terms) for BM25 depth.

Output: backend/data/corpus.json  — a list of movie records with BOTH the raw
display fields (preserved verbatim for the UI and for grounded explanations)
and two derived text fields used by the retrieval engine:
    - semantic_text : a natural-language blob for the embedding model
    - lexical_text  : a keyword-dense blob for BM25 tokenization

Run:  python scripts/build_corpus.py
It caches raw downloads under data/raw/ so re-runs are offline & deterministic.
"""
from __future__ import annotations

import ast
import json
import re
import sys
import unicodedata
import urllib.request
from pathlib import Path

import pandas as pd

ROOT = Path(__file__).resolve().parents[1]
RAW = ROOT / "data" / "raw"
OUT = ROOT / "data" / "corpus.json"
REPO_JSON = ROOT / "data" / "movies_dataframe.json"

SOURCES = {
    "pablinho_movies.csv": "https://huggingface.co/datasets/Pablinho/movies-dataset/resolve/main/9000plus.csv",
    "tmdb_5000.csv": "https://raw.githubusercontent.com/rashida048/Some-NLP-Projects/master/movie_dataset.csv",
}


def log(msg: str) -> None:
    print(f"[build_corpus] {msg}", flush=True)


def ensure_raw() -> None:
    RAW.mkdir(parents=True, exist_ok=True)
    for name, url in SOURCES.items():
        dst = RAW / name
        if dst.exists() and dst.stat().st_size > 1000:
            continue
        log(f"downloading {name} ...")
        req = urllib.request.Request(url, headers={"User-Agent": "cinematch/1.0"})
        with urllib.request.urlopen(req, timeout=120) as r, open(dst, "wb") as f:
            f.write(r.read())
        log(f"  saved {dst.stat().st_size // 1024} KB")


def norm_key(s: str) -> str:
    """Aggressive normalization for cross-source title joins (no separators)."""
    s = unicodedata.normalize("NFKD", str(s))
    s = "".join(c for c in s if not unicodedata.combining(c))
    return re.sub(r"[^a-z0-9]", "", s.lower())


def slugify(s: str) -> str:
    """Human-readable, URL-safe slug used as the public movie id."""
    s = unicodedata.normalize("NFKD", str(s))
    s = "".join(c for c in s if not unicodedata.combining(c))
    s = re.sub(r"[^a-z0-9]+", "-", s.lower()).strip("-")
    return s or "untitled"


def parse_tmdb_json(raw, key: str = "name", limit: int | None = None) -> list[str]:
    """Parse a stringified list-of-dicts (raw TMDB JSON dump) into names."""
    if not isinstance(raw, str) or not raw.strip().startswith("["):
        return []
    data = None
    for loader in (json.loads, ast.literal_eval):
        try:
            data = loader(raw)
            break
        except Exception:
            continue
    if not isinstance(data, list):
        return []
    out: list[str] = []
    for item in data:
        if isinstance(item, dict) and item.get(key):
            out.append(str(item[key]).strip())
        if limit and len(out) >= limit:
            break
    return out


_KW_STOP = {
    "of", "the", "a", "an", "and", "or", "to", "in", "on", "at", "by", "for",
    "with", "is", "it", "as", "de", "la", "el", "los", "las",
}


def parse_keywords(raw, limit: int = 20) -> list[str]:
    """The rashida mirror pre-flattens keywords to a space-joined token string
    (e.g. 'culture clash future space war'). Fall back to raw JSON if present."""
    js = parse_tmdb_json(raw, limit=limit)
    if js:
        return js
    if not isinstance(raw, str) or not raw.strip():
        return []
    seen, out = set(), []
    for tok in raw.split():
        t = tok.strip().lower()
        if len(t) < 3 or t in _KW_STOP or t in seen:
            continue
        seen.add(t)
        out.append(t)
        if len(out) >= limit:
            break
    return out


def year_of(date_str) -> int | None:
    m = re.search(r"(\d{4})", str(date_str))
    if not m:
        return None
    y = int(m.group(1))
    return y if 1878 <= y <= 2030 else None


def clean_ws(text) -> str:
    return re.sub(r"\s+", " ", str(text or "")).strip()


def main() -> None:
    ensure_raw()

    # ---- 1. Base catalog: Pablinho -----------------------------------------
    pab = pd.read_csv(RAW / "pablinho_movies.csv")
    log(f"pablinho rows: {len(pab)}")

    # ---- 2. TMDB-5000 enrichment table (keyed by normalized title) ---------
    tm = pd.read_csv(RAW / "tmdb_5000.csv")
    enrich: dict[str, dict] = {}
    for _, r in tm.iterrows():
        key = norm_key(r.get("title"))
        if not key:
            continue
        # `cast` in this mirror is a space-joined string of top billed actors
        # that cannot be reliably split into individual names, so it is kept as
        # free text for retrieval only (never shown as structured facts).
        cast_json = parse_tmdb_json(r.get("cast"), limit=6)
        enrich[key] = {
            "tmdb_id": int(r["id"]) if pd.notna(r.get("id")) else None,
            "cast": cast_json,  # populated only when clean JSON is available
            "cast_text": "" if cast_json else clean_ws(r.get("cast")),
            "director": clean_ws(r.get("director")) or None,
            "keywords": parse_keywords(r.get("keywords"), limit=18),
            "tagline": clean_ws(r.get("tagline")) or None,
            "runtime": int(r["runtime"]) if pd.notna(r.get("runtime")) and r["runtime"] else None,
        }
    log(f"tmdb-5000 enrichment entries: {len(enrich)}")

    # ---- 3. Repo JSON extra lexical tokens (keyed by normalized title) -----
    repo_tokens: dict[str, list[str]] = {}
    if REPO_JSON.exists():
        repo = json.loads(REPO_JSON.read_text())
        for m in repo:
            key = norm_key(m.get("title"))
            if key and isinstance(m.get("tags"), list):
                repo_tokens[key] = m["tags"]
        log(f"repo-json token bags: {len(repo_tokens)}")

    # ---- Merge -------------------------------------------------------------
    records: list[dict] = []
    seen: dict[str, int] = {}      # norm title -> index in records (dedupe)
    used_slugs: dict[str, str] = {}  # slug -> norm key that owns it
    n_dir = n_kw = n_repo = 0

    def unique_slug(title: str, year, key: str) -> str:
        base = slugify(title)
        cand = base
        if cand in used_slugs and used_slugs[cand] != key:
            cand = f"{base}-{year}" if year else f"{base}-x"
        n = 2
        while cand in used_slugs and used_slugs[cand] != key:
            cand = f"{base}-{year}-{n}" if year else f"{base}-{n}"
            n += 1
        used_slugs[cand] = key
        return cand

    for _, row in pab.iterrows():
        title = clean_ws(row.get("Title"))
        if not title:
            continue
        key = norm_key(title)
        overview = clean_ws(row.get("Overview"))
        genres = [g.strip() for g in str(row.get("Genre") or "").split(",") if g.strip()]
        year = year_of(row.get("Release_Date"))

        try:
            vote = round(float(row.get("Vote_Average")), 1)
        except (TypeError, ValueError):
            vote = None
        try:
            popularity = round(float(row.get("Popularity")), 3)
        except (TypeError, ValueError):
            popularity = 0.0
        try:
            vote_count = int(float(row.get("Vote_Count")))
        except (TypeError, ValueError):
            vote_count = 0

        # De-dupe on normalized title, keeping the more-voted entry.
        if key in seen:
            prev = records[seen[key]]
            if vote_count <= prev.get("vote_count", 0):
                continue
            records[seen[key]] = None  # tombstone; rebuild below

        enr = enrich.get(key, {})
        if enr.get("director"):
            n_dir += 1
        if enr.get("keywords"):
            n_kw += 1
        extra = repo_tokens.get(key, [])
        if extra:
            n_repo += 1

        cast = enr.get("cast", [])
        cast_text = enr.get("cast_text", "")
        director = enr.get("director")
        keywords = enr.get("keywords", [])
        tagline = enr.get("tagline")

        rec = {
            "id": unique_slug(title, year, key),  # human-readable, URL-safe, unique
            "tmdb_id": enr.get("tmdb_id"),
            "title": title,
            "year": year,
            "genres": genres,
            "overview": overview,
            "tagline": tagline,
            "poster_url": clean_ws(row.get("Poster_Url")) or None,
            "vote_average": vote,
            "vote_count": vote_count,
            "popularity": popularity,
            "language": clean_ws(row.get("Original_Language")) or None,
            "cast": cast,
            "director": director,
            "keywords": keywords,
            "runtime": enr.get("runtime"),
        }

        # ---- derived retrieval text ----
        # semantic_text: natural language for the embedding model.
        parts = [title]
        if year:
            parts.append(f"({year})")
        if genres:
            parts.append("Genres: " + ", ".join(genres) + ".")
        if tagline:
            parts.append(tagline)
        if overview:
            parts.append(overview)
        if director:
            parts.append(f"Directed by {director}.")
        if cast:
            parts.append("Starring " + ", ".join(cast) + ".")
        elif cast_text:
            parts.append("Starring " + cast_text + ".")
        if keywords:
            parts.append("Themes: " + ", ".join(keywords) + ".")
        rec["semantic_text"] = clean_ws(" ".join(parts))

        # lexical_text: keyword-dense bag for BM25. Genres/keywords/cast get
        # repeated (they are high-signal facets) and the repo token bag is
        # appended for extra plot vocabulary.
        lex = [
            title,
            " ".join(genres * 2),
            overview,
            tagline or "",
            " ".join(keywords),
            " ".join(cast) or cast_text,
            director or "",
            " ".join(extra),
        ]
        rec["lexical_text"] = clean_ws(" ".join(lex))

        if key in seen:
            records[seen[key]] = rec
        else:
            seen[key] = len(records)
            records.append(rec)

    records = [r for r in records if r is not None]

    OUT.write_text(json.dumps(records, ensure_ascii=False))
    log(f"WROTE {OUT}  ({len(records)} movies, {OUT.stat().st_size // 1024} KB)")
    log(f"  with director:{n_dir}  with keywords:{n_kw}  with repo-tokens:{n_repo}")
    with_year = sum(1 for r in records if r["year"])
    with_poster = sum(1 for r in records if r["poster_url"])
    log(f"  with year:{with_year}  with poster:{with_poster}")
    decades = {}
    for r in records:
        if r["year"]:
            decades[r["year"] // 10 * 10] = decades.get(r["year"] // 10 * 10, 0) + 1
    log("  decades: " + ", ".join(f"{k}s:{v}" for k, v in sorted(decades.items())))


if __name__ == "__main__":
    sys.exit(main())