Skip to content

Commit

Permalink
Merge pull request mir-evaluation#378 from bmcfee/chord-validation-ef…
Browse files Browse the repository at this point in the history
…ficiency

More efficient chord validation
  • Loading branch information
bmcfee authored Apr 1, 2024
2 parents e299244 + b44cb8a commit ca6d7b2
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions mir_eval/chord.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,14 @@ def reduce_extended_quality(quality):


# --- Chord Label Parsing ---
# This monster regexp is pulled from the JAMS chord namespace,
# which is in turn derived from the context-free grammar of
# Harte et al., 2005.
CHORD_RE = re.compile(
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]?)))?)?))$"""
) # nopep8


def validate_chord_label(chord_label):
"""Test for well-formedness of a chord label.
Expand All @@ -348,15 +356,7 @@ def validate_chord_label(chord_label):
chord_label : str
Chord label to validate.
"""
# This monster regexp is pulled from the JAMS chord namespace,
# which is in turn derived from the context-free grammar of
# Harte et al., 2005.

pattern = re.compile(
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]?)))?)?))$"""
) # nopep8

if not pattern.match(chord_label):
if not CHORD_RE.match(chord_label):
raise InvalidChordException("Invalid chord label: " "{}".format(chord_label))
pass

Expand Down

0 comments on commit ca6d7b2

Please sign in to comment.