<commit_before>#!/usr/bin/env python
################################################################################
#    Created by <NAME>                                                 #
#    <EMAIL>                                                  #
################################################################################
from flask import Flask, request, jsonify
from TermSuggester import TermSuggester, SearchMethodAggregation
from elsearch import ELSearch
from wnsearch import WNSearch

app = Flask(__name__)

searchMethodClasses = (ELSearch, WNSearch)
initializeParameters = ((None, False),([]))
ts = TermSuggester(searchMethodClasses, initializeParameters)

@app.route("/suggester", methods = ['GET',])
def api_term():
    if request.method == 'GET':
        if 'term' in request.args:
            data = ts.getSuggestions(str(request.args['term']), SearchMethodAggregation.SumMethod)
            resp = jsonify(data)
            resp.status_code = 200
            return resp
        else:
            return 'Error: Need to specif a term!'




if __name__ == "__main__":
    app.run(debug=True)<commit_msg>Add error handling, possibility to specify methods and aggregation<commit_after>#!/usr/bin/env python
################################################################################
#    Created by <NAME>                                                 #
#    <EMAIL>                                                  #
################################################################################
from flask import Flask, Response, request, jsonify
from TermSuggester import TermSuggester, SearchMethodAggregation
from elsearch import ELSearch
from wnsearch import WNSearch
import MakeChart

app = Flask(__name__)

searchMethodClasses = (ELSearch, WNSearch)
initializeParameters = ((None, False),([]))
ts = TermSuggester(searchMethodClasses, initializeParameters)

@app.route('/')
def api_root():
    return 'Welcome to TermSuggester Web API'

@app.errorhandler(404)
def api_error(error=None):
    message = {
            'status': 404,
            'message': 'Error: ' + error,
    }
    resp = jsonify(message)
    resp.status_code = 404
    return resp

@app.route("/suggester", methods = ['GET',])
def api_term():
    if request.method == 'GET':
        if 'term' in request.args:
            if 'agg-method' in request.args:
                aggMethod = str(request.args['agg-method']).strip()
                if aggMethod == 'sum':
                    aggMethod = SearchMethodAggregation.SumMethod
                elif aggMethod == 'average':
                    aggMethod = SearchMethodAggregation.AverageMethod
                else:
                    return api_error('specify correct aggregation method: sum or average')
            else:
                # Default aggragation method
                aggMethod = SearchMethodAggregation.SumMethod
            
            if 'methods' in request.args:
                try:
                    methods = str(request.args['methods']).split(',')
                    for i in range(len(methods)):
                        methods[i] = int(methods[i])
                except:
                    return api_error('specify correct method. Example: 1,2')
            else:
                methods = None
            data = ts.getSuggestions(str(request.args['term']), aggMethod, methods)
            resp = Response(MakeChart.dict2bar(data), status=200, mimetype='application/json')
            return resp
        else:
            return api_error('a term is required')




if __name__ == "__main__":
    app.run(debug=True)
{'fraction_non_alphanumeric': 0.18070953436807094, 'fraction_numerical': 0.004988913525498891, 'mean_word_length': 2.317095588235294, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4797975', 'n_tokens_mistral': 925, 'n_tokens_neox': 855, 'n_words': 225}