import duckdb as duckdb import pandas as pd import tabulate from datasets import load_dataset cursor = duckdb.connect() cursor.execute("PRAGMA threads=4") NROWS = 100000000 NA_VALUES = "\\N" dataset = load_dataset("open_subtitles", lang1="en", lang2="nl", split="train") open_subtitles = dataset.data.table print( tabulate.tabulate( cursor.execute(f"SELECT * FROM open_subtitles LIMIT 5").fetchdf(), headers="keys", tablefmt="psql", ) ) # title_akas = pd.read_csv('title.akas.tsv.gz', sep='\t', na_values=NA_VALUES, nrows=NROWS) # title_df = cursor.execute("SELECT * from title_akas limit 5").fetch_df() # print(tabulate.tabulate(title_df, headers="keys", tablefmt="psql")) title_basics = pd.read_csv( "title.basics.tsv.gz", sep="\t", na_values=NA_VALUES, nrows=NROWS ) basics_df = cursor.execute("SELECT * from title_basics limit 5").fetch_df() print(tabulate.tabulate(basics_df, headers="keys", tablefmt="psql")) title_episodes = pd.read_csv( "title.episode.tsv.gz", sep="\t", na_values=NA_VALUES, nrows=NROWS ) episodes_df = cursor.execute("SELECT * from title_episodes limit 5").fetch_df() print(tabulate.tabulate(episodes_df, headers="keys", tablefmt="psql")) title_ratings = pd.read_csv( "title.ratings.tsv.gz", sep="\t", na_values=NA_VALUES, nrows=NROWS ) ratings_df = cursor.execute("SELECT * from title_ratings limit 5").fetch_df() print(tabulate.tabulate(ratings_df, headers="keys", tablefmt="psql")) # # FIGURE OUT HOW WE CAN JOIN THE SUBTITLE DATASET WITH THE IMDB DATASET # count_join_subtitle_title_akas = cursor.execute( # """ # SELECT COUNT(*) FROM open_subtitles JOIN title_akas ON 'tt' || open_subtitles.meta.imdbId = title_akas.titleId # """ # ).fetchall() # print(f"Count join subtitle title akas: {count_join_subtitle_title_akas}") # # count_join_subtitle_title_basics = cursor.execute( # """ # SELECT COUNT(*) FROM open_subtitles JOIN title_basics ON 'tt' || open_subtitles.meta.imdbId = title_basics.tconst # """ # ).fetchdf() # print(f"Count join subtitle title basics: {count_join_subtitle_title_basics}") # # count_join_subtitle_title_episodes = cursor.execute( # """ # SELECT COUNT(*) FROM open_subtitles JOIN title_episodes ON 'tt' || open_subtitles.meta.imdbId = title_episodes.tconst # """ # ).fetchdf() # print(f"Count join subtitle title episodes: {count_join_subtitle_title_episodes}") # # count_join_subtitle_title_episodes_parent = cursor.execute( # """ # SELECT COUNT(*) FROM open_subtitles JOIN title_episodes ON 'tt' || open_subtitles.meta.imdbId = title_episodes.parentTconst # """ # ).fetchdf() # print(f"Count join subtitle title episodes parent: {count_join_subtitle_title_episodes_parent}") # # count_join_subtitle_title_ratings = cursor.execute( # """ # SELECT COUNT(*) FROM open_subtitles JOIN title_ratings ON 'tt' || open_subtitles.meta.imdbId = title_ratings.tconst # """ # ).fetchdf() # print(f"Count join subtitle title ratings: {count_join_subtitle_title_ratings}") # join title_episode with its parent title_basics and title_ratings episode_detail = cursor.execute( """ SELECT open_subtitles.id, open_subtitles.translation, open_subtitles.meta, title_basics.tconst, title_basics.primaryTitle, title_basics.startYear, title_basics.endYear, title_basics.genres, title_basics.runtimeMinutes, title_basics.titleType, title_basics.isAdult, title_ratings.tconst AS rating_tconst, title_ratings.averageRating, title_ratings.numVotes, title_episodes.tconst as episode_tconst, title_episodes.parentTconst, title_episodes.seasonNumber, title_episodes.episodeNumber FROM title_episodes INNER JOIN title_basics ON title_episodes.parentTconst = title_basics.tconst INNER JOIN title_ratings ON title_episodes.tconst = title_ratings.tconst INNER JOIN open_subtitles ON title_episodes.tconst = 'tt' || open_subtitles.meta.imdbId WHERE isAdult == 0 and averageRating > 8.0 and numVotes > 1000 ORDER BY startYear, episode_tconst, seasonNumber, episodeNumber, meta.sentenceIds.en """ ).fetch_df() print(tabulate.tabulate(episode_detail[:5], headers="keys", tablefmt="psql")) # write episode_detail to json file episode_detail.to_json("episode_opensubtitles.json", orient="records", lines=True)