From 4a9496c60f55ab1e294d8c10346f44762d0e54b6 Mon Sep 17 00:00:00 2001 From: HeNine <> Date: Fri, 22 Oct 2021 15:47:15 +0200 Subject: [PATCH] Add highlighting --- buscribe-api/buscribeapi/buscribeapi.py | 13 +++++++++---- buscribe-web/line.less | 4 ++++ buscribe_data.sql | 12 +++++++++++- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/buscribe-api/buscribeapi/buscribeapi.py b/buscribe-api/buscribeapi/buscribeapi.py index 14cd43a..5245287 100644 --- a/buscribe-api/buscribeapi/buscribeapi.py +++ b/buscribe-api/buscribeapi/buscribeapi.py @@ -95,16 +95,19 @@ def get_json(): "end_bus_time": round_bus_time(row.start_time - app.bustime_start), "verifier": row.verifier, "speakers": row.names, - "text": row.transcription_line if row.transcription_line is not None else ""} for row in results]) + "text": row.highlighted_text if row.highlighted_text is not None else ""} for row in results]) def fetch_lines(db_conn, start_time, end_time, ts_query=None, limit=None, offset=None): - query = "SELECT * FROM buscribe_all_transcriptions WHERE start_time > %(start_time)s AND end_time < %(end_time)s " + query = "SELECT *" + \ + ( + ",ts_headline(transcription_line, convert_query(%(text_query)s), 'StartSel='''', StopSel=') AS highlighted_text" if ts_query is not None else ",transcription_line AS highlighted_text") + \ + " FROM buscribe_all_transcriptions WHERE start_time > %(start_time)s AND end_time < %(end_time)s " if ts_query is not None: query += "AND (coalesce(transcription_line_ts, ''::tsvector) || coalesce(names_ts, ''::tsvector)) @@ " \ - "(CASE WHEN websearch_to_tsquery(%(text_query)s)::text != '' THEN websearch_to_tsquery(%(text_query)s)::text || ':*' ELSE '' END)::tsquery " \ - "ORDER BY ts_rank_cd(coalesce(transcription_line_ts, ''::tsvector) || coalesce(names_ts, ''::tsvector), (CASE WHEN websearch_to_tsquery(%(text_query)s)::text != '' THEN websearch_to_tsquery(%(text_query)s)::text || ':*' ELSE '' END)::tsquery) DESC, " \ + "convert_query(%(text_query)s) " \ + "ORDER BY ts_rank_cd(coalesce(transcription_line_ts, ''::tsvector) || coalesce(names_ts, ''::tsvector), convert_query(%(text_query)s)) DESC, " \ "start_time " else: query += "ORDER BY start_time " @@ -117,6 +120,8 @@ def fetch_lines(db_conn, start_time, end_time, ts_query=None, limit=None, offset query += ";" + print(query) + return database.query(db_conn, query, start_time=start_time if start_time is not None else '-infinity', end_time=end_time if end_time is not None else 'infinity', diff --git a/buscribe-web/line.less b/buscribe-web/line.less index e5f0c8b..c86646d 100644 --- a/buscribe-web/line.less +++ b/buscribe-web/line.less @@ -44,6 +44,10 @@ grid-column: text; //grid-row: span 2; + span.highlight { + font-weight: bold; + color: powderblue; + } } .line_speakers { diff --git a/buscribe_data.sql b/buscribe_data.sql index 497d273..b06607f 100644 --- a/buscribe_data.sql +++ b/buscribe_data.sql @@ -111,4 +111,14 @@ SELECT id, null AS names_ts FROM buscribe_transcriptions; -ROLLBACK; \ No newline at end of file +ROLLBACK; + +-- Convert last lexeme in a query to prefix query. +CREATE FUNCTION convert_query(query_text text) RETURNS tsquery AS +$$ +DECLARE + ws_query text := websearch_to_tsquery(query_text)::text; +BEGIN + RETURN (CASE WHEN ws_query != '' THEN ws_query || ':*' ELSE '' END)::tsquery; +END; +$$ LANGUAGE plpgsql; \ No newline at end of file