Spaces:
Sleeping
Sleeping
import pandas as pd | |
import json | |
from bs4 import BeautifulSoup | |
# === Ladda Excel-filen === | |
df = pd.read_excel("artiklar_rensad_sammanfattad.xlsx") | |
# Om du vill vara extra säker på att ignorera kolumnerna, kan du ta bort dem (om de existerar) | |
columns_to_drop = ["(Ändra inte) Kunskapsbasartikel", "(Ändra inte) Kontrollsumma för rad"] | |
df = df.drop(columns=[col for col in columns_to_drop if col in df.columns], errors='ignore') | |
# === Lista för JSONL-rader === | |
records = [] | |
for _, row in df.iterrows(): | |
# Extrahera nyckelfält som vi vet bidrar med relevant information | |
article_id = str(row.get("Offentligt artikelnummer", "")).strip() | |
title = str(row.get("Rubrik", "")).strip() | |
content = str(row.get("Innehåll", "")).strip() | |
description = row.get("Beskrivning", "") | |
keywords = str(row.get("Nyckelord", "")).strip() | |
# Rensa eventuell HTML från innehåll och beskrivning | |
content = BeautifulSoup(content, "html.parser").get_text(separator="\n").strip() | |
if pd.notna(description): | |
description = BeautifulSoup(str(description), "html.parser").get_text(separator="\n").strip() | |
else: | |
description = "" | |
# Slå ihop innehåll och beskrivning (om beskrivningen finns) | |
text_parts = [content] | |
if description: | |
text_parts.append(description) | |
full_text = "\n\n".join(text_parts).strip() | |
# Skapa record med full artikeltext och metadata | |
record = { | |
"text": full_text, | |
"metadata": { | |
"id": article_id, | |
"rubrik": title, | |
"nyckelord": keywords, | |
} | |
} | |
# Lägg till i listan om full_text inte är tom | |
if full_text: | |
records.append(record) | |
# === Spara som JSONL === | |
with open("knowledge_base.jsonl", "w", encoding="utf-8") as f: | |
for r in records: | |
f.write(json.dumps(r, ensure_ascii=False) + "\n") | |
print(f"✅ Klar! Sparade {len(records)} artiklar i knowledge_base.jsonl") | |