File size: 1,938 Bytes
17bc83e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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")