sayed125 commited on
Commit
d7ec002
·
verified ·
1 Parent(s): 05c8c2a

Upload phase_c_augment.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. phase_c_augment.py +106 -0
phase_c_augment.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Phase C — training-signal generator: paraphrase augmentation.
3
+ Word-level synonyms (EN + AR), word dropout, typos. This is the 'exposure'
4
+ that teaches the encoder that start≈begin, عاصمة≈العاصمة, etc.
5
+ (Honest note: with 243 entries this lexicon is the training signal;
6
+ at Phase-D scale the same InfoNCE learns distributional semantics from data.)
7
+ """
8
+ import re
9
+
10
+ SYN = {
11
+ # English
12
+ "start": ["begin", "commence", "launch"],
13
+ "started": ["began", "commenced", "launched"],
14
+ "operating": ["flying", "functioning", "working", "operations"],
15
+ "survive": ["live", "endure"],
16
+ "get": ["find", "obtain", "retrieve"],
17
+ "total": ["sum"],
18
+ "routine": ["function"],
19
+ "carrier": ["airline"],
20
+ "make": ["write", "create"],
21
+ "write": ["create", "make"],
22
+ "generate": ["produce", "create"],
23
+ "operate": ["run", "work", "function"],
24
+ "services": ["flights", "operations", "service"],
25
+ "commenced": ["began", "started"],
26
+ "capital": ["capital city"],
27
+ "what": ["which"],
28
+ "when": ["at what time"],
29
+ "how": ["in what way"],
30
+ "many": ["number of"],
31
+ "people": ["population", "inhabitants"],
32
+ "live": ["reside", "living"],
33
+ "difference": ["distinction", "gap"],
34
+ "created": ["built", "made", "developed"],
35
+ "built": ["made", "created", "developed"],
36
+ "developed": ["created", "built", "made"],
37
+ "create": ["make", "write", "build"],
38
+ "calculate": ["compute", "sum", "evaluate"],
39
+ "function": ["method", "program"],
40
+ "sequence": ["list", "series"],
41
+ "integers": ["numbers", "ints"],
42
+ "brain": ["mind"],
43
+ "human": ["person"],
44
+ "works": ["functions", "operates"],
45
+ "working": ["functioning", "operating"],
46
+ "artificial": ["synthetic"],
47
+ "learning": ["training"],
48
+ "machine": ["computer"],
49
+ "intelligence": ["ai"],
50
+ "who": ["whom"],
51
+ "your": ["ur"],
52
+ # Arabic
53
+ "ما": ["ايه"],
54
+ "هي": ["هى"],
55
+ "من": ["مين"],
56
+ "كيف": ["ازاي"],
57
+ "كم": ["قد ايه"],
58
+ "عاصمة": ["العاصمة", "العاصمة"],
59
+ "يبلغ": ["يصل", "يقدر"],
60
+ "يعمل": ["يشتغل", "يقوم"],
61
+ "عملك": ["طورك", "صنعك", "تطويرك"],
62
+ "تطويرك": ["عملك", "صنعك"],
63
+ "أنت": ["انت"],
64
+ "دماغ": ["عقل"],
65
+ "الإنسان": ["البشري", "الانسان"],
66
+ "بين": ["وبين"],
67
+ "ماهي": ["ايه هي"],
68
+ "الفرق": ["الاختلاف"],
69
+ "عدد": ["عدد"],
70
+ "ساكنين": ["سكان", "السكان"],
71
+ "الساحل": ["الشاطئ"],
72
+ }
73
+
74
+ _DROP = ["the", "of", "a", "an", "to", "is", "are", "was", "في", "من"]
75
+
76
+
77
+ def augment(text, rng):
78
+ """One noisy paraphrase view of `text`."""
79
+ words = re.findall(r"\w+", text, flags=re.UNICODE)
80
+ if len(words) == 0:
81
+ return text
82
+ out = []
83
+ for w in words:
84
+ lw = w.lower()
85
+ if rng.rand() < 0.6 and lw in SYN: # aggressive synonymy
86
+ out.append(SYN[lw][rng.randint(len(SYN[lw]))])
87
+ else:
88
+ out.append(w)
89
+ # word dropout (keep at least 2 words)
90
+ kept = [w for w in out if rng.rand() > 0.25 or w.lower() not in _DROP]
91
+ if len(kept) >= 2:
92
+ out = kept
93
+ # typos (char delete/swap/insert) — char 3-grams make this robust
94
+ final = []
95
+ for w in out:
96
+ if rng.rand() < 0.12 and len(w) > 3:
97
+ i = rng.randint(1, len(w) - 1)
98
+ kind = rng.randint(3)
99
+ if kind == 0:
100
+ w = w[:i] + w[i + 1:] # delete
101
+ elif kind == 1:
102
+ w = w[:i] + w[i + 1] + w[i] + w[i + 2:] # swap
103
+ else:
104
+ w = w[:i] + w[i] + w[i:] # insert
105
+ final.append(w)
106
+ return " ".join(final)