frhyme

guess the last phonemes of a French word
git clone https://a3nm.net/git/frhyme/
Log | Files | Refs | README

commit a385809da4461daa2c93d244eb106391a4ed2c03
parent 8ce52592c167d1e070ae3d91a67bf282a2443cb4
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Sun, 10 Jul 2011 22:42:12 -0400

continue

Diffstat:
additions | 2++
pron.py | 29+++++++++++++++++++++--------
rhyme.py | 38+++++++++++++++++++++++---------------
3 files changed, 46 insertions(+), 23 deletions(-)

diff --git a/additions b/additions @@ -18,3 +18,5 @@ dis-je iZ employ @plwa amusemens amyzm@ parens paR@ +peur p9R +vapeur vap9R diff --git a/pron.py b/pron.py @@ -1,4 +1,4 @@ -#!/usr/bin/python3 +#!/usr/bin/python3 -O """Determine if a French word starts by an aspirated 'h' or not, by a lookup in a precompiled trie""" @@ -14,13 +14,7 @@ trie = json.load(f) f.close() def to_list(d, rev=True): - l = [] - for a in d.keys(): - if rev: - l.append((d[a], a[::-1])) - else: - l.append((d[a], a)) - return l + return [(d[a], a[::-1] if rev else a) for a in d.keys()] def trie2list(trie): v, c = trie @@ -36,7 +30,24 @@ def trie2list(trie): d[x[1]] += x[0] return to_list(d, False) +def add_dict(a, b): + return dict( [ (n, a.get(n, 0)+b.get(n, 0)) for n in set(a)|set(b) ] ) + +#def trie2list(trie): +# l = [trie] +# d = {} +# while len(l) > 0: +# print(l[0]) +# v, c = l.pop() +# if c == {}: +# d = add_dict(dict(to_list(v)), d) +# else: +# for child in c.values(): +# l.append(c) +# return d + def do_lookup(trie, key): + #print(key) if len(key) == 0 or key[0] not in trie[1].keys(): return trie2list(trie) return do_lookup(trie[1][key[0]], key[1:]) @@ -48,6 +59,8 @@ def nbest(l, t): def lookup(key): """Return pronunciations for key""" + if key.rstrip() == '': + raise ValueError # TODO this is debug return nbest(do_lookup(trie, key[::-1] + ' '), 5) if __name__ == '__main__': diff --git a/rhyme.py b/rhyme.py @@ -1,8 +1,5 @@ #!/usr/bin/python3 -u -"""Determine if a French word starts by an aspirated 'h' or not, by a -lookup in a precompiled trie""" - import os import re import sys @@ -20,7 +17,7 @@ def suffix(x, y): return bound def rhyme(x, y): - print ("test: %s %s" % (x, y)) + #print ("test: %s %s" % (x, y)) l = suffix(x, y) for c in x[-l:]: if c in vowel: @@ -37,8 +34,8 @@ def eye_rhyme(x, y): return suffix(x, y) def concat_couples(a, b): - pprint(a) - pprint(b) + #pprint(a) + #pprint(b) l = set() for x in a: for y in b: @@ -46,24 +43,35 @@ def concat_couples(a, b): return l def lookup(s): - #TODO split words s = s.split(' ')[-3:] + #pprint(s) sets = list(map((lambda a : set([x[1] for x in pron.lookup(escape(a))])), s)) - print("HERE") - pprint(sets) + #print("HERE") + #pprint(sets) return functools.reduce(concat_couples, sets, set([''])) def init_rhyme(line, constraint): + #print(line) return (lookup(line), line, constraint) +def mmax(a, b): + if a == -1 or b == -1: + return -1 + else: + return max(a, b) + def max_constraints(a, b): - return (max(a[0], b[0]), max(a[1], b[1]), max(a[2], b[2])) + # TODO get rid of that + return (mmax(a[0], b[0]), mmax(a[1], b[1]), mmax(a[2], b[2])) def check_rhyme(current, new): oldp, old, old_constraints = current new, new_constraints = new + #print("THIS") + #pprint(new_constraints) + #pprint(new) constraints = max_constraints(new_constraints, old_constraints) newp = lookup(new) newp_r, new_r = match(oldp, old, newp, new, constraints) @@ -77,18 +85,18 @@ def match(ap, a, bp, b, constraints): for y in bp: val = rhyme(x, y) #print ("%s = %s" % (x, y)) - if val >= normalc: + if val >= normalc and normalc >= 0: rp.add(x[-val:]) val = assonance_rhyme(x, y) #print ("%s = %s" % (x, y)) - if val >= assonancec: + if val >= assonancec and assonancec >= 0: rp.add(x[-val:]) if a != None: val = eye_rhyme(a, b) - if val < eyec: - r = None + if val >= eyec and eyec >= 0: + r = a[0][-val:] else: - r = line[0][-val:] + r = None else: r = None return rp, r