Word

Word(
    syllables: Iterable[str],
    stress: Mapping[int, LexicalStress] | Iterable[LexicalStress | None] | None = None,
)

The syllables of one word, as they are sung, and its accent pattern.

One Word is shared by the LyricSyllables that spell it; each of them holds an index into it. Nobody needs to build one by hand: lyrics are typed as text and go through parse_lyrics (or Word.from_string).

Syllables are the display form, so punctuation stays inside them ("ia,"). stress is parallel to syllables; unmarked syllables are None, and at most one syllable is PRIMARY.

>>> word = Word.from_string("al-le-'lu-ia")
>>> word
Word('al', 'le', 'lu', 'ia', stress={2: 'primary'})
>>> str(word), len(word), word[2], word.primary
('alleluia', 4, 'lu', 2)
>>> word.stress_at(2), word.stress_at(0)
(<LexicalStress.PRIMARY: 'primary'>, None)

Stress can also be given by index or as a parallel sequence:

>>> Word(["al", "le", "lu", "ia"], stress={2: LexicalStress.PRIMARY}) == word
True
>>> Word(["al", "le", "lu", "ia"], stress=[None, None, LexicalStress.PRIMARY, None]) == word
True
>>> Word([])
Traceback (most recent call last):
...
openmusickit.errors.LyricConsistencyError: A Word needs at least one syllable.
>>> Word(["al", ""])
Traceback (most recent call last):
...
openmusickit.errors.LyricConsistencyError: A syllable cannot be empty: ['al', ''].
>>> Word(["al", "le"], stress={0: LexicalStress.PRIMARY, 1: LexicalStress.PRIMARY})
Traceback (most recent call last):
...
openmusickit.errors.LyricConsistencyError: A Word has at most one PRIMARY stress, got 2 in ['al', 'le'].

Attributes

Name Description
syllables
stress
primary The index of the PRIMARY syllable, if one is marked.

Methods

Name Description
__init__
from_string Builds a Word from its hyphenated spelling.
stress_at The stress of the syllable at index, or None if none is marked.
__iter__
__len__
__getitem__
__str__
__repr__

__init__

__init__(
    syllables: Iterable[str],
    stress: Mapping[int, LexicalStress] | Iterable[LexicalStress | None] | None = None,
)

from_string

from_string(
    word: str,
    hyphen: str = '-',
    primary: str | None = "'",
    secondary: str | None = ',',
)

Builds a Word from its hyphenated spelling.

A syllable prefixed with primary carries the primary stress, one prefixed with secondary a secondary stress (the IPA marks ˈ and ˌ, transliterated). A leading comma is never punctuation, so the default marks do not collide with a trailing one. Empty pieces from doubled or trailing hyphens are dropped. Pass primary=None (and secondary=None) to turn stress markup off.

>>> Word.from_string("Je-sus")
Word('Je', 'sus')
>>> Word.from_string("'Je-sus,")
Word('Je', 'sus,', stress={0: 'primary'})
>>> Word.from_string(",un-der-'stand")
Word('un', 'der', 'stand', stress={0: 'secondary', 2: 'primary'})
>>> Word.from_string("'tis", primary=None)
Word("'tis")
>>> Word.from_string("Al--le-")
Word('Al', 'le')

stress_at

stress_at(index: int)

The stress of the syllable at index, or None if none is marked.

>>> word = Word.from_string("al-le-'lu-ia")
>>> word.stress_at(2), word.stress_at(0)
(<LexicalStress.PRIMARY: 'primary'>, None)

__iter__

__iter__()

__len__

__len__()

__getitem__

__getitem__(index)

__str__

__str__()

__repr__

__repr__()