Skip to content

Commit ca6d7b2

Browse files
authored
Merge pull request #378 from bmcfee/chord-validation-efficiency
More efficient chord validation
2 parents e299244 + b44cb8a commit ca6d7b2

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

mir_eval/chord.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,14 @@ def reduce_extended_quality(quality):
340340

341341

342342
# --- Chord Label Parsing ---
343+
# This monster regexp is pulled from the JAMS chord namespace,
344+
# which is in turn derived from the context-free grammar of
345+
# Harte et al., 2005.
346+
CHORD_RE = re.compile(
347+
r"""^((N|X)|(([A-G](b*|#*))((:(maj|min|dim|aug|1|5|sus2|sus4|maj6|min6|7|maj7|min7|dim7|hdim7|minmaj7|aug7|9|maj9|min9|11|maj11|min11|13|maj13|min13)(\((\*?((b*|#*)([1-9]|1[0-3]?))(,\*?((b*|#*)([1-9]|1[0-3]?)))*)\))?)|(:\((\*?((b*|#*)([1-9]|1[0-3]?))(,\*?((b*|#*)([1-9]|1[0-3]?)))*)\)))?((/((b*|#*)([1-9]|1[0-3]?)))?)?))$"""
348+
) # nopep8
349+
350+
343351
def validate_chord_label(chord_label):
344352
"""Test for well-formedness of a chord label.
345353
@@ -348,15 +356,7 @@ def validate_chord_label(chord_label):
348356
chord_label : str
349357
Chord label to validate.
350358
"""
351-
# This monster regexp is pulled from the JAMS chord namespace,
352-
# which is in turn derived from the context-free grammar of
353-
# Harte et al., 2005.
354-
355-
pattern = re.compile(
356-
r"""^((N|X)|(([A-G](b*|#*))((:(maj|min|dim|aug|1|5|sus2|sus4|maj6|min6|7|maj7|min7|dim7|hdim7|minmaj7|aug7|9|maj9|min9|11|maj11|min11|13|maj13|min13)(\((\*?((b*|#*)([1-9]|1[0-3]?))(,\*?((b*|#*)([1-9]|1[0-3]?)))*)\))?)|(:\((\*?((b*|#*)([1-9]|1[0-3]?))(,\*?((b*|#*)([1-9]|1[0-3]?)))*)\)))?((/((b*|#*)([1-9]|1[0-3]?)))?)?))$"""
357-
) # nopep8
358-
359-
if not pattern.match(chord_label):
359+
if not CHORD_RE.match(chord_label):
360360
raise InvalidChordException("Invalid chord label: " "{}".format(chord_label))
361361
pass
362362

0 commit comments

Comments
 (0)