Corpus

Full module path: melody_features.feature_definitions.corpus.

Also importable via import melody_features as mf (for example mf.pitch_range).

Corpus-dependent FANTASTIC feature definitions.

get_ngram_document_frequency(ngram, corpus_stats)[source]

Retrieve the document frequency for a given n-gram from the corpus statistics.

Parameters:
  • ngram (tuple) – The n-gram to look up

  • corpus_stats (dict) – Dictionary containing corpus statistics

Returns:

Document frequency count for the n-gram

Return type:

int

InverseEntropyWeighting(ngram_counts, corpus_stats)[source]

Calculate local weights for n-grams using an inverse-entropy measure.

Inverse-entropy weighting is implemented following the specification in FANTASTIC and the Handbook of Latent Semantic Analysis (Landauer et al., 2007). It provides several quantifiers of the importance of an n-gram (here: m-type) based on its relative frequency in a given passage (here: melody) and its relative frequency in that passage as compared to the reference corpus.

This class contains functions to compute the local weight of an m-type, the global weight of an m-type, and the combined weight of an m-type.

Parameters:
  • ngram_counts (dict)

  • corpus_stats (dict)

tfdf_spearman(melody, corpus_stats, phrase_gap, max_ngram_order)[source]

Spearman rank correlation between TF and DF over m-types. Positive values mean higher within-melody usage tends to coincide with higher corpus-wide prevalence across m-types; negative values mean the opposite; near zero means little monotonic rank association.

Notes

Ties are ranked with the minimum-rank policy (ties.method=”min”), then Spearman correlation is computed over those ranks.

Parameters:
  • melody (Melody) – The melody to analyze

  • corpus_stats (dict) – Dictionary containing corpus statistics

  • phrase_gap (float) – Gap threshold for phrase segmentation

  • max_ngram_order (int) – Maximum n-gram order to consider

Returns:

Spearman correlation coefficient between TF and DF

Return type:

float

tfdf_kendall(melody, corpus_stats, phrase_gap, max_ngram_order)[source]

Kendall’s tau rank correlation between melody TF and corpus DF for each m-type.

Similar to tfdf_spearman, but ordinal association is measured with Kendall’s tau instead of Spearman’s rho. Positive values mean higher within-melody usage tends to coincide with higher corpus-wide prevalence across m-types; negative values mean the opposite; near zero means little monotonic rank association.

Parameters:
  • melody (Melody) – The melody to analyze

  • corpus_stats (dict) – Dictionary containing corpus statistics

  • phrase_gap (float) – Gap threshold for phrase segmentation

  • max_ngram_order (int) – Maximum n-gram order to consider

Returns:

Kendall’s tau correlation coefficient between TF and DF

Return type:

float

Notes

Ties are first converted to minimum ranks, then Kendall’s tau is computed over the resulting rank vectors.

mean_log_tfdf(melody, corpus_stats, phrase_gap, max_ngram_order)[source]

Mean of log2-normalized TF × DF products over m-types.

Higher values mean stronger alignment between within-melody usage and corpus document-frequency on the same m-types.

Parameters:
  • melody (Melody) – The melody to analyze

  • corpus_stats (dict) – Dictionary containing corpus statistics

  • phrase_gap (float) – Gap threshold for phrase segmentation

  • max_ngram_order (int) – Maximum n-gram order to consider

Returns:

Mean log2 TF-DF score

Return type:

float

norm_log_dist(melody, corpus_stats, phrase_gap, max_ngram_order)[source]

Mean absolute difference between log2-normalized TF and DF.

Larger values mean the melody emphasizes different m-types than corpus prevalence; smaller values mean closer distributional match.

Parameters:
  • melody (Melody) – The melody to analyze

  • corpus_stats (dict) – Dictionary containing corpus statistics

  • phrase_gap (float) – Gap threshold for phrase segmentation

  • max_ngram_order (int) – Maximum n-gram order to consider

Returns:

Mean absolute deviation between log2-normalized TF and DF vectors

Return type:

float

max_log_df(melody, corpus_stats, phrase_gap, max_ngram_order)[source]

log2 of the largest corpus document frequency among melody m-types. Highlights how frequent the most common pattern in the melody is relative to the corpus. Large values indicate that the melody contains at least one pattern that is very frequent in the corpus.

