Doctorak-RAG / src /utils.py
aymanelgamal's picture
Upload 36 files
8cbfc52 verified
Raw
History Blame Contribute Delete
3.45 kB
import re
# ─────────────────────────────────────────────────────────────
# Arabic Diacritics
# ─────────────────────────────────────────────────────────────
ARABIC_DIACRITICS = re.compile("""
Ω‘ | # Tashdid
َ | # Fatha
Ω‹ | # Tanwin Fath
ُ | # Damma
ٌ | # Tanwin Damm
ِ | # Kasra
ٍ | # Tanwin Kasr
Ω’ | # Sukun
Ω€
""", re.VERBOSE)
# ─────────────────────────────────────────────────────────────
# Remove Diacritics
# ─────────────────────────────────────────────────────────────
def strip_diacritics(text):
return re.sub(
ARABIC_DIACRITICS,
"",
text,
)
# ─────────────────────────────────────────────────────────────
# Normalize Arabic
# ─────────────────────────────────────────────────────────────
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
# ─────────────────────────────────────────────────────────────
# Clean Text
# ─────────────────────────────────────────────────────────────
def clean_text(text):
text = str(text)
text = text.strip()
text = strip_diacritics(text)
text = normalize_arabic(text)
text = re.sub(
r"\s+",
" ",
text,
)
return text
# ─────────────────────────────────────────────────────────────
# Sanitize Query
# ─────────────────────────────────────────────────────────────
def sanitize_query(query):
query = clean_text(query)
# remove strange symbols
query = re.sub(
r"[^\w\s؟?]",
" ",
query,
)
query = re.sub(
r"\s+",
" ",
query,
)
return query.strip()