drime

French rhyme dictionary with web and CLI interface
git clone https://a3nm.net/git/drime/
Log | Files | Refs | README

commit 3a7bb73949663a28f75f5d5ba311c89ca04f37ec
parent 00ddac0224b135d0b1719eb6646bf791e4173d2f
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Fri, 30 Dec 2011 14:59:06 +0100

better disambig, and a few fixes

Diffstat:
query.py | 38+++++++++++++++++++++++---------------
templates/disambig.html | 4++--
2 files changed, 25 insertions(+), 17 deletions(-)

diff --git a/query.py b/query.py @@ -75,12 +75,24 @@ def decode_all(x): x[k] = x[k].decode('latin1') def get_key(x): - return (x['word'], x['phon'], + x['orig'] = decode_orig(x['orig']) + return (x['word'], x['phon'], render_orig(x), x['word'] + ' [' + to_xsampa(x['phon']) + ']') +def decode_orig(x): + bases = x.split(',') + for i in range(len(bases)): + bases[i] = bases[i].split('|') + return bases + +def render_orig(row): + return ', '.join( + [a[0] + (' ('+a[1]+')' if row['word'] != a[1] else '') + for a in row['orig']]) + def do_query(word, phon, minsyll, maxsyll, elide, gender, offset, size): cursor = run_query(''' - SELECT word, phon, word_end, phon_end, feminine + SELECT word, phon, word_end, phon_end, feminine, orig FROM words WHERE (word = ? OR ?) AND (phon = ? OR ?) ORDER BY freq DESC @@ -89,7 +101,8 @@ def do_query(word, phon, minsyll, maxsyll, elide, gender, offset, size): keys = [] for x in cursor: decode_all(x) - keys.append(get_key(x)) + key = get_key(x) + keys.append(key) if len(keys) > 1 or keys == []: return {'keys': keys}, 0 # require disambiguation or is empty word = x['word'] @@ -97,7 +110,6 @@ def do_query(word, phon, minsyll, maxsyll, elide, gender, offset, size): word_end = x['word_end'] phon_end = x['phon_end'] feminine = x['feminine'] - key = get_key(x) rest = ''' FROM words WHERE (phon_end = ? OR word_end = ?) @@ -141,19 +153,14 @@ def do_query(word, phon, minsyll, maxsyll, elide, gender, offset, size): seen = set() c = 0 for row in result: - bases = row['orig'].split(',') - for i in range(len(bases)): - bases[i] = bases[i].split('|') - row['orig'] = bases + row['orig'] = decode_orig(row['orig']) ok = False for i in range(len(row['orig'])): if row['orig'][i][1] not in seen: ok = True seen.add(row['orig'][i][1]) if ok: - row['orig'] = ', '.join( - [a[0] + (' ('+a[1]+')' if row['word'] != a[1] else '') - for a in row['orig']]) + row['orig'] = render_orig(row) row['phon'] = to_xsampa(row['phon']) result2.append(row) c += 1 @@ -183,16 +190,16 @@ if __name__ == '__main__': print ("Bad values passed as arguments.") usage() sys.exit(4) - except TypeError: - usage() - sys.exit(4) + #except TypeError: + #usage() + #sys.exit(4) if len(r['keys']) == 0: print ("No interpretation found for %s" % sys.argv[1]) sys.exit(1) if len(r['keys']) > 1: print ("Multiple interpretations found for %s:" % sys.argv[1]) for k in r['keys']: - print (" - %s" % k[2]) + print (" - %s" % k[-1] + ' -- ' + k[2]) print ("Please rerun with a more specific query") sys.exit(2) result = [["word", "phon", "pr", "wr", "freq", "orig"]] + [ @@ -205,4 +212,5 @@ if __name__ == '__main__': #print ("Displaying result for %s" % r['keys'][0][2]) for x in result: print '|'.join(x) + sys.exit(0) diff --git a/templates/disambig.html b/templates/disambig.html @@ -8,9 +8,9 @@ {% endif %} <ul id="disambig"> {% for k in keys %} -<li class="{{ loop.cycle('odd', 'even') }}"> +<li> <a href="?query={{ k[-1] | escape }}&nsyl={{ nsyl }}&gender={{ gender }}"> - {{ k[0] }} [{{ k[1] }}]{#, rhyming with + {{ k[0] }} [{{ k[1] }}] -- {{ k[2] }} {#, rhyming with {% for v in example[k][:5] %} {{ v.word }} {% endfor %}