drime

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

commit 2d1e58f02209a7378e4d08aeeba3789e95f33f4f
parent 53952546b200994da8d4d9a6aa7bc8fac07768dc
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Fri, 30 Dec 2011 14:01:03 +0100

cleanup, fix cli interface

Diffstat:
README | 4+---
drime.py | 9++++-----
query.py | 22+++++++++++-----------
3 files changed, 16 insertions(+), 19 deletions(-)

diff --git a/README b/README @@ -1,5 +1,3 @@ -WARNING -- this code does *not* work yet! - drime - by Antoine Amarilli A French rhyme dictionary Licence: GPL version 3 @@ -62,4 +60,4 @@ config = { } EOF -== +== 4.2. diff --git a/drime.py b/drime.py @@ -40,12 +40,12 @@ def q(): d['title'] = 'drime' d['count'] = count d['displayed'] = min(d['pagesize'], count) - d['keys'] = sorted(r.keys()) - if len(r.keys()) == 0: + d['keys'] = r['keys'] + if len(r['keys']) == 0: if request.args.get('json'): return jsonify(d) return render_template('notfound.html', **d) - elif len(r.keys()) > 1: + elif len(r['keys']) > 1: #d['example'] = {} #for k in d['keys']: #d['example'][k] = r[k] @@ -53,8 +53,7 @@ def q(): return jsonify(d) return render_template('disambig.html', **d) else: - d['result'] = r[r.keys()[0]] - # TODO better + d['result'] = r['result'] if request.args.get('json'): return jsonify(d) return render_template('results.html', **d) diff --git a/query.py b/query.py @@ -4,6 +4,7 @@ import sys import operator from db_mysql import run_query from common import from_xsampa, to_xsampa +import simplejson as json PAGESIZE = 500 @@ -82,11 +83,12 @@ def do_query(word, phon, minsyll, maxsyll, elide, gender, offset, size): ORDER BY freq DESC ''', (word, word == None, phon, phon == None,)) result = {} + keys = [] for x in cursor: decode_all(x) - result[get_key(x)] = x - if len(result.keys()) > 1 or result == {}: - return result, 0 # require disambiguation or is empty + keys.append(get_key(x)) + if len(keys) > 1 or keys == []: + return {'keys': keys}, 0 # require disambiguation or is empty word = x['word'] phon = x['phon'] word_end = x['word_end'] @@ -107,9 +109,7 @@ def do_query(word, phon, minsyll, maxsyll, elide, gender, offset, size): query = ''' SELECT word, phon, freq, min_nsyl, max_nsyl, elidable, orig, feminine ''' + rest #+ limit - print (query) cursor = run_query(query, args) #+ (size, offset,)) - print ("DONE") result = [] for row in cursor: @@ -133,7 +133,6 @@ def do_query(word, phon, minsyll, maxsyll, elide, gender, offset, size): ) result.append(row) - print ("DONE2") result.sort(key=operator.itemgetter('key')) result2 = [] seen = set() @@ -155,19 +154,20 @@ def do_query(word, phon, minsyll, maxsyll, elide, gender, offset, size): row['phon'] = to_xsampa(row['phon']) result2.append(row) c += 1 - if c > PAGESIZE: + if c > size: break seen.add(row['word']) count = len(result) - result2 = result2[:PAGESIZE] + result2 = result2[:size] - print ("DONE3") #cursor = run_query(''' #SELECT count(t2.word) #''' + rest, args) #for x in cursor: #count = x[x.keys()[0]] - return {key: result2}, count + return {'keys': [key], 'result': result2}, count if __name__ == '__main__': - print(query(*sys.argv[1:])) + r, c = query(*sys.argv[1:]) + print(json.dumps(r, indent=4)) +