Spaces:
Sleeping
Sleeping
File size: 4,918 Bytes
495399f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | from typing import List, Dict, Any
def parse_bps_json(data: Dict[str, Any]) -> List[str]:
"""
Memparse JSON dari API BPS menjadi daftar kalimat deskriptif.
Format Key DataContent:
[Kode Wilayah (4)] + [Kode Var (3?)] + [Kode TurVar (3?)] + [Kode Tahun (3)] + [Kode TurTahun (1?)]
Panjang Key tampaknya dinamis tergantung panjang kode masing-masing komponen.
Namun, kita bisa melakukan matching berdasarkan kode yang tersedia di metadata.
"""
if data.get("status") != "OK" or not data.get("datacontent"):
return []
# 1. Buat Lookup Dictionaries untuk performa
# Format: {val: label}
vervar_map = {str(item["val"]): item["label"] for item in data.get("vervar", [])}
var_map = {str(item["val"]): item for item in data.get("var", [])} # Simpan full item utk unit/note
turvar_map = {str(item["val"]): item["label"] for item in data.get("turvar", [])}
tahun_map = {str(item["val"]): item["label"] for item in data.get("tahun", [])}
turtahun_map = {str(item["val"]): item["label"] for item in data.get("turtahun", [])}
sentences = []
# 2. Iterasi Data Content
for key, value in data["datacontent"].items():
if value is None: continue
# Logika Pemecahan Key (Parsing Key)
# Karena panjang kode bisa bervariasi, kita harus mencocokkan dari komponen yang diketahui.
# Urutan Key BPS biasanya: Wilayah + Var + TurVar + Tahun + TurTahun
# A. Identifikasi Wilayah (Vervar) - Biasanya 4 digit pertama (1200, 1201, dst)
# Tapi kita harus hati-hati, lebih aman iterasi map wilayah
found_vervar = None
remaining_key = key
# Strategi: Coba match prefix dengan kode wilayah yang ada
for code, label in vervar_map.items():
if remaining_key.startswith(code):
found_vervar = {"code": code, "label": label}
remaining_key = remaining_key[len(code):]
break
if not found_vervar: continue # Skip jika wilayah tidak dikenali
# B. Identifikasi Variabel (Var)
found_var = None
for code, item in var_map.items():
if remaining_key.startswith(code):
found_var = {"code": code, "label": item["label"], "unit": item.get("unit", "")}
remaining_key = remaining_key[len(code):]
break
if not found_var: continue
# C. Identifikasi Turunan Variabel (TurVar)
found_turvar = None
# Turvar bisa "0" (Tidak ada) atau kode lain
for code, label in turvar_map.items():
if remaining_key.startswith(code):
found_turvar = {"code": code, "label": label}
remaining_key = remaining_key[len(code):]
break
# D. Identifikasi Tahun
found_tahun = None
for code, label in tahun_map.items():
if remaining_key.startswith(code):
found_tahun = {"code": code, "label": label}
remaining_key = remaining_key[len(code):]
break
if not found_tahun: continue
# E. Identifikasi Turunan Tahun (TurTahun)
found_turtahun = None
for code, label in turtahun_map.items():
if remaining_key == code: # Sisa key harusnya sama dengan turtahun
found_turtahun = {"code": code, "label": label}
break
# 3. Konstruksi Kalimat
# Format: [Var Label] ([TurVar Label]) di [Wilayah] pada [Tahun] [TurTahun] adalah [Nilai] [Unit]
var_text = found_var["label"]
# Tambahkan Turunan Variabel jika ada dan bukan "Tidak ada" atau "0"
turvar_text = ""
if found_turvar and found_turvar["label"] not in ["Tidak ada", "0", "Total"]:
turvar_text = f" kategori {found_turvar['label']}"
wilayah_text = found_vervar["label"]
tahun_text = found_tahun["label"]
# Tambahkan Turunan Tahun jika relevan (misal: Semester 1)
turtahun_text = ""
if found_turtahun and found_turtahun["label"] not in ["Tahun", "0"]:
turtahun_text = f" {found_turtahun['label']}"
unit_text = found_var["unit"] if found_var["unit"] else ""
# Bersihkan nilai
clean_value = str(value).replace(".", ",") # Format Indonesia untuk display? Atau keep dot untuk math?
# Untuk LLM, dot (.) lebih universal sebagai desimal.
clean_value = str(value)
sentence = f"{var_text}{turvar_text} di {wilayah_text} pada tahun {tahun_text}{turtahun_text} adalah {clean_value} {unit_text}."
# Bersihkan spasi ganda
sentence = " ".join(sentence.split())
sentences.append(sentence)
return sentences
|