chordseq 0.0.2__tar.gz → 0.0.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (25) hide show
  1. {chordseq-0.0.2 → chordseq-0.0.3}/PKG-INFO +1 -1
  2. {chordseq-0.0.2 → chordseq-0.0.3}/chordseq/ngrams.py +14 -5
  3. {chordseq-0.0.2 → chordseq-0.0.3}/chordseq/roman.py +85 -12
  4. {chordseq-0.0.2 → chordseq-0.0.3}/pyproject.toml +1 -1
  5. {chordseq-0.0.2 → chordseq-0.0.3}/tests/test_chordseq.py +54 -0
  6. {chordseq-0.0.2 → chordseq-0.0.3}/.github/workflows/ci.yml +0 -0
  7. {chordseq-0.0.2 → chordseq-0.0.3}/.gitignore +0 -0
  8. {chordseq-0.0.2 → chordseq-0.0.3}/LICENSE +0 -0
  9. {chordseq-0.0.2 → chordseq-0.0.3}/README.md +0 -0
  10. {chordseq-0.0.2 → chordseq-0.0.3}/chordseq/__init__.py +0 -0
  11. {chordseq-0.0.2 → chordseq-0.0.3}/chordseq/query.py +0 -0
  12. {chordseq-0.0.2 → chordseq-0.0.3}/chordseq/search.py +0 -0
  13. {chordseq-0.0.2 → chordseq-0.0.3}/chordseq/tokens.py +0 -0
  14. {chordseq-0.0.2 → chordseq-0.0.3}/misc/docs/research/chord-sequence-search-design.md +0 -0
  15. {chordseq-0.0.2 → chordseq-0.0.3}/misc/docs/research/prototypes/proto_index.py +0 -0
  16. {chordseq-0.0.2 → chordseq-0.0.3}/misc/docs/research/prototypes/proto_interval.py +0 -0
  17. {chordseq-0.0.2 → chordseq-0.0.3}/misc/docs/research/prototypes/proto_regex.py +0 -0
  18. {chordseq-0.0.2 → chordseq-0.0.3}/misc/docs/research/prototypes/proto_relative.py +0 -0
  19. {chordseq-0.0.2 → chordseq-0.0.3}/misc/docs/research/prototypes/proto_scan.mjs +0 -0
  20. {chordseq-0.0.2 → chordseq-0.0.3}/misc/docs/research/reports/01-chord-notation-and-query-language.md +0 -0
  21. {chordseq-0.0.2 → chordseq-0.0.3}/misc/docs/research/reports/02-transposition-invariant-matching-and-sequence-indexes.md +0 -0
  22. {chordseq-0.0.2 → chordseq-0.0.3}/misc/docs/research/reports/03-client-side-search-in-the-browser.md +0 -0
  23. {chordseq-0.0.2 → chordseq-0.0.3}/misc/docs/research/reports/04-popularity-data-and-ranking.md +0 -0
  24. {chordseq-0.0.2 → chordseq-0.0.3}/misc/docs/research/reports/05-prior-art-chord-progression-search.md +0 -0
  25. {chordseq-0.0.2 → chordseq-0.0.3}/tests/test_smoke.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: chordseq
3
- Version: 0.0.2
3
+ Version: 0.0.3
4
4
  Summary: Chord-progression search in any key, n-gram counts and Roman numerals over chord-symbol sequences
5
5
  Project-URL: Homepage, https://github.com/thorwhalen/chordseq
6
6
  Project-URL: Repository, https://github.com/thorwhalen/chordseq
@@ -69,6 +69,7 @@ def ngrams(
69
69
  key: Key | None = None,
70
70
  per_sequence: bool = False,
71
71
  dialect: str | Dialect = "default",
72
+ minor_reference: str = "parallel",
72
73
  ) -> Counter | list[Counter]:
