sung.chords_index¶
A SQLite + FTS5 index of the chords-and-lyrics corpus, read without the corpus in memory.
The corpus (sung.chords_and_lyrics.get_lyrics_and_chords_dataset()) is a
~650 MB CSV: as a DataFrame it costs about 1.1 GB per process. This module
writes it once to one SQLite file (titles, artists, language, popularity, each
chart zlib-compressed, and an FTS5 table of the charts’ words) and reads that
file back piecemeal: the short columns for matching, the words for lyrics
search, one chart at a time.
Note
The corpus’s popularity column is the Spotify artist popularity (0-100), copied
onto every song row by the dataset’s scraper: constant per artist, integer-valued,
a snapshot no later than March 2022. Sorting by it sorts by artist, with arbitrary
order inside an artist. Treat it as an artist-level prior, not a song signal.
It imports only the standard library, so a web worker that serves from the
index never loads pandas (thorwhalen/sung#17). Building it does load the corpus
(through pandas), so run build_chords_index() once, apart from any server.
Ranking and fuzzy matching are the consumer’s business (songleaf,
scoreseek); this module only stores and fetches.
>>> import os, tempfile
>>> rows = [
... (1, "The Band", "Paper Boats", "G\nPaper boats drift", "en", 3),
... (2, "Other", "Quiet Road", "C\nquiet road", "en", 1),
... ]
>>> path = os.path.join(tempfile.mkdtemp(), INDEX_NAME)
>>> build_chords_index(path, rows=rows)["songs"]
2
>>> read_columns(path)["titles"]
['Paper Boats', 'Quiet Road']
>>> rows_with_words(path, ["boats"])
[0]
>>> print(read_chart(path, 0))
G
Paper boats drift
Module Attributes
Environment variable naming a local copy of the corpus zip. |
|
The index's file name; it lives beside the corpus zip by convention. |
|
Environment variable naming the index file. |
|
The corpus columns the index is built from, in the order |
|
What |
Functions
|
Write the index to |
|
A read-only connection to the index, closed on exit of a |
|
The corpus's rows, loaded through pandas (see |
|
The rows |
|
|
Return the path of a local copy of the chords-and-lyrics zip, or |
|
Return the path of a local index file, or |
|
|
The chart (chords over lyrics) of |
|
The short columns ( |
|
What the build recorded: song count and build time. |
|
The rows whose chart contains every one of |
- sung.chords_index.CHORDS_AND_LYRICS_ZIP_ENVVAR = 'SUNG_CHORDS_AND_LYRICS_ZIP'¶
Environment variable naming a local copy of the corpus zip.
- sung.chords_index.CHORDS_INDEX_ENVVAR = 'SUNG_CHORDS_INDEX'¶
Environment variable naming the index file.
- sung.chords_index.COLUMN_NAMES = ('ids', 'titles', 'artists', 'langs', 'popularity')¶
What
read_columns()returns, in row order.popularityis Spotify artist popularity (constant per artist, a 2022 snapshot), not a song signal.
- sung.chords_index.CORPUS_COLUMNS = ('Unnamed: 0', 'artist_name', 'song_name', 'chords&lyrics', 'lang', 'popularity')¶
The corpus columns the index is built from, in the order
rowsgives them: song id, artist, title, chart (chords over lyrics), language, popularity.
- sung.chords_index.INDEX_NAME = 'chords-and-lyrics.sqlite'¶
The index’s file name; it lives beside the corpus zip by convention.
- sung.chords_index.build_chords_index(path, *, rows=None, batch_size=5000)[source]¶
Write the index to
path; return its song count, size and build time.rowsare tuples inCORPUS_COLUMNSorder (default:corpus_rows(), which loads the corpus). Rownofrowsis rownof the index. Thepopularitystored is the corpus’s, which is Spotify artist popularity (see the module docstring), not song popularity. The file is written besidepathand moved into place when complete, so a reader never sees half an index.- Return type:
- sung.chords_index.connect(path)[source]¶
A read-only connection to the index, closed on exit of a
withblock.One connection per call is safe across threads. Raises
FileNotFoundErrorwhen there is no index atpath.
- sung.chords_index.corpus_rows(*, zip_path=None)[source]¶
The corpus’s rows, loaded through pandas (see
get_lyrics_and_chords_dataset).zip_pathdefaults tolocal_chords_and_lyrics_zip()(and, failing that, a download through haggle): a consumer that keeps the zip elsewhere passes it.
- sung.chords_index.dataframe_rows(df)[source]¶
The rows
build_chords_index()takes, from a corpus DataFrame.
- sung.chords_index.index_version(path)[source]¶
(mtime_ns, size)of the index file, orNone: changes when it is rebuilt.
- sung.chords_index.local_chords_and_lyrics_zip()[source]¶
Return the path of a local copy of the chords-and-lyrics zip, or
None.Looks at
$SUNG_CHORDS_AND_LYRICS_ZIPfirst, then at wherehagglekeeps its downloads ($HAGGLE_ROOTDIR/zips/<owner>/<dataset>.zip, root defaulting to~/haggle). The haggle layout is replicated rather than asked of haggle, because importing haggle importskaggle, which authenticates against Kaggle at import time, even for a cached file.
- sung.chords_index.local_chords_index()[source]¶
Return the path of a local index file, or
None.$SUNG_CHORDS_INDEXfirst (an error if it names no file), thenINDEX_NAMEbeside the local corpus zip (local_chords_and_lyrics_zip()). An index kept elsewhere (songleaf keeps its own under its data root) is found only through the variable, or passed by path to the readers.
- sung.chords_index.read_chart(path, row)[source]¶
The chart (chords over lyrics) of
row;""for a row not in the index.- Return type:
- sung.chords_index.read_columns(path)[source]¶
The short columns (
COLUMN_NAMES), each a list in row order.
- sung.chords_index.rows_with_words(path, words)[source]¶
The rows whose chart contains every one of
words(whole words, any case).Words are matched as FTS5 tokens (
unicode61:_separates words, accents are kept). Tokenizing a query is the caller’s business: pass words, not a sentence. No words, no rows.