Improve highlights
This commit is contained in:
parent
6dc6f4435d
commit
9cc8e71d1c
89
main.rb
89
main.rb
|
@ -171,22 +171,21 @@ public
|
|||
def examples(word_id)
|
||||
word_id = Integer word_id
|
||||
|
||||
@db_conn.exec_params(
|
||||
left_lang_id = 5 # Georgian
|
||||
right_lang_id = 2 # Russian
|
||||
|
||||
lang_order = left_lang_id < right_lang_id ? 'ASC' : 'DESC'
|
||||
|
||||
result = @db_conn.exec_params(
|
||||
(
|
||||
<<~SQL
|
||||
SELECT
|
||||
array_agg(foo.value ORDER BY foo.language_id DESC)::text[]
|
||||
AS values,
|
||||
array_agg(word_forms.len) AS lens,
|
||||
array_agg(word_form_example_texts.pos) AS positions
|
||||
foo.example_id,
|
||||
array_agg(foo.id ORDER BY foo.language_id #{lang_order})::text[]
|
||||
AS ids,
|
||||
array_agg(foo.value ORDER BY foo.language_id #{lang_order})::text[]
|
||||
AS values
|
||||
FROM example_texts foo
|
||||
LEFT JOIN word_form_example_texts
|
||||
ON word_form_example_texts.example_text_id = foo.id
|
||||
LEFT JOIN word_forms
|
||||
ON
|
||||
word_form_example_texts.word_form_id = word_forms.id
|
||||
AND
|
||||
word_forms.word_id = $1
|
||||
INNER JOIN (
|
||||
SELECT example_id, language_id, MIN(index) as min_index
|
||||
FROM example_texts
|
||||
|
@ -204,7 +203,7 @@ public
|
|||
GROUP BY example_texts.example_id
|
||||
)
|
||||
AND
|
||||
(language_id = 5 OR language_id = 2)
|
||||
(language_id = $2 OR language_id = $3)
|
||||
GROUP BY example_id, language_id
|
||||
) bar
|
||||
ON
|
||||
|
@ -214,13 +213,69 @@ public
|
|||
GROUP BY foo.example_id
|
||||
SQL
|
||||
),
|
||||
[word_id],
|
||||
[word_id, left_lang_id, right_lang_id],
|
||||
).map do |row|
|
||||
{
|
||||
values: row['values'].map(&:freeze).freeze,
|
||||
length: row['lens'].compact.first,
|
||||
positions: row['positions'].compact.freeze,
|
||||
example_id: Integer(row['example_id']),
|
||||
left_id: Integer(row['ids'].first),
|
||||
right_id: Integer(row['ids'].last),
|
||||
left: String(row['values'].first).strip.freeze,
|
||||
right: String(row['values'].last).strip.freeze,
|
||||
highlights: [],
|
||||
}.freeze
|
||||
end.freeze
|
||||
|
||||
highlights =
|
||||
@db_conn.exec_params(
|
||||
(
|
||||
<<~SQL
|
||||
SELECT
|
||||
example_texts.id AS example_text_id,
|
||||
array_agg(
|
||||
word_form_example_texts.pos
|
||||
ORDER BY word_form_example_texts.word_form_id
|
||||
) AS positions,
|
||||
array_agg(
|
||||
word_forms.len
|
||||
ORDER BY word_forms.id
|
||||
) AS lengths
|
||||
FROM example_texts
|
||||
INNER JOIN word_form_example_texts
|
||||
ON word_form_example_texts.example_text_id = example_texts.id
|
||||
INNER JOIN word_forms
|
||||
ON word_form_example_texts.word_form_id = word_forms.id
|
||||
WHERE
|
||||
language_id = $1::bigint
|
||||
AND
|
||||
example_texts.example_id = ANY($2::bigint[])
|
||||
GROUP BY example_texts.id
|
||||
SQL
|
||||
),
|
||||
[
|
||||
left_lang_id,
|
||||
PG::TextEncoder::Array.new.encode(result.map { |h| h[:example_id] }),
|
||||
],
|
||||
)
|
||||
.map do |row|
|
||||
example_text_id = Integer(row['example_text_id'])
|
||||
|
||||
positions = row['positions'].map { |pos| Integer pos }.freeze
|
||||
lengths = row['lengths'].map { |len| Integer len }.freeze
|
||||
|
||||
{
|
||||
example_text_id: example_text_id,
|
||||
data: positions.zip(lengths).map do |pos, len|
|
||||
{ pos: pos, len: len }.freeze
|
||||
end.sort_by { |h| h[:pos] }.freeze,
|
||||
}
|
||||
end.to_a
|
||||
|
||||
highlights.each do |highlight|
|
||||
result
|
||||
.find { |item| item[:left_id] == highlight[:example_text_id] }
|
||||
&.tap { |item| item[:highlights].replace highlight[:data] }
|
||||
end
|
||||
|
||||
result.each { |h| h[:highlights].freeze }
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue