Commit a92b0fe3 authored by gijs's avatar gijs

Backend annotator

parent 5b7d8086
from flask import Flask, jsonify, request
import random
from classification_utils import db
from classification_utils.settings import CLASSES
baseurl = '/the-annotator/api'
app = Flask(__name__)
# def pick_sentence ():
# return [1, '''Logée modestement dans une aile du Capitole, où il fut possible
# d'introduire péniblement 350,000 volumes environ, elle envahit les
# cryptes du vaste édifice législatif, mais pour y enterrer, sans pouvoir
# les en extraire utilement, plusieurs centaines de mille documents de
# toute nature.''']
CLASSES = [
"0 - Science and Knowledge. Organization. Computer Science. Information Science. Documentation. Librarianship. Institutions. Publications",
"1 - Philosophy. Psychology",
"2 - Religion. Theology",
"3 - Social Sciences",
"4 - vacant",
"5 - Mathematics. Natural Sciences",
"6 - Applied Sciences. Medicine, Technology",
"7 - The Arts. Entertainment. Sport",
"8 - Linguistics. Literature",
"9 - Geography. History"
]
DATA = []
def load_data():
pass
def store_data():
pass
def classify (sentence, sentence_class):
DATA[sentence][2] = sentence_class
def pick_sentence ():
return DATA[random.randint(0,len(DATA))][0:2]
@app.route("{}/sentence".format(baseurl), methods=['GET'])
def get_sentence ():
id, sentence = pick_sentence()
conn = db.connect()
row = db.get_sentence_to_annotate(conn)
sentence_id = row['rowid']
document = row['document']
sentence = row['sentence']
line = row['line']
return jsonify({
'id': id,
'sentence': sentence
'after': [row['sentence'] for row in db.get_next_lines(conn, document, line, 2)],
'sentence': sentence,
'before': [row['sentence'] for row in db.get_prev_lines(conn, document, line, 2)][::-1],
'sentence_id': sentence_id,
})
@app.route("{}/classify".format(baseurl), methods=['POST'])
def post_sentence ():
sentence_id = int(request.form.id)
classify(sentence_id, min(len(CLASSES), max(0, request.form.sentenceClass)))
store_data()
def classify_sentence ():
conn = db.connect()
sentence_id = int(request.form['sentence_id'])
classification = min(len(CLASSES), max(0, int(request.form['classification'])))
db.classify_sentence(conn, sentence_id, classification)
return jsonify("DONE")
@app.route("{}/classes".format(baseurl), methods=['GET'])
......@@ -61,7 +37,10 @@ def get_classes ():
@app.route("{}/mark_dirty".format(baseurl), methods=['POST'])
def mark_dirty ():
return jsonify()
conn = db.connect()
sentence_id = int(request.form['sentence_id'])
db.mark_sentence_dirty(conn, sentence_id)
return jsonify("DONE")
@app.after_request
def add_headers(response):
......
../../classification_utils
\ No newline at end of file
export FLASK_APP=app.py
export FLASK_ENV=development
flask run
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment