In [1]:
%reload_ext autotime
import pandas as pd
from glob import glob
import json
import os
from bertopic import BERTopic
from bertopic.representation import KeyBERTInspired
import plotly.io as pio
pio.renderers.default = 'notebook'
pd.options.plotting.backend = "plotly"

df = pd.read_csv("country_calendar_metadata.csv")
df["Recording Date"] = df["Recording Date"].replace("0000-00-00", "2008-10-23")

def get_transcript(file):
    file = os.path.basename(file.replace("?download=1", ""))
    transcript_filename = f"transcripts/{file}.json"
    with open(transcript_filename) as f:
        return json.load(f)["text"]

df["transcript"] = df.link.apply(get_transcript)
#df.index = pd.to_datetime(df["Recording Date"])
df.to_csv("country_calendar_metadata_with_transcripts.csv")
df
✔️ 10.6 s (2024-05-21T14:37:21/2024-05-21T14:37:31)
2024-05-21 14:37:26.429595: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2024-05-21 14:37:26.487729: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-05-21 14:37:27.329129: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
Out[1]:
File Name Name Description Duration Type Subject Age Level Channel Recording Date Recording Time id link size_bytes transcript
0 etv/country_calendar_-_air_new_zealand_uniform... country calendar - air new zealand uniforms 23... from www.throng.co.nz : this episode of nation... 00:21:33 Library NaN G lostplusfound 2008-10-23 00:00:00 61580 https://cdn.etv.org.nz/etv/country_calendar_-_... 145371146 Music Music Music Music Music Music Music Mus...
1 etv/TV_One_1900_21022009_20090221193153256qtst... Country Calendar - Series 2009 - Episode 1 'Lyn of Taranaki' Country Calendar starts its ... 00:29:57 Documentary Agriculture & Horticulture, Business, Ecology ... Unrated TV 1 2009-02-21 19:00:00 12564 https://cdn.etv.org.nz/etv/TV_One_1900_2102200... 49973516 Lin Webster's life has taken many unusual twi...
2 etv/TV_One_1900_28022009_20090228193154256qtst... Country Calendar - Series 2009 - Episode 2 'Shared Fruit' New Zealand and Chinese scienti... 00:29:56 Documentary Agriculture & Horticulture, Asian Studies, Bus... Unrated TV 1 2009-02-28 19:00:00 12697 https://cdn.etv.org.nz/etv/TV_One_1900_2802200... 52854708 Yunnan, tucked away in the remote southwest o...
3 etv/TV_One_1900_07032009_20090307193229256qtst... Country Calendar - Series 2009 - Episode 3 'Grassroots Leader' Bruce Rollinson is a late-... 00:29:56 Documentary Agriculture & Horticulture, Business, Economic... Unrated TV 1 2009-03-07 19:00:00 12812 https://cdn.etv.org.nz/etv/TV_One_1900_0703200... 51865839 The season on Mount Ropiku was the best for y...
4 etv/TV_One_1900_14032009_20090314193119768qtst... Country Calendar - Series 2009 - Episode 4 'High-flying Fur' What's as fine as merino woo... 00:29:56 Documentary Agriculture & Horticulture, Biology, Business,... Unrated TV 1 2009-03-14 19:00:00 12939 https://cdn.etv.org.nz/etv/TV_One_1900_1403200... 154209978 With patches of bush and some good keen hunte...
... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
511 etv/hyundai_country_cale_tv1_20240414_1900 Hyundai Country Calendar - Season 2024 - 14 Ap... NaN 00:30:33 Documentary, Reality Agriculture & Horticulture, Business, Careers,... G TV 1 2024-04-14 19:00:00 518923 https://cdn.etv.org.nz/etv/hyundai_country_cal... 353531481 Always a favourite on every rural road. Hyund...
512 etv/hyundai_country_cale_tv1_20240421_1900 Hyundai Country Calendar - Season 2024 - 21 Ap... NaN 00:30:21 Documentary, Reality Agriculture & Horticulture, Business, Careers,... G TV 1 2024-04-21 19:00:00 518922 https://cdn.etv.org.nz/etv/hyundai_country_cal... 344594242 Always a favourite on every rural road. Hyund...
513 etv/hyundai_country_cale_tv1_20240428_1900 Hyundai Country Calendar - Season 2024 - 28 Ap... NaN 00:30:10 Documentary, Reality Agriculture & Horticulture, Business, Careers,... G TV 1 2024-04-28 19:00:00 518972 https://cdn.etv.org.nz/etv/hyundai_country_cal... 338647527 Always a favourite on every rural road. Hyund...
514 etv/hyundai_country_cale_tv1_20240505_1900 Hyundai Country Calendar - Season 2024 - 5 May... NaN 00:30:45 Documentary, Reality Agriculture & Horticulture, Business, Careers,... G TV 1 2024-05-05 19:00:00 519110 https://cdn.etv.org.nz/etv/hyundai_country_cal... 352367660 Always a favourite on every rural road. Hyund...
515 etv/hyundai_country_cale_tv1_20240512_1900 Hyundai Country Calendar - Season 2024 - 12 Ma... NaN 00:30:53 Documentary, Reality Agriculture & Horticulture, Business, Careers,... G TV 1 2024-05-12 19:00:00 519287 https://cdn.etv.org.nz/etv/hyundai_country_cal... 348651686 Always a favourite on every rural road. Hyund...

