songleash

generate chansons en laisse
git clone https://a3nm.net/git/songleash/
Log | Files | Refs

commit 467322a8972cef28ab6790e2a3044269d7e3efb0
parent ff35f55cede857c68ec274d457c675f5b6b2dde6
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Thu, 27 Aug 2015 02:16:35 +0200

split cycle computing and printing

Diffstat:
cycle.py | 61++++++++++++-------------------------------------------------
printcycle.py | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 92 insertions(+), 49 deletions(-)

diff --git a/cycle.py b/cycle.py @@ -4,55 +4,10 @@ import sys from common import fem, vowels_script, strip_accents, semivowels, start from collections import defaultdict -g = defaultdict(lambda: defaultdict(lambda: set())) +g = defaultdict(lambda: set()) bestlen = 0 visited = set() -fcache = {} - -def print_word(w, prn): - s = "" - vowels = False - for i in range(len(w)): - p = -(i+1) - s += w[p] - x = strip_accents(w[p])[0] - if not vowels: - if x in vowels_script and (i > 0 or w[-1] != 'e') and (i > 1 or w[-1] != - 's' or w[-2] != 'e') and (i > 2 or (not w.endswith('gue')) - and - not (w.endswith('que'))) and (i > 3 or (not - w.endswith('gues') and (not w.endswith('ques')))): - vowels = True - else: - if x not in vowels_script and x != '-': - if prn[0] in semivowels: - s = s[:-1] - break - # exception - if w[p] == 'h': - s += w[-(i+2)] - break - s = s[::-1] - print ("## %s, %s, %s, %s, %s" % (w, w, w, s, s)) - -def print_list(l): - l = l + [l[1]] - print("------------------") - for i in range(len(l)-1): - w = list(g[l[i]][l[i+1]])[0] - #print(w[0]) - fs = w[1].split('-') - if len(w[1]) == 0 or len(fs) < 3: - print_word(w[0], l[i+1][1]) - else: - end = ''.join(fs[2:]) - print (" %s, %s, %s, %s, %s" - % (w[0], w[0], w[0], end, end)) - print("------------------") - print("") - -# TODO: do something more clever def dfs(l): global g @@ -62,19 +17,25 @@ def dfs(l): #print(l) if l[-1] == start and len(l) > 1: if len(l) > bestlen: - print_list(l) + #print_list(l) bestlen = len(l) + print(bestlen-1) + f = open(out, 'w') + for w in l: + print('%s %s' % ('f' if w[0] else 'm', w[1]), file=f) + f.close() return if l[-1] in visited: return v = l[-1] visited.add(v) - for t in g[v].keys(): + for t in g[v]: #if (len(l) % 2 != 0) in [fem(w) for w in g[v][t]]: #if fcache[v][t][len(l) % 2 == 0]: dfs(l+[t]) visited.remove(v) +out = sys.argv[1] for l in sys.stdin.readlines(): l = l.strip().split('\t') @@ -82,7 +43,9 @@ for l in sys.stdin.readlines(): l.append('') fr = (l[0] == 'f', l[1]) to = (l[2] == 'f', l[3]) - g[fr][to].add((l[4], l[5])) + #g[fr][to].add((l[4], l[5])) + g[fr].add(to) + #g[fr][to] = (l[4], l[5]) dfs([start]) diff --git a/printcycle.py b/printcycle.py @@ -0,0 +1,80 @@ +#!/usr/bin/python3 + +import sys +from common import fem, vowels_script, strip_accents, semivowels, start +from collections import defaultdict + +g = defaultdict(lambda: {}) + +visited = set() + +def print_word(w, prn): + s = "" + vowels = False + for i in range(len(w)): + p = -(i+1) + s += w[p] + x = strip_accents(w[p])[0] + if not vowels: + if x in vowels_script and (i > 0 or w[-1] != 'e') and (i > 1 or w[-1] != + 's' or w[-2] != 'e') and (i > 2 or (not w.endswith('gue')) + and + not (w.endswith('que'))) and (i > 3 or (not + w.endswith('gues') and (not w.endswith('ques')))): + vowels = True + else: + if x not in vowels_script and x != '-': + if prn[0] in semivowels: + s = s[:-1] + break + # exception + if w[p] == 'h': + s += w[-(i+2)] + break + s = s[::-1] + print ("## %s, %s, %s, %s, %s" % (w, w, w, s, s)) + +def print_list(l): + global out + if not start[0]: + l = l[1:] + [l[1], l[2]] + for i in range(len(l)-1): + #w = list(g[l[i]][l[i+1]])[0] + w = g[l[i]][l[i+1]] + #print(w[0]) + fs = w[1].split('-') + if len(w[1]) == 0 or len(fs) < 3: + print_word(w[0], l[i+1][1]) + else: + # remove spaces in syll for singing + for i in range(len(fs)): + fs[i] = ''.join(fs[i].split(' ')) + end = ''.join(fs[2:]) + spl = ' '.join(fs) + if w[0]: + spl2 = fs[0] + ' ' + fs[1] + ' ' + ''.join(fs[2:]) + else: + spl2 = spl + print (" %s, %s, %s, %s, %s" + % (spl, spl, spl2, end, end)) + +fname = sys.argv[1] +f = open(fname) + +for l in sys.stdin.readlines(): + l = l.strip().split('\t') + if len(l) < 6: + l.append('') + fr = (l[0] == 'f', l[1]) + to = (l[2] == 'f', l[3]) + #g[fr][to].add((l[4], l[5])) + g[fr][to] = (l[4], l[5]) + +li = [] +for l in f.readlines(): + ff = l.strip().split(' ') + li.append((ff[0] == 'f', ff[1])) + +f.close() +print_list(li) +