From 2cab52a110f1aff12adb052a7fce5f5cb2e6a1d8 Mon Sep 17 00:00:00 2001 From: Debby Date: Thu, 26 Mar 2026 20:03:49 +0700 Subject: [PATCH] agg narative country list --- scripts/bigquery_aggregate_layer.py | 51 +++++++++++++++++++---------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/scripts/bigquery_aggregate_layer.py b/scripts/bigquery_aggregate_layer.py index 07b02c2..977a0d4 100644 --- a/scripts/bigquery_aggregate_layer.py +++ b/scripts/bigquery_aggregate_layer.py @@ -216,27 +216,42 @@ def _build_overview_narrative( f"no prior-year data is available for year-over-year comparison." ) - # -- Sentence 3: country ranking ---------------------------------------- + # -- Sentence 3: country ranking ---------------------------- sent3 = "" if ranking_list: - top3 = ranking_list[:3] - last = ranking_list[-1] - if len(top3) >= 3: - after_first = ( - f"{top3[1]['country_name']} ({_fmt_score(top3[1]['score'])}) and " - f"{top3[2]['country_name']} ({_fmt_score(top3[2]['score'])})." - ) - elif len(top3) == 2: - after_first = f"{top3[1]['country_name']} ({_fmt_score(top3[1]['score'])})." - else: - after_first = "." + first = ranking_list[0] + last = ranking_list[-1] + middle = ranking_list[1:-1] - sent3 = ( - f"In terms of country performance, {top3[0]['country_name']} led the region " - f"with a score of {_fmt_score(top3[0]['score'])}, followed by {after_first} " - f"At the other end, {last['country_name']} recorded the lowest score " - f"of {_fmt_score(last['score'])} in {year}." - ) + if len(ranking_list) == 1: + sent3 = ( + f"In terms of country performance, {first['country_name']} was the only " + f"country assessed, scoring {_fmt_score(first['score'])} in {year}." + ) + elif len(ranking_list) == 2: + sent3 = ( + f"In terms of country performance, {first['country_name']} led the region " + f"with a score of {_fmt_score(first['score'])}, while " + f"{last['country_name']} recorded the lowest score of " + f"{_fmt_score(last['score'])} in {year}." + ) + else: + # Susun semua negara di tengah: "B (xx.xx), C (xx.xx), ..., and Y (xx.xx)" + middle_parts = [ + f"{c['country_name']} ({_fmt_score(c['score'])})" + for c in middle + ] + if len(middle_parts) == 1: + middle_str = middle_parts[0] + else: + middle_str = ", ".join(middle_parts[:-1]) + f", and {middle_parts[-1]}" + + sent3 = ( + f"In terms of country performance, {first['country_name']} led the region " + f"with a score of {_fmt_score(first['score'])}, followed by {middle_str}. " + f"At the other end, {last['country_name']} recorded the lowest score " + f"of {_fmt_score(last['score'])} in {year}." + ) # -- Sentence 4: most improved / declined ------------------------------ sent4_parts = []