516 rows × 14 columns

In [2]:
df["Year"] = df["Recording Date"].str[:4]
✔️ 1.8 ms (2024-05-21T14:37:31/2024-05-21T14:37:31)
In [3]:
df["year_in_name"] = df.Name.str.extract("(\d{4})")
✔️ 2.76 ms (2024-05-21T14:37:31/2024-05-21T14:37:31)
In [4]:
mismatches = df[(df.Year != df.year_in_name) & ~df.year_in_name.isna()]
df.loc[mismatches.index, "Recording Date"] = df.year_in_name + "-01-01"
df.index = pd.to_datetime(df["Recording Date"])
df
✔️ 20.9 ms (2024-05-21T14:37:32/2024-05-21T14:37:32)
Out[4]:
File Name Name Description Duration Type Subject Age Level Channel Recording Date Recording Time id link size_bytes transcript Year year_in_name
Recording Date
2008-10-23 etv/country_calendar_-_air_new_zealand_uniform... country calendar - air new zealand uniforms 23... from www.throng.co.nz : this episode of nation... 00:21:33 Library NaN G lostplusfound 2008-10-23 00:00:00 61580 https://cdn.etv.org.nz/etv/country_calendar_-_... 145371146 Music Music Music Music Music Music Music Mus... 2008 2008
2009-02-21 etv/TV_One_1900_21022009_20090221193153256qtst... Country Calendar - Series 2009 - Episode 1 'Lyn of Taranaki' Country Calendar starts its ... 00:29:57 Documentary Agriculture & Horticulture, Business, Ecology ... Unrated TV 1 2009-02-21 19:00:00 12564 https://cdn.etv.org.nz/etv/TV_One_1900_2102200... 49973516 Lin Webster's life has taken many unusual twi... 2009 2009
2009-02-28 etv/TV_One_1900_28022009_20090228193154256qtst... Country Calendar - Series 2009 - Episode 2 'Shared Fruit' New Zealand and Chinese scienti... 00:29:56 Documentary Agriculture & Horticulture, Asian Studies, Bus... Unrated TV 1 2009-02-28 19:00:00 12697 https://cdn.etv.org.nz/etv/TV_One_1900_2802200... 52854708 Yunnan, tucked away in the remote southwest o... 2009 2009
2009-03-07 etv/TV_One_1900_07032009_20090307193229256qtst... Country Calendar - Series 2009 - Episode 3 'Grassroots Leader' Bruce Rollinson is a late-... 00:29:56 Documentary Agriculture & Horticulture, Business, Economic... Unrated TV 1 2009-03-07 19:00:00 12812 https://cdn.etv.org.nz/etv/TV_One_1900_0703200... 51865839 The season on Mount Ropiku was the best for y... 2009 2009
2009-03-14 etv/TV_One_1900_14032009_20090314193119768qtst... Country Calendar - Series 2009 - Episode 4 'High-flying Fur' What's as fine as merino woo... 00:29:56 Documentary Agriculture & Horticulture, Biology, Business,... Unrated TV 1 2009-03-14 19:00:00 12939 https://cdn.etv.org.nz/etv/TV_One_1900_1403200... 154209978 With patches of bush and some good keen hunte... 2009 2009
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2024-04-14 etv/hyundai_country_cale_tv1_20240414_1900 Hyundai Country Calendar - Season 2024 - 14 Ap... NaN 00:30:33 Documentary, Reality Agriculture & Horticulture, Business, Careers,... G TV 1 2024-04-14 19:00:00 518923 https://cdn.etv.org.nz/etv/hyundai_country_cal... 353531481 Always a favourite on every rural road. Hyund... 2024 2024
2024-04-21 etv/hyundai_country_cale_tv1_20240421_1900 Hyundai Country Calendar - Season 2024 - 21 Ap... NaN 00:30:21 Documentary, Reality Agriculture & Horticulture, Business, Careers,... G TV 1 2024-04-21 19:00:00 518922 https://cdn.etv.org.nz/etv/hyundai_country_cal... 344594242 Always a favourite on every rural road. Hyund... 2024 2024
2024-04-28 etv/hyundai_country_cale_tv1_20240428_1900 Hyundai Country Calendar - Season 2024 - 28 Ap... NaN 00:30:10 Documentary, Reality Agriculture & Horticulture, Business, Careers,... G TV 1 2024-04-28 19:00:00 518972 https://cdn.etv.org.nz/etv/hyundai_country_cal... 338647527 Always a favourite on every rural road. Hyund... 2024 2024
2024-05-05 etv/hyundai_country_cale_tv1_20240505_1900 Hyundai Country Calendar - Season 2024 - 5 May... NaN 00:30:45 Documentary, Reality Agriculture & Horticulture, Business, Careers,... G TV 1 2024-05-05 19:00:00 519110 https://cdn.etv.org.nz/etv/hyundai_country_cal... 352367660 Always a favourite on every rural road. Hyund... 2024 2024
2024-05-12 etv/hyundai_country_cale_tv1_20240512_1900 Hyundai Country Calendar - Season 2024 - 12 Ma... NaN 00:30:53 Documentary, Reality Agriculture & Horticulture, Business, Careers,... G TV 1 2024-05-12 19:00:00 519287 https://cdn.etv.org.nz/etv/hyundai_country_cal... 348651686 Always a favourite on every rural road. Hyund... 2024 2024

