def from_analyses(filenames, use_inversions=True):
  ttable = dict()
  for filename in filenames:
    analysis = mreader.from_file(filename)
    if analysis:
      for a, b in zip(analysis, analysis[1:]): 
        k1 = a.key
        rn1 = a.rn
        k2 = b.key
        rn2 = b.rn

        if not use_inversions:
          rn1 = pychord.just_numeral_with_secondary(rn1)
          rn2 = pychord.just_numeral_with_secondary(rn2)
        if k1 == k2:
          if not rn1 in ttable:
            ttable[rn1] = []
          ttable[rn1].append(rn2)
  table = dict()
  for s in ttable:
    temp = dict()
    for t in ttable[s]:
      if not t in temp:
        temp[t] = 0
      temp[t] += 1
    total = sum([temp[t] for t in temp])
    table[s] = dict()
    for t in ttable[s]:
      table[s][t] = float(temp[t]) / total
  return MarkovModel(table, use_inversions)
 def score(self, s, t):
   if not self.use_inversions:
     s = pychord.just_numeral_with_secondary(s)
     t = pychord.just_numeral_with_secondary(t)
   if s == t:
     return 1
   if not t in self.table[s]:
     return 0
   return self.table[s][t]
def fix_vii_to_V(labels):
  result = labels[:]
  N = len(result)
  for i in range(N):
    if i < N - 1:
      a = result[i]
      b = result[i + 1]
      rn1 = a.rn
      rn2 = b.rn
      srn1 = pychord.just_numeral_with_secondary(rn1)
      srn2 = pychord.just_numeral_with_secondary(rn2)
      if srn1 == 'V' and srn2 == 'vii':
        b.rn = rn1
      if srn2 == 'V' and srn1 == 'vii':
        a.rn = rn2
      result[i] = a
      result[i+1] = b
  return result
def fix_inversions(labels, es):
  result = labels[:]
  N = len(result)
  for i in range(N):
    if i < N - 1:
      a = result[i]
      b = result[i + 1]
      rn1 = a.rn
      rn2 = b.rn
      srn1 = pychord.just_numeral_with_secondary(rn1)
      srn2 = pychord.just_numeral_with_secondary(rn2)
      if srn1.lower() == srn2.lower() and not rn1 == rn2:
        anotes = es.notes_in_measure(a.measure, a.start, a.stop)
        bnotes = es.notes_in_measure(b.measure, b.start, b.stop)
        abass = min(anotes, key=lambda x: x.pitch.midi).pitch.midi
        bbass = min(bnotes, key=lambda x: x.pitch.midi).pitch.midi
        if abass + 6 < bbass or abass == bbass:
          b.rn = a.rn
        if bbass + 11 < abass and ('64' in rn1 or '6/4' in rn1):
          a.rn = b.rn
      result[i] = a
      result[i+1] = b
  return result
def fix_prefixes(labels):
  result = labels[:]
  N = len(result)
  for i in range(N):
    if i < N - 1:
      a = result[i]
      b = result[i + 1]
      rn1 = a.rn
      rn2 = b.rn
      srn2 = pychord.just_numeral_with_secondary(rn2)
      if rn1 == 'ii2' and rn2 == 'I':
        a.rn = 'I'
      if rn1 == 'ii7':
        a.rn = 'I6'
      result[i] = a
      result[i+1] = b
  return result
def fix_common_errors(labels, es):
  result = labels[:]
  N = len(result)
  for i in range(N):
    if i < N - 1:
      a = result[i]
      b = result[i + 1]
      rn1 = a.rn
      rn2 = b.rn
      srn2 = pychord.just_numeral_with_secondary(rn2)
      if rn2 in ['I', 'I6', 'i', 'i6']:
        notes = es.notes_in_measure(a.measure, a.start, a.stop)
        ps = set([n.pitch.pitchClass for n in notes])
        if (11 + a.key) % 12 in ps:
          if rn1 == 'ii':
            a.rn = 'viio6'
          if rn1 == 'iio65':
            a.rn == 'viio43'
      result[i] = a
      result[i+1] = b
  return result