Commit 1817b74f authored by gijs's avatar gijs

Start of poem generation

parent 67f4ca96
......@@ -139,6 +139,31 @@ def get_sentence_to_annotate(conn):
error('Could not select row {}'.format(e))
return False
def get_poem_sentence(conn):
cur = conn.cursor()
try:
cur.execute(
"SELECT document, line, sentence FROM sentences WHERE clean = :clean ORDER BY RANDOM() LIMIT 1",
{ 'clean': False })
return cur.fetchone()
except Exception as e:
conn.rollback()
error('Could not select row {}'.format(e))
return False
def get_sentence_at_line(conn, line):
cur = conn.cursor()
try:
cur.execute(
"SELECT sentence, clean_annotation FROM sentences WHERE clean = :clean AND line = :line ORDER BY RANDOM() LIMIT 1",
{ 'clean': False, 'line': line })
return cur.fetchone()
except Exception as e:
conn.rollback()
error('Could not select row {}'.format(e))
return False
def get_prev_lines (conn, document, line, limit = 5):
cur = conn.cursor()
try:
......
from flask import Flask
from flask import Flask, jsonify, request
from classification_utils import db
from classification_utils import db, clean_states
import json
import random
baseurl = '/the-cleaner/api'
......@@ -56,9 +57,29 @@ def update_document():
else:
return jsonify("Error")
def get_start_line (conn):
sentence = db.get_poem_sentence(conn)
if sentence['sentence']:
return sentence
else:
return get_start_line(conn)
@app.route("{}/get_poem".format(baseurl), methods=["GET"])
def make_poem():
return jsonify("text")
def get_poem():
conn = db.connect()
sentences = []
target = random.randint(2,4) * 3
start = get_start_line(conn)
while len(sentences) < target:
sentence = db.get_sentence_at_line(conn, start['line'])
if sentence['clean_annotation'] != clean_states.EMPTY:
sentences.append(sentence['sentence'])
return jsonify({ 'text': '\n'.join(sentences) })
@app.after_request
def add_headers(response):
......
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