Parameters:
  • melody (Melody) – The melody to analyze

  • corpus_stats (dict) – Dictionary containing corpus statistics

  • phrase_gap (float) – Gap threshold for phrase segmentation

  • max_ngram_order (int) – Maximum n-gram order to consider

Returns:

Maximum log2 document frequency

Return type:

float

min_log_df(melody, corpus_stats, phrase_gap, max_ngram_order)[source]

log2 of the smallest corpus DF among melody m-types. Highlights how frequent the least common pattern in the melody is relative to the corpus. Small values indicate that the melody contains at least one pattern that is very rare in the corpus.

Parameters:
  • melody (Melody) – The melody to analyze

  • corpus_stats (dict) – Dictionary containing corpus statistics

  • phrase_gap (float) – Gap threshold for phrase segmentation

  • max_ngram_order (int) – Maximum n-gram order to consider

Returns:

Minimum log2 document frequency

Return type:

float

mean_log_df(melody, corpus_stats, phrase_gap, max_ngram_order)[source]

Mean log2 corpus DF over melody m-types. Highlights how frequent the average pattern in the melody is relative to the corpus. Large values indicate that the melody contains patterns that are relatively frequent in the corpus. Small values indicate that the melody contains patterns that are relatively rare in the corpus.

Parameters:
  • melody (Melody) – The melody to analyze

  • corpus_stats (dict) – Dictionary containing corpus statistics

  • phrase_gap (float) – Gap threshold for phrase segmentation

  • max_ngram_order (int) – Maximum n-gram order to consider

Returns:

Mean log2 document frequency

Return type:

float

mean_global_local_weight(melody, corpus_stats, phrase_gap, max_ngram_order)[source]

Mean combined local-global weights for n-grams. The combined weight of an m-type is the product of local and global weights. In this implementation, unseen m-types (DF=0) receive a neutral global weight of 1.0. Higher values therefore indicate either high local frequency, higher global weight, or both. This relates to the percept of distinctiveness.

Parameters:
  • melody (Melody) – The melody to analyze

  • corpus_stats (dict) – Dictionary containing corpus statistics

  • phrase_gap (float) – Gap threshold for phrase segmentation

  • max_ngram_order (int) – Maximum n-gram order to consider

Returns:

Mean global-local weight

Return type:

float

std_global_local_weight(melody, corpus_stats, phrase_gap, max_ngram_order)[source]

Sample standard deviation of combined local-global weights for n-grams.

Parameters:
  • melody (Melody) – The melody to analyze

  • corpus_stats (dict) – Dictionary containing corpus statistics

  • phrase_gap (float) – Gap threshold for phrase segmentation

  • max_ngram_order (int) – Maximum n-gram order to consider

Returns:

Standard deviation of global-local weight

Return type:

float

mean_global_weight(melody, corpus_stats, phrase_gap, max_ngram_order)[source]

Mean global weight across m-types.

Higher values mean the m-types are less globally informative (more expected); lower values mean they are more globally informative (more distinctive).

Parameters:
  • melody (Melody) – The melody to analyze

  • corpus_stats (dict) – Dictionary containing corpus statistics

  • phrase_gap (float) – Gap threshold for phrase segmentation

  • max_ngram_order (int) – Maximum n-gram order to consider

Returns:

Mean global weight

Return type:

float

std_global_weight(melody, corpus_stats, phrase_gap, max_ngram_order)[source]

Sample standard deviation of global weights for m-types.

Parameters:
  • melody (Melody) – The melody to analyze

  • corpus_stats (dict) – Dictionary containing corpus statistics

  • phrase_gap (float) – Gap threshold for phrase segmentation

  • max_ngram_order (int) – Maximum n-gram order to consider

Returns:

Standard deviation of global weight

Return type:

float

get_corpus_features(melody, corpus_stats, phrase_gap, max_ngram_order)[source]

Compute all corpus-based features for a melody.

Parameters:
  • melody (Melody) – The melody to analyze

  • corpus_stats (dict) – Dictionary containing corpus statistics

  • phrase_gap (float)

  • max_ngram_order (int)

Returns:

Dictionary of corpus-based feature values

Return type:

Dict