73
74
  """Counts of n-grams (tuples of chord names) pooled over ``sequences``.
74
75
 
@@ -84,7 +85,7 @@ def ngrams(
84
85
  if not sizes or min(sizes) < 1:
85
86
  raise ValueError(f"n-gram sizes must be positive, not {sizes!r}")
86
87
  results = [
87
- _count(seq, sizes, level, collapse, relative_to, key, dialect)
88
+ _count(seq, sizes, level, collapse, relative_to, key, dialect, minor_reference)
88
89
  for seq in sequences
89
90
  ]
90
91
  if per_sequence:
@@ -95,7 +96,9 @@ def ngrams(
95
96
  return pooled
96
97
 
97
98
 
98
- def _count(sequence, sizes, level, collapse, relative_to, key, dialect) -> Counter:
99
+ def _count(
100
+ sequence, sizes, level, collapse, relative_to, key, dialect, minor_reference
101
+ ) -> Counter:
99
102
  items = list(sequence)
100
103
  runs, _ = encode(items, level=level, collapse=collapse, dialect=dialect)
101
104
  parts = [token_parts(char) for char, _, _ in runs]
@@ -107,15 +110,21 @@ def _count(sequence, sizes, level, collapse, relative_to, key, dialect) -> Count
107
110
  window = parts[start : start + size]
108
111
  if None in window:
109
112
  continue
110
- counter[_name_window(window, relative_to, key, level)] += 1
113
+ counter[_name_window(window, relative_to, key, level, minor_reference)] += 1
111
114
  return counter
112
115
 
113
116
 
114
- def _name_window(window, relative_to, key, level) -> tuple[str, ...]:
117
+ def _name_window(window, relative_to, key, level, minor_reference) -> tuple[str, ...]:
115
118
  if relative_to is None:
116
119
  return tuple(_chord_name(p) for p in window)
117
120
  tonic = window[0][0] if relative_to == "first" else key.tonic
121
+ minor = relative_to == "key" and key.minor
118
122
  return tuple(
119
- roman_from_parts(((root - tonic) % 12, triad, seventh), level=level)
123
+ roman_from_parts(
124
+ ((root - tonic) % 12, triad, seventh),
125
+ level=level,
126
+ minor=minor,
127
+ minor_reference=minor_reference,
128
+ )
120
129
  for root, triad, seventh in window
121
130
  )
@@ -1,8 +1,16 @@
1
1
  """Roman numerals: chords relative to a key, both ways.
2
2
 
3
- One chromatic scheme, measured from the key's tonic whether the key is major or minor
4
- (so ``bIII``, ``bVI``, ``bVII`` in a minor key, and ``to_roman`` and ``parse_roman`` round-trip):
5
- ``I bII II bIII III IV #IV V bVI VI bVII VII``. Upper case is major, lower case minor,
3
+ Degrees are semitones above the key's tonic. Two ways to name them in a minor key, chosen by
4
+ ``minor_reference`` (``to_roman`` and ``parse_roman`` round-trip under either):
5
+
6
+ - ``"parallel"`` (the default, the pop/jazz convention): one chromatic scheme read on the MAJOR
7
+ scale in every key, so a minor key reads ``i bIII iv bVI bVII``:
8
+ ``I bII II bIII III IV #IV V bVI VI bVII VII``.
9
+ - ``"relative"``: a minor key is read on its own natural-minor scale, so it reads
10
+ ``i III iv VI VII`` (the classical convention; what a learner who thinks "relative major"
11
+ expects): ``I bII II III #III IV #IV V VI #VI VII #VII``. A major key is unaffected.
12
+
13
+ Upper case is major, lower case minor,
6
14
  ``°`` diminished, ``ø`` half-diminished (``ø7``), ``+`` augmented, ``sus`` suspended or
7
15
  power; the seventh follows as ``7``, ``maj7`` or (after ``°``) ``°7``.
8
16
 
@@ -13,6 +21,12 @@ power; the seventh follows as ``7``, ``maj7`` or (after ``°``) ``°7``.
13
21
  ['vi', 'IV', 'I', 'V']
14
22
  >>> parse_roman("V7"), parse_roman("bVII"), parse_roman("Am")
15
23
  ((7, 0, 1), (10, 0, 0), None)
