Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 43 additions & 45 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,44 @@ def login():
return render_template(
'login.html', error=error, totalsents=len(SENTENCES))

def loadgrammar(username):
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loadgrammar() is factored out of dologin() because we now additionally call it in undoaccept()

_, lang = os.path.split(os.path.basename(app.config['GRAMMAR']))
app.logger.info('Loading grammar %r', lang)
if username in WORKERS and isinstance(WORKERS[username], ProcessPoolExecutor):
WORKERS[username].shutdown(wait=False)
pool = ProcessPoolExecutor(max_workers=1)
future = pool.submit(
worker.loadgrammar,
app.config['GRAMMAR'], app.config['LIMIT'])
future.result()
app.logger.info('Grammar %r loaded.', lang)
# train on annotated sentences
annotations = readannotations()
if annotations:
app.logger.info('training on %d previously annotated sentences',
len(annotations))
trees, sents = [], []
headrules = pool.submit(worker.getprop, 'headrules').result()
for block in annotations.values():
# HOTFIX for ROOT error
blocklns = block.splitlines()
for iln,blockln in enumerate(blocklns):
if '\tROOT\t' in blockln and '\t0\t' not in blockln:
blocklns[iln] = blocklns[iln].replace('\tROOT\t', '\tXXX-XXX\t')
block = '\n'.join(blocklns)

item = exporttree(block.splitlines())
canonicalize(item.tree)
if headrules:
applyheadrules(item.tree, headrules)
trees.append(item.tree)
sents.append(item.sent)
if False and app.config['DEBUG']:
future = NoFuture(worker.augment, trees, sents)
else:
future = pool.submit(worker.augment, trees, sents)
future.result()
WORKERS[username] = pool

@app.route('/annotate/dologin')
def dologin():
Expand All @@ -457,7 +495,7 @@ def generate(url):
'<!doctype html>'
'<title>redirect</title>'
'You were logged in successfully. ')
if username in WORKERS:
if username in WORKERS and isinstance(WORKERS[username], ProcessPoolExecutor):
try:
_ = WORKERS[username].submit(
worker.getprop, 'headrules').result()
Expand All @@ -467,50 +505,8 @@ def generate(url):
yield "<script>window.location.replace('%s');</script>" % url
return
yield 'Loading grammar; this will take a few seconds ...'
_, lang = os.path.split(os.path.basename(app.config['GRAMMAR']))
app.logger.info('Loading grammar %r', lang)
pool = ProcessPoolExecutor(max_workers=1)
if False and app.config['DEBUG']:
from discodop.treesearch import NoFuture
future = NoFuture(
worker.loadgrammar,
app.config['GRAMMAR'], app.config['LIMIT'])
else:
future = pool.submit(
worker.loadgrammar,
app.config['GRAMMAR'], app.config['LIMIT'])
future.result()
app.logger.info('Grammar %r loaded.', lang)
# train on annotated sentences
annotations = readannotations()
if annotations:
app.logger.info('training on %d previously annotated sentences',
len(annotations))
trees, sents = [], []
headrules = pool.submit(worker.getprop, 'headrules').result()
for block in annotations.values():
# HOTFIX for ROOT error
blocklns = block.splitlines()
for iln,blockln in enumerate(blocklns):
if '\tROOT\t' in blockln and '\t0\t' not in blockln:
blocklns[iln] = blocklns[iln].replace('\tROOT\t', '\tXXX-XXX\t')
block = '\n'.join(blocklns)

item = exporttree(block.splitlines())
canonicalize(item.tree)
if headrules:
applyheadrules(item.tree, headrules)
trees.append(item.tree)
sents.append(item.sent)
if False and app.config['DEBUG']:
future = NoFuture(worker.augment, trees, sents)
else:
future = pool.submit(worker.augment, trees, sents)
future.result()
WORKERS[username] = pool
loadgrammar(username)
yield "<script>window.location.replace('%s');</script>" % url
# return "<script>window.location.replace('%s');</script>" % url

nexturl = request.args.get('next')
if not is_safe_url(nexturl) or 'username' not in session:
return abort(400)
Expand Down Expand Up @@ -572,6 +568,8 @@ def undoaccept():
'DELETE FROM entries WHERE username = ? AND id = ?',
(username, sentid))
db.commit()
# reload the grammar
loadgrammar(username)
return jsonify({"success": True})

@app.route('/retokenize', methods=['POST'])
Expand Down Expand Up @@ -741,7 +739,7 @@ def edit():
msg = ''
if request.args.get('annotated') == '1': # there is a saved tree
msg = Markup('<font color=red>You have already annotated '
'this sentence.</font><button id="undo" onclick="undoAccept()">Delete tree from database</button>')
'this sentence.</font><button id="undo" onclick="undoAccept()">Revert (deletes annotation)</button>')
id = QUEUE[sentno - 1][3]
treestr, n = getannotation(username, id) # get tree from database
treeobj = ActivedopTree.from_str(treestr)
Expand Down
2 changes: 2 additions & 0 deletions templates/edittree.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
window.location.href = '/annotate/annotate/' + sentno;
}
function undoAccept() {
// Change text of button to "Reverting..." to indicate that the action is being processed
$('#undo').text('Reverting...');
var sentid = $('#sentid').val();
var sentno = $('#sentno').val();
$.ajax({
Expand Down