Doctorak-RAG / src /data_loader.py
aymanelgamal's picture
Upload 36 files
8cbfc52 verified
Raw
History Blame Contribute Delete
4.38 kB
import re
from dataclasses import dataclass
from typing import List
import pandas as pd
import config
# ─────────────────────────────────────────────────────────────
# Data Model
# ─────────────────────────────────────────────────────────────
@dataclass
class QARecord:
id: int
question: str
answer: str
content: str
tokens: List[str]
# ─────────────────────────────────────────────────────────────
# Arabic Cleaning
# ─────────────────────────────────────────────────────────────
ARABIC_DIACRITICS = re.compile("""
Ω‘ | # Tashdid
َ | # Fatha
Ω‹ | # Tanwin Fath
ُ | # Damma
ٌ | # Tanwin Damm
ِ | # Kasra
ٍ | # Tanwin Kasr
Ω’ | # Sukun
Ω€
""", re.VERBOSE)
def normalize_arabic(text):
text = re.sub(
"[Ψ₯Ψ£Ψ’Ψ§]",
"Ψ§",
text,
)
text = re.sub(
"Ω‰",
"ي",
text,
)
text = re.sub(
"Ψ€",
"و",
text,
)
text = re.sub(
"Ψ¦",
"ي",
text,
)
text = re.sub(
"Ψ©",
"Ω‡",
text,
)
return text
def strip_diacritics(text):
return re.sub(
ARABIC_DIACRITICS,
"",
text,
)
# ─────────────────────────────────────────────────────────────
# Cleaning
# ─────────────────────────────────────────────────────────────
def clean_text(text):
text = str(text).strip()
text = strip_diacritics(text)
text = normalize_arabic(text)
text = re.sub(
r"\s+",
" ",
text,
)
return text
# ─────────────────────────────────────────────────────────────
# Tokenization
# ─────────────────────────────────────────────────────────────
def tokenize(text):
text = clean_text(text)
text = re.sub(
r"[^\w\s]",
" ",
text,
)
tokens = text.split()
return tokens
# ─────────────────────────────────────────────────────────────
# Load Excel
# ─────────────────────────────────────────────────────────────
def load_excel(
path,
question_col=None,
answer_col=None,
):
question_col = (
question_col
or
config.QUESTION_COL
)
answer_col = (
answer_col
or
config.ANSWER_COL
)
df = pd.read_excel(path)
records = []
for idx, row in df.iterrows():
q = clean_text(
row[question_col]
)
a = clean_text(
row[answer_col]
)
# IMPORTANT:
# index question + answer together
content = f"""
Ψ§Ω„Ψ³Ψ€Ψ§Ω„:
{q}
Ψ§Ω„Ψ¬ΩˆΨ§Ψ¨:
{a}
"""
tokens = tokenize(content)
records.append(
QARecord(
id=idx,
question=q,
answer=a,
content=content,
tokens=tokens,
)
)
return records