24
+ >>> [to_roman(c, Key(9, minor=True)) for c in ("Am", "F", "C", "G")]
25
+ ['i', 'bVI', 'bIII', 'bVII']
26
+ >>> [to_roman(c, Key(9, minor=True), minor_reference="relative") for c in ("Am", "F", "C", "G")]
27
+ ['i', 'VI', 'III', 'VII']
28
+ >>> parse_roman("VI", minor=True, minor_reference="relative")
29
+ (8, 0, 0)
16
30
  """
17
31
 
18
32
  from __future__ import annotations
@@ -45,7 +59,36 @@ NUMERALS = (
45
59
  "bVII",
46
60
  "VII",
47
61
  )
62
+ MINOR_REFERENCES = ("parallel", "relative")
63
+ DEFAULT_MINOR_REFERENCE = "parallel"
64
+ #: Names of the twelve degrees in a minor key read on its natural-minor scale (``minor_reference="relative"``).
65
+ NUMERALS_RELATIVE = (
66
+ "I",
67
+ "bII",
68
+ "II",
69
+ "III",
70
+ "#III",
71
+ "IV",
72
+ "#IV",
73
+ "V",
74
+ "VI",
75
+ "#VI",
76
+ "VII",
77
+ "#VII",
78
+ )
48
79
  _DEGREE = {"I": 0, "II": 2, "III": 4, "IV": 5, "V": 7, "VI": 9, "VII": 11}
80
+ _DEGREE_RELATIVE = {"I": 0, "II": 2, "III": 3, "IV": 5, "V": 7, "VI": 8, "VII": 10}
81
+
82
+
83
+ def check_minor_reference(minor_reference: str) -> str:
84
+ """``minor_reference`` if it is one of :data:`MINOR_REFERENCES`; a ``ValueError`` naming them otherwise."""
85
+ if minor_reference not in MINOR_REFERENCES:
86
+ raise ValueError(
87
+ f"minor_reference must be one of {MINOR_REFERENCES}, not {minor_reference!r}"
88
+ )
89
+ return minor_reference
90
+
91
+
49
92
  _ROMAN_RE = re.compile(
50
93
  r"(?P<acc>[b#]?)(?P<num>VII|VI|IV|V|III|II|I|vii|vi|iv|v|iii|ii|i)"
51
94
  r"(?P<q>°|o|ø|\+|sus)?(?P<sev>maj7|M7|7)?"
@@ -57,17 +100,23 @@ def is_roman(token: str) -> bool:
57
100
  return _ROMAN_RE.fullmatch(token) is not None
58
101
 
59
102
 
60
- def parse_roman(token: str) -> Parts | None:
103
+ def parse_roman(
104
+ token: str, *, minor: bool = False, minor_reference: str = DEFAULT_MINOR_REFERENCE
105
+ ) -> Parts | None:
61
106
  """``(degree, triad index, seventh index)`` of a Roman numeral; None if it is not one.
62
107
 
63
108
  The degree is semitones above the tonic. Case gives major or minor; ``°7`` is a
