Source

match-o-matic / webservice.py

Full commit
import os
import sys

sys.path.append(os.path.dirname(__file__))
os.chdir(os.path.dirname(__file__))

import bottle

import worddict

def sortlist(wordsequence):
    return sorted(wordsequence, 
                  cmp=lambda x,y: cmp(len(x), 
                                      len(y)) or 
                                  cmp(x, y))

@bottle.route('/')
def index():
    bottle.response.content_type = 'text/plain'
    return ("=== API REFERENCE ===\n" +
            "\n".join(x['rule'] for x in
                      bottle.app().routes))

@bottle.route('/is_word/:word')
def is_word(word):
    return {'is_word': lookup.is_word(word)}

@bottle.route('/matches/:word')
def matches(word):
    return {'words': sortlist(lookup.matches(word))}

@bottle.route('/words_starting_with/:word')
def words_starting_with(word):
    return {'words': sortlist(lookup.words_with_prefix(word))}

@bottle.route('/words_containing/:word')
def words_containing(word):
    return {'words': sortlist(lookup.matches_for_bag(word))}

lookup = worddict.WordLookup(os.path.join(os.path.dirname(
                                                os.path.abspath(
                                                        __file__)),
                                          'words.txt'))

#bottle.run(host='localhost', port=8080, reloader=True)
#application = bottle.default_app()