516 rows × 16 columns

In [5]:
keywords = ["dirt", "soil", "ground", "land", "earth"]
for word in keywords:
  # \b means a word boundary, so we only get complete words, and not words like dirty, or grounded, or zealand
  df[word] = df.transcript.str.lower().str.count(rf"\b{word}\b")
df
✔️ 1.41 s (2024-05-21T14:37:32/2024-05-21T14:37:33)
Out[5]:
File Name Name Description Duration Type Subject Age Level Channel Recording Date Recording Time ... link size_bytes transcript Year year_in_name dirt soil ground land earth
Recording Date
2008-10-23 etv/country_calendar_-_air_new_zealand_uniform... country calendar - air new zealand uniforms 23... from www.throng.co.nz : this episode of nation... 00:21:33 Library NaN G lostplusfound 2008-10-23 00:00:00 ... https://cdn.etv.org.nz/etv/country_calendar_-_... 145371146 Music Music Music Music Music Music Music Mus... 2008 2008 0 0 0 0 0
2009-02-21 etv/TV_One_1900_21022009_20090221193153256qtst... Country Calendar - Series 2009 - Episode 1 'Lyn of Taranaki' Country Calendar starts its ... 00:29:57 Documentary Agriculture & Horticulture, Business, Ecology ... Unrated TV 1 2009-02-21 19:00:00 ... https://cdn.etv.org.nz/etv/TV_One_1900_2102200... 49973516 Lin Webster's life has taken many unusual twi... 2009 2009 0 2 0 2 1
2009-02-28 etv/TV_One_1900_28022009_20090228193154256qtst... Country Calendar - Series 2009 - Episode 2 'Shared Fruit' New Zealand and Chinese scienti... 00:29:56 Documentary Agriculture & Horticulture, Asian Studies, Bus... Unrated TV 1 2009-02-28 19:00:00 ... https://cdn.etv.org.nz/etv/TV_One_1900_2802200... 52854708 Yunnan, tucked away in the remote southwest o... 2009 2009 0 0 0 6 0
2009-03-07 etv/TV_One_1900_07032009_20090307193229256qtst... Country Calendar - Series 2009 - Episode 3 'Grassroots Leader' Bruce Rollinson is a late-... 00:29:56 Documentary Agriculture & Horticulture, Business, Economic... Unrated TV 1 2009-03-07 19:00:00 ... https://cdn.etv.org.nz/etv/TV_One_1900_0703200... 51865839 The season on Mount Ropiku was the best for y... 2009 2009 0 5 3 8 0
2009-03-14 etv/TV_One_1900_14032009_20090314193119768qtst... Country Calendar - Series 2009 - Episode 4 'High-flying Fur' What's as fine as merino woo... 00:29:56 Documentary Agriculture & Horticulture, Biology, Business,... Unrated TV 1 2009-03-14 19:00:00 ... https://cdn.etv.org.nz/etv/TV_One_1900_1403200... 154209978 With patches of bush and some good keen hunte... 2009 2009 0 0 0 2 1
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2024-04-14 etv/hyundai_country_cale_tv1_20240414_1900 Hyundai Country Calendar - Season 2024 - 14 Ap... NaN 00:30:33 Documentary, Reality Agriculture & Horticulture, Business, Careers,... G TV 1 2024-04-14 19:00:00 ... https://cdn.etv.org.nz/etv/hyundai_country_cal... 353531481 Always a favourite on every rural road. Hyund... 2024 2024 0 0 0 1 0
2024-04-21 etv/hyundai_country_cale_tv1_20240421_1900 Hyundai Country Calendar - Season 2024 - 21 Ap... NaN 00:30:21 Documentary, Reality Agriculture & Horticulture, Business, Careers,... G TV 1 2024-04-21 19:00:00 ... https://cdn.etv.org.nz/etv/hyundai_country_cal... 344594242 Always a favourite on every rural road. Hyund... 2024 2024 0 1 0 4 0
2024-04-28 etv/hyundai_country_cale_tv1_20240428_1900 Hyundai Country Calendar - Season 2024 - 28 Ap... NaN 00:30:10 Documentary, Reality Agriculture & Horticulture, Business, Careers,... G TV 1 2024-04-28 19:00:00 ... https://cdn.etv.org.nz/etv/hyundai_country_cal... 338647527 Always a favourite on every rural road. Hyund... 2024 2024 0 0 2 1 0
2024-05-05 etv/hyundai_country_cale_tv1_20240505_1900 Hyundai Country Calendar - Season 2024 - 5 May... NaN 00:30:45 Documentary, Reality Agriculture & Horticulture, Business, Careers,... G TV 1 2024-05-05 19:00:00 ... https://cdn.etv.org.nz/etv/hyundai_country_cal... 352367660 Always a favourite on every rural road. Hyund... 2024 2024 0 0 0 5 0
2024-05-12 etv/hyundai_country_cale_tv1_20240512_1900 Hyundai Country Calendar - Season 2024 - 12 Ma... NaN 00:30:53 Documentary, Reality Agriculture & Horticulture, Business, Careers,... G TV 1 2024-05-12 19:00:00 ... https://cdn.etv.org.nz/etv/hyundai_country_cal... 348651686 Always a favourite on every rural road. Hyund... 2024 2024 0 0 0 0 1

