count_melodic_ngrams
Count and rank melodic n-gram patterns by frequency.
Overview
This helper is designed for questions like:
- "How many times does each pattern occur?"
- "What are the top 2 melodic n-grams?"
- "Which motive is most common in this piece?"
Instead of returning one row per location, it groups occurrences by pattern string and returns counts sorted from most frequent to least frequent.
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
filename |
str |
Yes | - | Name of the MEI file |
n |
int |
No | 4 | Length of n-grams to extract |
kind |
str |
No | 'd' |
Interval type: 'd', 'c', 'q', or 'z' |
entries |
bool |
No | False |
Restrict to thematic entries only |
combine_unisons |
bool \| None |
No | None |
Whether to combine unisons when extracting notes |
compound |
bool |
No | False |
Whether to use compound intervals |
Returns
| Key | Type | Description |
|---|---|---|
filename |
str |
The input filename |
n |
int |
The n-gram length used |
kind |
str |
The interval type used |
entries |
bool |
Whether entry filtering was applied |
combine_unisons |
bool \| None |
Whether unison combining was explicitly applied |
compound |
bool |
Whether compound intervals were used |
pattern_counts |
list[dict] |
Ranked pattern counts |
Example Output
{
"filename": "Bach_BWV_0772.mei",
"n": 4,
"kind": "d",
"entries": False,
"pattern_counts": [
{"pattern": ["2", "2", "2", "-3"], "pattern_string": "2_2_2_-3", "count": 6},
{"pattern": ["2", "2", "-3", "2"], "pattern_string": "2_2_-3_2", "count": 5},
],
}
Typical Workflow
- Use
get_melodic_ngramsto inspect the full location table. - Use
count_melodic_ngramsto answer frequency and ranking questions. - Use
get_melodic_ngram_matchesonly for the patterns you want to highlight.