Tokenizers and n-grams
FANTASTIC m-type tokenization and n-gram counting, plus MUST pitch / interval / duration distributions. FANTASTIC pipeline: FANTASTIC workflow in Usage. MUST is documented separately under “MUST tokenization” on that page.
Melody tokenizer
- class MType(pitch_interval, ioi_ratio)[source]
Bases:
objectA class representing a melody token based on pitch interval and IOI ratio classifications.
- class FantasticTokenizer(scheme='FANTASTIC')[source]
Bases:
MelodyTokenizerFANTASTIC melody tokenization using classified interval and IOI-ratio m-types.
- Parameters:
scheme (str)
- __init__(scheme='FANTASTIC')[source]
Initialize the tokenizer with a specific interval classification scheme.
- Parameters:
scheme (str, optional) – The scheme to use for pitch interval classification, by default “FANTASTIC” Options: “FANTASTIC”, “SIMILE”
- class MustDistribution(values, weights)[source]
Bases:
objectNormalized MUST distribution weights with their category labels.
- class MustTokenizer[source]
Bases:
MelodyTokenizerMUST distribution tokenization (Clemente et al., 2020).
Implements pdist*, idist*, and ddist* on notematrix-style timing: onsets and durations in beats.
- static pitch_distribution(pitches)[source]
Marginal pitch distribution (pdist1 on a pitch vector).
- Parameters:
pitches (ndarray)
- Return type:
- pdist1(melody)[source]
Pitch distribution (MUST pdist1.m).
- Parameters:
melody (Melody)
- Return type:
- pdist2(melody)[source]
2-tuple pitch distribution (MUST pdist2.m).
- Parameters:
melody (Melody)
- Return type:
- pdist3(melody)[source]
3-tuple pitch distribution (MUST pdist3.m).
- Parameters:
melody (Melody)
- Return type:
- idist1(melody)[source]
Interval distribution marginalized from pdist2 (MUST idist1.m).
- Parameters:
melody (Melody)
- Return type:
- idist2(melody)[source]
2-interval distribution marginalized from pdist3 (MUST idist2.m).
- Parameters:
melody (Melody)
- Return type:
- ddist1(melody)[source]
Duration distribution in beats (MUST ddist1.m).
- Parameters:
melody (Melody)
- Return type:
- ddist2(melody)[source]
2-tuple duration distribution (MUST ddist2.m).
- Parameters:
melody (Melody)
- Return type:
N-gram counter
- class NGramCounter[source]
Bases:
objectA stateful n-gram counter that accumulates counts across multiple sequences.
- count_ngrams(tokens, max_order=5)[source]
Count n-grams in the token sequence up to max_order length.
- property yules_k: float
Yule’s K measure of m-type repetitiveness.
This lexical-diversity feature is calculated from the frequency spectrum of m-types in the melody. Higher values indicate that a smaller set of m-types is repeated more often, whereas lower values indicate a more even or varied m-type vocabulary.
Citation
Yule (1944)
- property simpsons_d: float
Simpson’s D measure of m-type concentration.
Simpson’s D is calculated from squared m-type frequencies. Higher values indicate a greater probability that two sampled tokens belong to the same m-type, and therefore a more concentrated or repetitive m-type vocabulary.
Citation
Simpson (1949)
- property sichels_s: float
The proportion of m-types that occur exactly twice.
Sichel’s S is the number of distinct m-types with frequency two divided by the total number of distinct m-types. Higher values indicate that more of the melody’s m-type vocabulary consists of types that recur once.
Citation
Sichel (1975)
- property honores_h: float
Honoré’s H measure of m-type lexical richness.
Honoré’s H relates the total number of m-type tokens to the proportion of distinct m-types that occur exactly once (hapax legomena). It increases when a sequence contains many single-occurrence m-types relative to its overall m-type vocabulary.
Citation
Honoré (1979)
- property mean_entropy: float
Mean zeroth-order m-type entropy across counted n-gram orders.
For each n-gram order, this feature treats the m-type counts as a discrete distribution and computes zeroth-order entropy. The returned value is the mean of those entropy values across the counted orders. Higher values indicate more even m-type distributions.
- property mean_productivity: float
The proportion of distinct m-types that occur only once.
M-types that occur only once are hapax legomena. This feature divides the number of hapax m-types by the total number of distinct m-types, so higher values indicate that more of the melody’s m-type vocabulary is used only once.