516 rows × 21 columns

In [6]:
df[keywords].describe()
✔️ 20.4 ms (2024-05-21T14:37:33/2024-05-21T14:37:33)
Out[6]:
dirt soil ground land earth
count 516.000000 516.000000 516.000000 516.000000 516.000000
mean 0.182171 1.139535 1.220930 3.451550 0.269380
std 0.569237 2.519806 1.526983 3.661871 0.549592
min 0.000000 0.000000 0.000000 0.000000 0.000000
25% 0.000000 0.000000 0.000000 1.000000 0.000000
50% 0.000000 0.000000 1.000000 2.500000 0.000000
75% 0.000000 1.000000 2.000000 5.000000 0.000000
max 5.000000 19.000000 12.000000 29.000000 4.000000
In [7]:
df.resample("1M")[keywords].sum().plot(height=800)
✔️ 255 ms (2024-05-21T14:37:33/2024-05-21T14:37:34)
In [8]:
topic_model = BERTopic(representation_model=KeyBERTInspired(), min_topic_size=5)
topic_model.fit(df.transcript)
topic_model.get_topic_info()
✔️ 17.7 s (2024-05-21T14:37:34/2024-05-21T14:37:52)
Out[8]:
Topic Count Name Representation Representative_Docs
0 -1 209 -1_farm_home_country_over [farm, home, country, over, going, weve, bit, ... [ At home on every rural road, Hyundai Country...
1 0 36 0_fishing_fish_boat_catch [fishing, fish, boat, catch, around, bit, abou... [ At home on every rural road, Hyundai Country...
2 1 30 1_fruit_orchard_trees_tree [fruit, orchard, trees, tree, weve, are, years... [ Always a favourite on every rural road. Hyun...
3 2 18 2_horse_horses_riding_farm [horse, horses, riding, farm, bit, work, going... [ At home on every rural road, Hyundai Country...
4 3 16 3_farm_land_country_place [farm, land, country, place, bit, home, very, ... [ Just what is it about Country Canada that's ...
5 4 15 4_cows_farm_milking_dairy [cows, farm, milking, dairy, milk, work, make,... [ At home on every rural road, Hyundai Country...
6 5 15 5_winery_vineyard_wines_wine [winery, vineyard, wines, wine, grapes, zealan... [ Good night. It's been a fickle season for gr...
7 6 14 6_farm_deer_dogs_work [farm, deer, dogs, work, dog, bit, around, goi... [ At home on every rural road, Hyundai Country...
8 7 14 7_farm_bit_weve_things [farm, bit, weve, things, going, ha, take, to,... [ Always a favourite on every rural road. Kyun...
9 8 13 8_beekeeping_bees_bee_hive [beekeeping, bees, bee, hive, hives, honey, bu... [ Always a favourite on every rural road. Hyun...
10 9 13 9_zealand_day_island_all [zealand, day, island, all, very, going, just,... [ Rest of your night, let's hope the ABs serve...
11 10 12 10_calf_calves_bull_farm [calf, calves, bull, farm, bulls, very, get, b... [ At home on every rural road, Hyundai Country...
12 11 12 11_farm_muster_home_country [farm, muster, home, country, going, around, w... [ I wouldn't change my life for anything. You ...
13 12 11 12_cherry_cherries_orchard_fruit [cherry, cherries, orchard, fruit, what, get, ... [ Always a favourite on every rural road. Hyun...
14 13 11 13_sheep_farm_wool_bit [sheep, farm, wool, bit, work, going, country,... [ At home on every rural road, Hyundai Country...
15 14 11 14_milking_dairy_milk_cows [milking, dairy, milk, cows, farm, cheese, buf... [ Lin Webster's life has taken many unusual tw...
16 15 9 15_vegetable_produce_grow_growing [vegetable, produce, grow, growing, soil, make... [ Always a favourite on every rural road. Hyun...
17 16 9 16_goats_goat_sheep_farm [goats, goat, sheep, farm, milking, milk, bit,... [ At home on every rural road, Hyundai Country...
18 17 8 17_lambs_lamb_sheep_farm [lambs, lamb, sheep, farm, home, make, life, f... [ At home on every rural road, Hyundai Country...
19 18 8 18_sheep_wool_farm_zealand [sheep, wool, farm, zealand, merino, bit, are,... [ Always a favourite on every rural road. Hyun...
20 19 8 19_farm_eggs_egg_chickens [farm, eggs, egg, chickens, organic, hens, chi... [ At home on every rural road, Hyundai Country...
21 20 7 20_farm_home_flowers_going [farm, home, flowers, going, pretty, day, alwa... [ Always a favourite on every rural road. Kyun...
22 21 6 21_music_zealand_valley_land [music, zealand, valley, land, gold, farm, wha... [ Music Music Music Music Music Music Music Mu...
23 22 6 22_pigs_pig_pork_farm [pigs, pig, pork, farm, zealand, bit, business... [ At home on every rural road, Hyundai Country...
24 23 5 23_farming_farm_sheep_zealand [farming, farm, sheep, zealand, wool, blair, o... [ At home on every rural road, Hyundai Country...
In [9]:
topic_model.visualize_topics()
✔️ 1.48 s (2024-05-21T14:37:52/2024-05-21T14:37:53)
In [10]:
topics_over_time = topic_model.topics_over_time(df.transcript, df.index, nr_bins=10)
topics_over_time
✔️ 7.72 s (2024-05-21T14:37:53/2024-05-21T14:38:01)
Out[10]:
Topic Words Frequency Timestamp
0 4 cows, cow, milking, milk, bullocks 1 1986-12-18 08:29:45.600
1 6 deer, helicopters, helicopter, choppers, tim 1 1986-12-18 08:29:45.600
2 -1 ron, rons, build, get, work 1 1990-09-26 14:24:00.000
3 7 musical, instruments, live, instrument, farm 1 1990-09-26 14:24:00.000
4 11 muster, musterers, hills, horses, sheep 1 1994-06-22 04:48:00.000
... ... ... ... ...
99 17 lambs, sheep, farmers, farm, zealand 1 2020-08-16 09:36:00.000
100 18 sheep, wool, farm, merino, hops 4 2020-08-16 09:36:00.000
101 19 farm, eggs, egg, chickens, birds 3 2020-08-16 09:36:00.000
102 20 farm, home, dogs, going, morning 2 2020-08-16 09:36:00.000
103 22 pigs, pig, farm, brood, bees 1 2020-08-16 09:36:00.000

104 rows × 4 columns

In [11]:
topic_model.visualize_topics_over_time(topics_over_time, top_n_topics=20, height=600)
✔️ 62.4 ms (2024-05-21T14:38:01/2024-05-21T14:38:01)