64
- diminished seventh chord, ``ø`` and ``ø7`` a half-diminished one.
109
+ diminished seventh chord, ``ø`` and ``ø7`` a half-diminished one. With ``minor=True`` and
110
+ ``minor_reference="relative"`` the numeral is read on the natural-minor scale (``VI`` is
111
+ 8 semitones up); otherwise on the major scale.
65
112
  """
113
+ check_minor_reference(minor_reference)
66
114
  m = _ROMAN_RE.fullmatch(token)
67
115
  if m is None:
68
116
  return None
69
117
  num = m["num"]
70
- degree = (_DEGREE[num.upper()] + {"b": -1, "#": 1, "": 0}[m["acc"]]) % 12
118
+ steps = _DEGREE_RELATIVE if (minor and minor_reference == "relative") else _DEGREE
119
+ degree = (steps[num.upper()] + {"b": -1, "#": 1, "": 0}[m["acc"]]) % 12
71
120
  q, sev = m["q"], m["sev"]
72
121
  if q == "sus":
73
122
  triad = TRIADS.index("open")
@@ -86,15 +135,26 @@ def parse_roman(token: str) -> Parts | None:
86
135
  return degree, triad, seventh
87
136
 
88
137
 
89
- def roman_from_parts(parts: Parts, *, level: str = DEFAULT_LEVEL) -> str:
90
- """The Roman numeral for ``(degree, triad, seventh)`` (degree = semitones above the tonic)."""
138
+ def roman_from_parts(
139
+ parts: Parts,
140
+ *,
141
+ level: str = DEFAULT_LEVEL,
142
+ minor: bool = False,
143
+ minor_reference: str = DEFAULT_MINOR_REFERENCE,
144
+ ) -> str:
145
+ """The Roman numeral for ``(degree, triad, seventh)`` (degree = semitones above the tonic).
146
+
147
+ ``minor`` says the key is minor; it matters only with ``minor_reference="relative"``.
148
+ """
91
149
  check_level(level)
150
+ check_minor_reference(minor_reference)
92
151
  degree, triad, seventh = parts
93
152
  if level == "root":
94
153
  triad = seventh = 0
95
154
  elif level == "triads":
96
155
  seventh = 0
97
- name, kind, sev = NUMERALS[degree % 12], TRIADS[triad], SEVENTHS[seventh]
156
+ table = NUMERALS_RELATIVE if (minor and minor_reference == "relative") else NUMERALS
157
+ name, kind, sev = table[degree % 12], TRIADS[triad], SEVENTHS[seventh]
98
158
  if kind == "minor":
99
159
  name = name.lower()
100
160
  if kind == "diminished":
@@ -115,13 +175,22 @@ def to_roman(
115
175
  *,
116
176
  level: str = DEFAULT_LEVEL,
117
177
  dialect: str | Dialect = "default",
178
+ minor_reference: str = DEFAULT_MINOR_REFERENCE,
118
179
  ) -> str | None:
119
- """The Roman numeral of ``symbol`` in ``key``; None if ``symbol`` is not a chord."""
180
+ """The Roman numeral of ``symbol`` in ``key``; None if ``symbol`` is not a chord.
181
+
182
+ ``minor_reference`` (see the module docstring) only changes what a *minor* ``key`` reads.
183
+ """
120
184
  parts = chord_parts(symbol, dialect=dialect)
121
185
  if parts is None:
122
186
  return None
123
187
  root, triad, seventh = parts
124
- return roman_from_parts(((root - key.tonic) % 12, triad, seventh), level=level)
188
+ return roman_from_parts(
189
+ ((root - key.tonic) % 12, triad, seventh),
190
+ level=level,
191
+ minor=key.minor,
192
+ minor_reference=minor_reference,
193
+ )
125
194
 
126
195
 
127
196
  def to_roman_sequence(
@@ -130,6 +199,7 @@ def to_roman_sequence(
130
199
  key: Key | None = None,
131
200
  level: str = DEFAULT_LEVEL,
132
201
  dialect: str | Dialect = "default",
202
+ minor_reference: str = DEFAULT_MINOR_REFERENCE,
133
203
  ) -> list[str | None]:
134
204
  """Roman numerals of a chord sequence in ``key`` (estimated from the sequence when None)."""
135
205
  symbols = list(symbols)
@@ -137,4 +207,7 @@ def to_roman_sequence(
137
207
  key = estimate_key(symbols, dialect=dialect)
138
208
  if key is None:
139
209
  return [None] * len(symbols)
140
- return [to_roman(s, key, level=level, dialect=dialect) for s in symbols]
210
+ return [
211
+ to_roman(s, key, level=level, dialect=dialect, minor_reference=minor_reference)
212
+ for s in symbols
213
+ ]
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "chordseq"
7
- version = "0.0.2"
7
+ version = "0.0.3"
8
8
  description = "Chord-progression search in any key, n-gram counts and Roman numerals over chord-symbol sequences"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -100,3 +100,57 @@ def test_ngram_sizes_and_pooling():
100
100
  def test_to_roman_sequence():
101
101
  assert to_roman_sequence(["G", "D", "Em", "C", "G"]) == ["I", "V", "vi", "IV", "I"]
102
102
  assert to_roman_sequence(["Em", "N", "G"], key=Key(7)) == ["vi", None, "I"]
103
+
104
+
105
+ def test_minor_reference_relative_reads_a_minor_key_on_its_own_scale():
106
+ am = Key(9, minor=True)
107
+ chords = ["Am", "Dm", "Em", "F", "C", "G", "E", "Bdim"]
108
+ assert to_roman_sequence(chords, key=am) == [
109
+ "i",
110
+ "iv",
111
+ "v",
112
+ "bVI",
113
+ "bIII",
114
+ "bVII",
115
+ "V",
116
+ "ii°",
117
+ ]
118
+ assert to_roman_sequence(chords, key=am, minor_reference="relative") == [
119
+ "i",
120
+ "iv",
121
+ "v",
122
+ "VI",
123
+ "III",
124
+ "VII",
125
+ "V",
126
+ "ii°",
127
+ ]
128
+ # a major key is unaffected, and so is the default
129
+ assert to_roman_sequence(
130
+ ["C", "Ab", "Bb"], key=Key(0), minor_reference="relative"
131
+ ) == ["I", "bVI", "bVII"]
132
+
133
+
134
+ def test_minor_reference_round_trips_and_validates():
135
+ import pytest
136
+ from chordseq.roman import NUMERALS_RELATIVE, parse_roman, roman_from_parts
137
+
138
+ for degree in range(12):
139
+ for triad in (0, 1):
140
+ tok = roman_from_parts(
141
+ (degree, triad, 0), minor=True, minor_reference="relative"
142
+ )
143
+ assert parse_roman(tok, minor=True, minor_reference="relative") == (
144
+ degree,
145
+ triad,
146
+ 0,
147
+ ), tok
148
+ assert len(set(NUMERALS_RELATIVE)) == 12
149
+ with pytest.raises(ValueError, match="minor_reference"):
150
+ to_roman_sequence(["Am"], key=Key(9, minor=True), minor_reference="nope")
151
+
152
+
153
+ def test_ngrams_minor_reference():
154
+ seq = ["Am", "F", "C", "G"] * 3
155
+ c = ngrams([seq], 2, key=Key(9, minor=True), minor_reference="relative")
156
+ assert ("i", "VI") in c and ("VI", "III") in c
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes