# -*- coding: utf8 -*-
from flask import Flask, request, jsonify
from nltk.tokenize.punkt import PunktWordTokenizer
from nltk.corpus import stopwords
from nltk.stem import SnowballStemmer
import json

app = Flask(__name__)
JSON_MIME = 'application/json; charset=utf8'

# ISO639-1
NLTKlanguages={'NL': "dutch",
               'FI': "finnish",
               'DE': "german",
               'IT': "italian",
               'PT': "portuguese",
               'ES': "spanish",
               'TR': "turkish",
               'DA': "danish",
               'EN': "english",
               'FR': "french",
               'HU': "hungarian",
               'NO': "norwegian",
               'RU': "russian",
               'SV': "swedish"}

def russian(sent):
    stem = SnowballStemmer('russian')
    stop = stopwords.words('russian')
    tx  = PunktWordTokenizer().tokenize(sent) 
    mx = map(stem.stem, tx)
    px = [x for x in mx if x not in stop]
    return px


@app.route("/")
def hello():
    return "Tokenize, stemming, removing stop words.\n"


@app.route("/ru", methods=['POST'])
def post_russian():
    js = russian(request.form['text'])
    return jsonify(text=js)


if __name__ == '__main__':
    app.config['JSON_AS_ASCII'] = False     # JSON in UTF-8
    app.config['DEBUG'] = True              # I love debug mode
    app.run(host = '127.0.0.1')

@@ -57,16 +57,23 @@
  jsonify
+, abort
 %0Afrom nl
@@ -269,16 +269,622 @@
 =utf8'%0A%0A
+def dutch(sent):%0A    pass%0A%0Adef finnish(sent):%0A    pass%0A%0Adef german(sent):%0A    pass%0A%0Adef italian(sent):%0A    pass%0A%0Adef portuguese(sent):%0A    pass%0A%0Adef spanish(sent):%0A    pass%0A%0Adef turkish(sent):%0A    pass%0A%0Adef danish(sent):%0A    pass%0A%0Adef english(sent):%0A    pass%0A%0Adef french(sent):%0A    pass%0A%0Adef hungarian(sent):%0A    pass%0A%0Adef norwegian(sent):%0A    pass%0A%0Adef russian(sent):%0A    stem = SnowballStemmer('russian')%0A    stop = stopwords.words('russian')%0A    tx  = PunktWordTokenizer().tokenize(sent) %0A    mx = map(stem.stem, tx)%0A    px = %5Bx for x in mx if x not in stop%5D%0A    return px%0A%0Adef swedish(sent):%0A    pass%0A%0A
 # ISO639
@@ -906,20 +906,18 @@
 s=%7B'
-NL
+nl
 ': 
-%22
 dutch
-%22
 ,%0A  
@@ -934,22 +934,20 @@
    '
-FI
+fi
 ': 
-%22
 finnish
-%22
 ,%0A  
@@ -964,21 +964,19 @@
    '
-DE
+de
 ': 
-%22
 german
-%22
 ,%0A  
@@ -993,22 +993,20 @@
    '
-IT
+it
 ': 
-%22
 italian
-%22
 ,%0A  
@@ -1023,14 +1023,13 @@
    '
-PT
+pt
 ': 
-%22
 port
@@ -1034,17 +1034,16 @@
 rtuguese
-%22
 ,%0A      
@@ -1056,22 +1056,20 @@
    '
-ES
+es
 ': 
-%22
 spanish
-%22
 ,%0A  
@@ -1086,22 +1086,20 @@
    '
-TR
+tr
 ': 
-%22
 turkish
-%22
 ,%0A  
@@ -1116,21 +1116,19 @@
    '
-DA
+da
 ': 
-%22
 danish
-%22
 ,%0A  
@@ -1145,22 +1145,20 @@
    '
-EN
+en
 ': 
-%22
 english
-%22
 ,%0A  
@@ -1175,21 +1175,19 @@
    '
-FR
+fr
 ': 
-%22
 french
-%22
 ,%0A  
@@ -1204,14 +1204,13 @@
    '
-HU
+hu
 ': 
-%22
 hung
@@ -1214,17 +1214,16 @@
 ungarian
-%22
 ,%0A      
@@ -1236,14 +1236,13 @@
    '
-NO
+no
 ': 
-%22
 norw
@@ -1246,17 +1246,16 @@
 orwegian
-%22
 ,%0A      
@@ -1268,22 +1268,20 @@
    '
-RU
+ru
 ': 
-%22
 russian
-%22
 ,%0A  
@@ -1298,253 +1298,23 @@
    '
-SV
+sv
 ': 
-%22
 swedish
-%22
 %7D%0A%0A
-def russian(sent):%0A    stem = SnowballStemmer('russian')%0A    stop = stopwords.words('russian')%0A    tx  = PunktWordTokenizer().tokenize(sent) %0A    mx = map(stem.stem, tx)%0A    px = %5Bx for x in mx if x not in stop%5D%0A    return px%0A%0A%0A
 @app
@@ -1409,18 +1409,22 @@
 route(%22/
-ru
+%3Clang%3E
 %22, metho
@@ -1449,35 +1449,141 @@
 ost_
-russian():%0A    js = russian
+lang(lang):%0A    lang = lang.lower()%0A    if lang not in NLTKlanguages:%0A        abort(404)%0A    func = NLTKlanguages%5Blang%5D%0A    js = func
 (req

Use functional pointer