Skip to content

Commit

Permalink
fixed ug2tex for python3
Browse files Browse the repository at this point in the history
  • Loading branch information
jonashoechst committed Jul 22, 2024
1 parent 355b5ee commit 8c4d822
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions Tools/ug2tex.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,67 @@
#!/usr/bin/env python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys, re
import re
import sys

legit_chords = ["A","B","H","C","C#","D","D#","E","F","F#","G","G#"]
legit_chords += [chord+"m" for chord in legit_chords]
legit_chords += [chord+"7" for chord in legit_chords]
legit_chords += [chord+"sus2" for chord in legit_chords]
legit_chords += [chord+"sus4" for chord in legit_chords]
legit_chords = ["A", "B", "H", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"]
legit_chords += [chord + "m" for chord in legit_chords]
legit_chords += [chord + "7" for chord in legit_chords]
legit_chords += [chord + "sus2" for chord in legit_chords]
legit_chords += [chord + "sus4" for chord in legit_chords]
legit_chords += ["Cadd9"]


def isChordLine(line):
# criteria: more than half of the words are legit chords
chord_count = sum([c in legit_chords for c in line.split()])
# sys.stderr.write("{} / {} chords are legit: {}\n".format(chord_count, len(line.split()), line))
if chord_count > 0 and float(chord_count) / len(line.split()) > 0.5:
if chord_count > 0 and float(chord_count) / len(line.split()) > 0.5:
return True

return False



def getChordLocations(line):
chords = []
for m in re.finditer(r'\S+', line):
for m in re.finditer(r"\S+", line):
index, chord = m.start(), m.group()
chords.append((index, chord))
return chords


def mergeChordsAndLine(chords, line):
positions = [0] + [chord[0] for chord in chords] + [len(line)]
inserts = ["\["+chord[1]+"]" for chord in chords]
inserts = ["\\[" + chord[1] + "]" for chord in chords]
parts = [line[i:j] for i, j in zip(positions[:-1], positions[1:])]
merged = [item for pair in zip(parts, inserts) for item in pair] + [parts[-1]]

return "".join(merged)


def cleanupLine(line):
return line.replace("´", "'")


if __name__ == "__main__":

sys.stdout.write('''%!TEX root = ../Single-Song.tex
sys.stdout.write("""%!TEX root = ../Single-Song.tex
\\beginsong{Song}[]
\\beginverse
''')
""")

for line in sys.stdin:
line = cleanupLine(line)
if isChordLine(line):
chords = getChordLocations(line)
lyrics = cleanupLine(sys.stdin.next())
lyrics = cleanupLine(next(sys.stdin))

chordline = mergeChordsAndLine(chords, lyrics)
sys.stdout.write(chordline)
else:
sys.stdout.write(line)
pass
sys.stdout.write('''

sys.stdout.write("""
\\endverse
\\endsong
Expand All @@ -66,4 +70,4 @@ def cleanupLine(line):
\\begin{intersong}
\\end{intersong}''')
\\end{intersong}""")

0 comments on commit 8c4d822

Please sign in to comment.