sung.chords_and_lyrics¶
Tools for chords and lyrics acquisition and processing.
Example usage. The song is inlined rather than read from a file so that the
to='text' examples below run as real doctests instead of being skipped:
an example that is never executed is free to drift away from the signature it
claims to demonstrate.
>>> raw = '''[Verse 1]
... C G
... Hello there my friend
... Am F
... This is a line of lyrics
... '''
>>> print(render_chords_and_lyrics(raw, to='text'))
[Verse 1]
C G
Hello there my friend
Am F
This is a line of lyrics
The same call with to='pdf' writes a file instead of returning text:
>>> render_chords_and_lyrics(raw, to='pdf', output_path='out.pdf', lyrics_font={'size':14})
New features:
Filter out non-lyrics content. The keyword is apply_filter_non_lyrics;
filter_non_lyrics is the standalone function it delegates to, and passing
that name as a keyword would land in **kwargs and be silently ignored.
>>> print(render_chords_and_lyrics(raw, to='text', apply_filter_non_lyrics=True))
C G
Hello there my friend
Am F
This is a line of lyrics
>>> print(remove_non_lyrics(raw))
C G
Hello there my friend
Am F
This is a line of lyrics
Keep the section markers ([Verse 1] and friends) while filtering:
>>> print(render_chords_and_lyrics(
... raw, to='text', apply_filter_non_lyrics=True, keep_metadata_lines=True
... ))
[Verse 1]
C G
Hello there my friend
Am F
This is a line of lyrics
Pack lines for better space usage (not sure this works as intended):
>>> packed_txt = render_chords_and_lyrics(raw, to='text', pack_lines=True, max_line_length=80)
>>> packed_txt = pack_song_text(raw, max_length=80)
Combine both features:
>>> optimized = render_chords_and_lyrics(raw, to='text', apply_filter_non_lyrics=True, pack_lines=True)
Module Attributes
Environment variable naming a local copy of the corpus zip. |
Functions
|
|
|
If song is a string, parse it and return (list_of_sections, raw_text). |
|
|
|
Filter out non-lyrics content from a song. |
|
|
|
Load the Kaggle chords-and-lyrics corpus (~135K songs) as a DataFrame. |
|
|
|
Determine if a line is likely to contain lyrics. |
Return the path of a local copy of the chords-and-lyrics zip, or |
|
|
Pack multiple short lines together to make better use of horizontal space. |
|
Convenience function to pack lines and return as text. |
|
Generator: parse fixed-width chords/lyrics and yield dicts with 'chords' and 'lyrics'. |
|
Convenience function to filter non-lyrics and return as text. |
|
Aggregator: render to 'pdf' or 'text'. |
|
Render song to PDF with styling parameters. |
Reconstruct fixed-width text from parsed song data. |
|
|
Resolve page size from string or return the object as-is. |
|
Search for a song by title, lyrics, or artist. |
Classes
|
- sung.chords_and_lyrics.CHORDS_AND_LYRICS_ZIP_ENVVAR = 'SUNG_CHORDS_AND_LYRICS_ZIP'¶
Environment variable naming a local copy of the corpus zip.
- sung.chords_and_lyrics.ensure_parsed_song(song)[source]¶
If song is a string, parse it and return (list_of_sections, raw_text). If song is already iterable of dicts, return (list(song), None).
- sung.chords_and_lyrics.filter_non_lyrics(song, keep_metadata_lines=False)[source]¶
Filter out non-lyrics content from a song.
- sung.chords_and_lyrics.get_lyrics_and_chords_dataset(*, zip_path=None, usecols=None)[source]¶
Load the Kaggle chords-and-lyrics corpus (~135K songs) as a DataFrame.
A local zip is read directly, with no Kaggle credentials:
zip_pathif given, elselocal_chords_and_lyrics_zip(). Only when there is no local copy is the dataset downloaded throughhaggle(which needs credentials).- Parameters:
zip_path – Path to the dataset zip (as downloaded from Kaggle).
usecols – Columns to load. The CSV is ~650 MB; loading only the columns you need roughly halves the time and memory.
- sung.chords_and_lyrics.is_likely_lyrics(line, has_chords_before=False)[source]¶
Determine if a line is likely to contain lyrics.
- sung.chords_and_lyrics.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_and_lyrics.pack_song_lines(song, max_line_length=80, preserve_chord_alignment=True)[source]¶
Pack multiple short lines together to make better use of horizontal space.
- sung.chords_and_lyrics.pack_song_text(song, max_length=80)[source]¶
Convenience function to pack lines and return as text.
- sung.chords_and_lyrics.parse_chord_lyrics(raw_text)[source]¶
Generator: parse fixed-width chords/lyrics and yield dicts with ‘chords’ and ‘lyrics’. Each ‘chords’ is List of (chord_name, start_index).
- sung.chords_and_lyrics.remove_non_lyrics(song, keep_metadata=False)[source]¶
Convenience function to filter non-lyrics and return as text.
- sung.chords_and_lyrics.render_chords_and_lyrics(song, to='pdf', output_path=None, apply_filter_non_lyrics=False, keep_metadata_lines=False, pack_lines=False, max_line_length=80, **kwargs)[source]¶
Aggregator: render to ‘pdf’ or ‘text’.
to=’pdf’: writes file to output_path (required).
to=’text’: returns reconstructed text.
- Parameters:
to (
str) – Output format (‘pdf’ or ‘text’)output_path (
str) – Path for PDF output (required for PDF)apply_filter_non_lyrics (
bool) – If True, filter out non-lyrics contentkeep_metadata_lines (
bool) – If True, keep metadata lines (like [Verse]) when filteringpack_lines (
bool) – If True, pack short lines together for better space usagemax_line_length (
int) – Maximum length for packed lines**kwargs – Additional arguments passed to rendering functions
- Return type:
- sung.chords_and_lyrics.render_chords_and_lyrics_to_pdf(song, *, output_path, title=None, page_size='A4', margin=72, lyrics_font=None, chords_font=None, title_font=None, spacing_chord_lyrics=None, spacing_group=None, spacing_paragraph=None)[source]¶
Render song to PDF with styling parameters. Accepts raw text or parsed song.
- sung.chords_and_lyrics.render_chords_and_lyrics_to_text(song)[source]¶
Reconstruct fixed-width text from parsed song data.
- Return type: