empty repo for clean single-source upload
Browse files- phase_d_bilingual.py +0 -86
phase_d_bilingual.py
DELETED
|
@@ -1,86 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Phase D — bilingual bridge (EN ↔ AR): a phrase lexicon that generates
|
| 3 |
-
'other-language views' of any question. Training pairs (EN-view, AR-view)
|
| 4 |
-
of the SAME meaning teach W to place both languages in one semantic space.
|
| 5 |
-
(Bag-of-ngrams encoder => word order does not matter — token mapping suffices.)
|
| 6 |
-
"""
|
| 7 |
-
import re
|
| 8 |
-
|
| 9 |
-
# token map EN -> AR (values may be multi-token strings)
|
| 10 |
-
EN2AR = {
|
| 11 |
-
"what": "ما ايه", "is": "", "are": "", "the": "", "of": "", "in": "",
|
| 12 |
-
"a": "", "an": "", "to": "", "does": "", "did": "", "and": "و",
|
| 13 |
-
"when": "متى امتى", "who": "من مين", "whom": "من", "how": "كيف ازاي",
|
| 14 |
-
"why": "لماذا ليه", "where": "اين فين", "many": "كم كثير",
|
| 15 |
-
"capital": "عاصمة", "city": "مدينة", "country": "دولة", "kingdom": "مملكة",
|
| 16 |
-
"people": "سكان ناس بشر", "population": "سكان", "live": "يسكنون ساكنين",
|
| 17 |
-
"lives": "ساكنين", "living": "ساكنين", "work": "يعمل يشتغل",
|
| 18 |
-
"works": "يعمل يشتغل", "working": "يشتغل", "human": "إنسان بشري انسان",
|
| 19 |
-
"brain": "دماغ عقل", "mind": "عقل", "created": "صنع عمل طور",
|
| 20 |
-
"built": "عمل طور صنع", "made": "عمل صنع", "developed": "طور عمل",
|
| 21 |
-
"you": "أنت انت ك", "your": "لك", "abilities": "قدرات مهارات",
|
| 22 |
-
"skills": "مهارات", "difference": "فرق اختلاف", "between": "بين",
|
| 23 |
-
"artificial": "اصطناعي", "intelligence": "ذكاء", "machine": "آلة جهاز",
|
| 24 |
-
"learning": "تعلم تعلّم", "start": "بدأ", "started": "بدأ",
|
| 25 |
-
"operating": "تشغيل تعمل", "operations": "عمليات تشغيل",
|
| 26 |
-
"flying": "تطير", "begin": "يبدأ", "commence": "يبدأ",
|
| 27 |
-
"survive": "ينجو يعيش", "without": "بدون", "water": "ماء مياه",
|
| 28 |
-
"camels": "جمال", "number": "رقم عدد", "numbers": "أرقام",
|
| 29 |
-
"function": "دالة", "list": "قائمة ليستة", "sum": "مجموع المجموع",
|
| 30 |
-
"calculate": "يحسب", "compute": "يحسب", "sequence": "تسلسل",
|
| 31 |
-
"integers": "أعداد صحيحة", "python": "بايثون", "code": "كود",
|
| 32 |
-
"script": "سكريبت", "random": "عشوائي", "between": "بين",
|
| 33 |
-
"egypt": "مصر", "france": "فرنسا", "saudi": "سعودية", "arabia": "عربية",
|
| 34 |
-
"australia": "أستراليا", "virgin": "فيرجن", "year": "سنة عام",
|
| 35 |
-
"name": "اسم", "third": "ثالث", "largest": "الأكبر", "element": "عنصر",
|
| 36 |
-
"row": "صف", "total": "مجموع", "routine": "دالة", "carrier": "ناقلة",
|
| 37 |
-
"airline": "شركة طيران", "at": "في", "time": "وقت",
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
-
# AR -> EN (built by inverting EN2AR where unambiguous + explicit extras)
|
| 41 |
-
AR2EN = {
|
| 42 |
-
"ما": "what", "ايه": "what", "هي": "is", "من": "who", "مين": "who",
|
| 43 |
-
"اللي": "that", "كيف": "how", "ازاي": "how", "كم": "how many",
|
| 44 |
-
"قد": "how", "عاصمة": "capital", "العاصمة": "capital", "مصر": "egypt",
|
| 45 |
-
"فرنسا": "france", "السعودية": "saudi", "العربية": "arabian",
|
| 46 |
-
"المملكة": "kingdom", "يبلغ": "reaches", "عدد": "number", "سكان": "population people",
|
| 47 |
-
"يعمل": "works", "يشتغل": "works", "دماغ": "brain", "عقل": "brain",
|
| 48 |
-
"الإنسان": "human", "الانسان": "human", "البشري": "human",
|
| 49 |
-
"عملك": "created you", "طورك": "developed you", "صنعك": "made you",
|
| 50 |
-
"أنت": "you", "انت": "you", "ماهي": "what is", "الفرق": "difference",
|
| 51 |
-
"بين": "between", "الذكاء": "intelligence", "الاصطناعي": "artificial",
|
| 52 |
-
"الاصطناعية": "artificial", "التعلم": "learning", "الآلي": "machine",
|
| 53 |
-
"الالي": "machine", "قدراتك": "abilities", "وبين": "and between",
|
| 54 |
-
"ساكنين": "live", "الذكاء": "intelligence", "ليه": "why",
|
| 55 |
-
"متى": "when", "امتى": "when", "الحاجات": "things",
|
| 56 |
-
}
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
def translate(text, mapping):
|
| 60 |
-
"""Token-level 'translation' view (bag-of-words — order irrelevant)."""
|
| 61 |
-
toks = re.findall(r"\w+", text, flags=re.UNICODE)
|
| 62 |
-
out = []
|
| 63 |
-
for t in toks:
|
| 64 |
-
m = mapping.get(t.lower())
|
| 65 |
-
if m is None:
|
| 66 |
-
out.append(t) # keep unknown words (names…)
|
| 67 |
-
elif m == "":
|
| 68 |
-
continue # drop function words
|
| 69 |
-
else:
|
| 70 |
-
out.extend(m.split())
|
| 71 |
-
return " ".join(out) if out else text
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
def make_pairs(entries):
|
| 75 |
-
"""For every KB entry: (english_view, arabic_view) of the same meaning."""
|
| 76 |
-
pairs = []
|
| 77 |
-
for e in entries:
|
| 78 |
-
p = e["prompt"]
|
| 79 |
-
toks = set(re.findall(r"\w+", p.lower(), flags=re.UNICODE))
|
| 80 |
-
ar_score = sum(1 for t in toks if t in AR2EN)
|
| 81 |
-
en_score = sum(1 for t in toks if t in EN2AR)
|
| 82 |
-
if ar_score >= en_score: # Arabic prompt -> EN view + AR original
|
| 83 |
-
pairs.append((translate(p, AR2EN), p))
|
| 84 |
-
else: # English prompt -> EN original + AR view
|
| 85 |
-
pairs.append((p, translate(p, EN2AR)))
|
| 86 |
-
return pairs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|