2019-11-12 12:23:54 +00:00
|
|
|
from app import db, logger, socketio
|
2019-11-06 13:35:33 +00:00
|
|
|
from app.events import connected_sessions
|
2019-11-06 13:45:08 +00:00
|
|
|
from app.models import Corpus
|
2019-11-06 13:35:33 +00:00
|
|
|
from flask import current_app, request
|
2019-11-12 11:04:07 +00:00
|
|
|
from flask_login import current_user, login_required
|
2019-11-12 13:00:03 +00:00
|
|
|
from .CQiWrapper.CQiClient import CQiClient
|
|
|
|
from .CQiWrapper.CQi import CONST_FIELD_MATCH, CONST_FIELD_MATCHEND
|
2019-11-06 13:35:33 +00:00
|
|
|
|
|
|
|
|
2019-11-11 13:20:44 +00:00
|
|
|
'''
|
2019-11-12 11:04:07 +00:00
|
|
|
' A dictionary containing lists of, with corpus ids associated, Socket.IO
|
|
|
|
' session ids (sid). {<corpus_id>: [<sid>, ...], ...}
|
2019-11-11 13:20:44 +00:00
|
|
|
'''
|
2019-11-11 10:51:18 +00:00
|
|
|
analysis_sessions = {}
|
2019-11-11 13:20:44 +00:00
|
|
|
'''
|
|
|
|
' A dictionary containing Socket.IO session id - CQi client pairs.
|
2019-11-12 11:04:07 +00:00
|
|
|
' {<sid>: CQiClient, ...}
|
2019-11-11 13:20:44 +00:00
|
|
|
'''
|
2019-11-07 13:33:58 +00:00
|
|
|
analysis_clients = {}
|
2019-11-06 13:35:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
@socketio.on('init_corpus_analysis')
|
|
|
|
@login_required
|
|
|
|
def init_corpus_analysis(corpus_id):
|
2019-11-12 11:04:07 +00:00
|
|
|
corpus = Corpus.query.filter_by(id=corpus_id).first()
|
|
|
|
if corpus is None:
|
|
|
|
socketio.emit('init_corpus_analysis', '[ERROR 404]: Not Found',
|
|
|
|
room=request.sid)
|
|
|
|
if not (corpus.creator == current_user or current_user.is_administrator()):
|
|
|
|
socketio.emit('init_corpus_analysis', '[ERROR 403]: Forbidden',
|
|
|
|
room=request.sid)
|
2019-11-11 10:51:18 +00:00
|
|
|
if str(corpus_id) not in analysis_sessions:
|
|
|
|
analysis_sessions[str(corpus_id)] = [request.sid]
|
2019-11-06 13:45:08 +00:00
|
|
|
socketio.start_background_task(observe_corpus_analysis_connection,
|
2019-11-06 13:35:33 +00:00
|
|
|
current_app._get_current_object(),
|
2019-11-11 10:51:18 +00:00
|
|
|
corpus_id, request.sid)
|
2019-11-06 13:35:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
@socketio.on('query_event')
|
2019-11-12 11:04:07 +00:00
|
|
|
@login_required
|
2019-11-06 13:35:33 +00:00
|
|
|
def recv_query(message):
|
|
|
|
logger.warning(message)
|
2019-11-11 13:20:44 +00:00
|
|
|
analysis_client = analysis_clients[request.sid]
|
2019-11-12 13:00:03 +00:00
|
|
|
""" Prepare and execute a query """
|
|
|
|
corpus = 'CORPUS'
|
|
|
|
query = '"and";'
|
|
|
|
query_subcorpus = 'Results'
|
|
|
|
analysis_client.cqp_query(corpus, query_subcorpus, query)
|
|
|
|
""" Evaluate query results """
|
|
|
|
match_corpus = '{}:{}'.format(corpus, query_subcorpus)
|
|
|
|
match_num = analysis_client.cqp_subcorpus_size(match_corpus)
|
|
|
|
match_num = min(19, match_num)
|
|
|
|
if match_num == 0:
|
|
|
|
print('No matches found.')
|
|
|
|
exit()
|
|
|
|
if not analysis_client.cqp_subcorpus_has_field(match_corpus, CONST_FIELD_MATCH):
|
|
|
|
print('Error.')
|
|
|
|
exit()
|
|
|
|
if not analysis_client.cqp_subcorpus_has_field(match_corpus, CONST_FIELD_MATCHEND):
|
|
|
|
print('Error')
|
|
|
|
exit()
|
|
|
|
match_boundaries = zip(analysis_client.cqp_dump_subcorpus(match_corpus, CONST_FIELD_MATCH, 0, match_num - 1),
|
|
|
|
analysis_client.cqp_dump_subcorpus(match_corpus, CONST_FIELD_MATCHEND, 0, match_num - 1))
|
|
|
|
matches = []
|
|
|
|
for match_start, match_end in match_boundaries:
|
|
|
|
tokens = analysis_client.cl_cpos2str('{}.word'.format(corpus), range(match_start, match_end + 1))
|
|
|
|
pos = analysis_client.cl_cpos2str('{}.pos'.format(corpus), range(match_start, match_end + 1))
|
|
|
|
matches.append({'tokens': tokens, 'pos': pos})
|
|
|
|
logger.warning(matches)
|
2019-11-06 13:35:33 +00:00
|
|
|
|
|
|
|
|
2019-11-06 13:45:08 +00:00
|
|
|
def observe_corpus_analysis_connection(app, corpus_id, session_id):
|
2019-11-06 13:35:33 +00:00
|
|
|
with app.app_context():
|
2019-11-07 09:55:23 +00:00
|
|
|
corpus = Corpus.query.filter_by(id=corpus_id).first()
|
2019-11-07 14:28:07 +00:00
|
|
|
while corpus.status != 'analysing':
|
|
|
|
db.session.refresh(corpus)
|
|
|
|
socketio.sleep(3)
|
2019-11-12 13:00:03 +00:00
|
|
|
analysis_client = CQiClient(host='{}_analysis_container{}'.format(corpus.creator.username, corpus.id), port=4877)
|
|
|
|
analysis_client.ctrl_connect('opaque', 'opaque')
|
2019-11-11 13:20:44 +00:00
|
|
|
analysis_clients[session_id] = analysis_client
|
2019-11-11 14:57:55 +00:00
|
|
|
socketio.emit('init_corpus_analysis', 'Ready', room=session_id)
|
2019-11-06 13:35:33 +00:00
|
|
|
while session_id in connected_sessions:
|
2019-11-12 12:23:54 +00:00
|
|
|
'''
|
2019-11-11 13:20:44 +00:00
|
|
|
try:
|
|
|
|
analysis_client.ctrl_ping()
|
|
|
|
except Exception as err:
|
|
|
|
logger.warning('[Exception]: {}'.format(err))
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
socketio.sleep(3)
|
2019-11-12 12:23:54 +00:00
|
|
|
'''
|
|
|
|
socketio.sleep(3)
|
2019-11-12 13:00:03 +00:00
|
|
|
analysis_client.ctrl_bye()
|
2019-11-07 14:28:07 +00:00
|
|
|
analysis_clients.pop(session_id, None)
|
2019-11-11 10:51:18 +00:00
|
|
|
analysis_sessions[str(corpus_id)].remove(session_id)
|
|
|
|
if not analysis_sessions[str(corpus_id)]:
|
|
|
|
analysis_sessions.pop(str(corpus_id), None)
|
|
|
|
corpus.status = 'stop analysis'
|
2019-11-12 11:04:07 +00:00
|
|
|
db.session.commit()
|