Spaces:
Sleeping
Sleeping
Commit
·
850b586
1
Parent(s):
b0f8dcf
Hindi regex brutality
Browse files- BPE.py +37 -3
- decoded_output.txt +3 -1
- encode_decode.py +38 -9
- encode_input.txt +3 -1
BPE.py
CHANGED
@@ -3,11 +3,45 @@ import regex as re
|
|
3 |
from tqdm import tqdm
|
4 |
|
5 |
# Read text from a file
|
6 |
-
with open('
|
7 |
text = file.read()
|
8 |
|
9 |
-
#
|
10 |
-
gpt2pat = re.compile(r"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# Apply the regex pattern to the raw text to tokenize it
|
13 |
tokens = re.findall(gpt2pat, text)
|
|
|
3 |
from tqdm import tqdm
|
4 |
|
5 |
# Read text from a file
|
6 |
+
with open('text_file.txt', 'r', encoding='utf-8') as file:
|
7 |
text = file.read()
|
8 |
|
9 |
+
# Hindi-focused pattern
|
10 |
+
gpt2pat = re.compile(r"""
|
11 |
+
# Simpler syllable-based grouping
|
12 |
+
(?:[\p{Devanagari}&&[क-ह]][ा-ौ\u093C\u0901-\u0903]?) # Consonant + modifiers
|
13 |
+
# This part matches:
|
14 |
+
# - Any consonant [क-ह]
|
15 |
+
# - Optionally followed by:
|
16 |
+
# - maatras [ा-ौ] (like ा ि ी ु ू े ै ो ौ)
|
17 |
+
# - OR nukta (\u093C = ़)
|
18 |
+
# - OR chandrabindu (\u0901 = ँ)
|
19 |
+
# - OR anusvara (\u0902 = ं)
|
20 |
+
# - OR visarga (\u0903 = ः)
|
21 |
+
|
22 |
+
|[\u0905-\u0914] # Independent vowels
|
23 |
+
# Matches standalone vowels like अ आ इ ई उ ऊ ए ऐ ओ औ
|
24 |
+
|
25 |
+
|[क-ह]्[क-ह] # Basic conjuncts
|
26 |
+
# Matches basic consonant conjuncts:
|
27 |
+
# - First consonant + halant (्) + second consonant
|
28 |
+
# - Examples: क्क, न्न, त्त
|
29 |
+
|
30 |
+
|\p{N}+ # Numbers
|
31 |
+
# Matches one or more digits
|
32 |
+
|
33 |
+
|\s+ # Whitespace
|
34 |
+
# Matches spaces, tabs, newlines
|
35 |
+
|
36 |
+
|[।॥] # Punctuation
|
37 |
+
# Matches Hindi punctuation marks
|
38 |
+
|
39 |
+
|[^\s\p{Devanagari}\p{N}]+ # Other characters
|
40 |
+
# Matches any sequence of characters that aren't:
|
41 |
+
# - whitespace
|
42 |
+
# - Devanagari script
|
43 |
+
# - numbers
|
44 |
+
""", re.VERBOSE)
|
45 |
|
46 |
# Apply the regex pattern to the raw text to tokenize it
|
47 |
tokens = re.findall(gpt2pat, text)
|
decoded_output.txt
CHANGED
@@ -1 +1,3 @@
|
|
1 |
-
|
|
|
|
|
|
1 |
+
अ र्व क्षत्र आ ए ण्ड . इ ल्ल ऐ स्ज , औ इ 23 स्य द्र 10 . अ अ उ 20 ए , इ उन्ह अ .
|
2 |
+
|
3 |
+
55 र्ष अ न्यन्ड , " आ . ज्य इ . 10 ए उन्ह उन्ह . इए स्ज ए. . इ . औ अ , - र्वक्ष एओ ( ऑ) इए उन्ह ए.”
|
encode_decode.py
CHANGED
@@ -7,18 +7,47 @@ with open('bpe_results.pkl', 'rb') as f:
|
|
7 |
merges, ids, num_merges = pickle.load(f)
|
8 |
|
9 |
# Define the GPT-2 regex pattern (same as in BPE.py)
|
10 |
-
gpt2pat = re.compile(r"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
vocab = {idx: bytes([idx]) for idx in range(256)}
|
13 |
for (p0, p1), idx in merges.items():
|
14 |
vocab[idx] = vocab[p0] + vocab[p1]
|
15 |
|
16 |
def decode(ids):
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
# Write the decoded text to a new file
|
24 |
with open('decoded_output.txt', 'w', encoding='utf-8') as f:
|
@@ -27,7 +56,7 @@ def decode(ids):
|
|
27 |
return text
|
28 |
|
29 |
# Example: Decode a list of IDs
|
30 |
-
set_of_ids = [
|
31 |
decoded_text = decode(set_of_ids) # Pass the list of IDs
|
32 |
print(decoded_text)
|
33 |
|
@@ -61,5 +90,5 @@ def encode():
|
|
61 |
return final_tokens
|
62 |
|
63 |
# Example: Encode text from a file
|
64 |
-
encoded_tokens = encode()
|
65 |
-
print(encoded_tokens)
|
|
|
7 |
merges, ids, num_merges = pickle.load(f)
|
8 |
|
9 |
# Define the GPT-2 regex pattern (same as in BPE.py)
|
10 |
+
gpt2pat = re.compile(r"""
|
11 |
+
# Simpler syllable-based grouping
|
12 |
+
(?:[\p{Devanagari}&&[क-ह]][ा-ौ\u093C\u0901-\u0903]?) # Consonant + modifiers
|
13 |
+
|[\u0905-\u0914] # Independent vowels
|
14 |
+
|[क-ह]्[क-ह] # Basic conjuncts
|
15 |
+
|\p{N}+ # Numbers
|
16 |
+
|\s+ # Whitespace
|
17 |
+
|[।॥] # Punctuation
|
18 |
+
|[^\s\p{Devanagari}\p{N}]+ # Other characters
|
19 |
+
""", re.VERBOSE)
|
20 |
|
21 |
vocab = {idx: bytes([idx]) for idx in range(256)}
|
22 |
for (p0, p1), idx in merges.items():
|
23 |
vocab[idx] = vocab[p0] + vocab[p1]
|
24 |
|
25 |
def decode(ids):
|
26 |
+
# Debug printing
|
27 |
+
print("Vocabulary contents:")
|
28 |
+
for idx, byte_seq in vocab.items():
|
29 |
+
try:
|
30 |
+
char = byte_seq.decode('utf-8')
|
31 |
+
print(f"ID {idx}: bytes {list(byte_seq)} -> '{char}'")
|
32 |
+
except UnicodeDecodeError:
|
33 |
+
print(f"ID {idx}: bytes {list(byte_seq)} -> [INVALID UTF-8]")
|
34 |
+
|
35 |
+
print("\nDecoding sequence:")
|
36 |
+
tokens = []
|
37 |
+
for idx in ids:
|
38 |
+
if idx in vocab:
|
39 |
+
token_bytes = vocab[idx]
|
40 |
+
try:
|
41 |
+
char = token_bytes.decode('utf-8')
|
42 |
+
print(f"ID {idx} -> '{char}'")
|
43 |
+
except UnicodeDecodeError:
|
44 |
+
print(f"ID {idx} -> [INVALID UTF-8] {list(token_bytes)}")
|
45 |
+
tokens.append(token_bytes)
|
46 |
+
else:
|
47 |
+
print(f"Missing ID: {idx}")
|
48 |
+
|
49 |
+
# Original decoding logic
|
50 |
+
text = b''.join(tokens).decode('utf-8', errors='replace')
|
51 |
|
52 |
# Write the decoded text to a new file
|
53 |
with open('decoded_output.txt', 'w', encoding='utf-8') as f:
|
|
|
56 |
return text
|
57 |
|
58 |
# Example: Decode a list of IDs
|
59 |
+
set_of_ids = [262, 32, 32, 32, 32, 32, 32, 32, 32, 342, 32, 287, 281, 32, 32, 32, 266, 32, 32, 32, 32, 32, 32, 32, 32, 260, 32, 32, 32, 32, 32, 1719, 32, 32, 32, 46, 32, 32, 265, 32, 308, 32, 32, 317, 32, 32, 639, 32, 32, 32, 32, 32, 32, 44, 32, 272, 32, 265, 32, 32, 32, 611, 32, 410, 32, 32, 313, 32, 354, 32, 32, 32, 32, 46, 32, 32, 32, 32, 32, 32, 32, 32, 262, 32, 32, 32, 32, 32, 32, 262, 32, 32, 32, 267, 32, 297, 32, 32, 32, 32, 260, 32, 44, 32, 32, 32, 32, 32, 32, 265, 32, 32, 32, 32, 32, 32, 32, 267, 293, 32, 262, 32, 32, 32, 32, 46, 270, 666, 32, 396, 32, 262, 32, 32, 353, 829, 32, 32, 44, 32, 34, 32, 32, 32, 32, 32, 266, 32, 46, 32, 32, 32, 32, 32, 32, 32, 32, 314, 32, 32, 32, 32, 32, 32, 265, 32, 32, 32, 32, 32, 32, 46, 32, 32, 32, 32, 32, 32, 354, 32, 32, 260, 32, 32, 267, 293, 32, 32, 32, 32, 267, 293, 32, 32, 32, 32, 32, 32, 32, 46, 32, 265, 260, 32, 32, 32, 639, 32, 32, 32, 32, 32, 32, 32, 32, 260, 46, 32, 32, 32, 32, 32, 32, 32, 32, 32, 46, 32, 265, 32, 32, 32, 32, 32, 32, 32, 32, 32, 46, 32, 272, 32, 32, 262, 32, 32, 32, 32, 32, 32, 44, 32, 32, 32, 32, 32, 32, 45, 32, 32, 342, 287, 32, 32, 32, 260, 298, 32, 40, 32, 32, 351, 41, 32, 32, 32, 32, 32, 32, 32, 265, 260, 32, 267, 293, 32, 32, 32, 32, 260, 760]
|
60 |
decoded_text = decode(set_of_ids) # Pass the list of IDs
|
61 |
print(decoded_text)
|
62 |
|
|
|
90 |
return final_tokens
|
91 |
|
92 |
# Example: Encode text from a file
|
93 |
+
#encoded_tokens = encode()
|
94 |
+
#print(encoded_tokens)
|
encode_input.txt
CHANGED
@@ -1 +1,3 @@
|
|
1 |
-
|
|
|
|
|
|
1 |
+
अमजद के परिवार की तीन पीढ़ियां चांदनी चौक निर्वाचन क्षेत्र में हवेली आज़म खां के नाम से पहचाने जाने वाले एकदम सटकर बने घरों के झुण्ड में रहती हैं. यह इलाका दिल्ली की ऐतिहासिक जामा मस्जिद से पैदल की दूरी पर है, और इस परिवार के 23 सदस्य मतदान केंद्र 10 पर पंजीकृत मतदाता हैं. लेकिन पिछले साल लोकसभा चुनावों के दौरान अमजद को पता चला कि वह अपने परिवार के उन 20 लोगों में से एक हैं, जिनका नाम मतदाता सूची से इस वजह से काट दिया गया कि उन्होंने अपना घर बदल लिया है.
|
2 |
+
|
3 |
+
55 वर्षीय अमजद ने न्यूज़लॉन्ड्री को बताया, "हमारे सामने ये पहली बार हुआ है. लेकिन नाम कटने के बारे में सबसे ज्यादा निराशाजनक बात ये थी कि इसका पता मतदान के दिन ही चला. जब हम पहली बार बूथ 10 पर गए तो उन्होंने हमें बताया कि उन्हें मतदाता सूची में हमारा नाम नहीं मिला. इसलिए हमें जामा मस्जिद में किसी दूसरे बूथ पर जाकर देखना चाहिए. वहां से हमें दूसरे बूथ पर भेज दिया गया. इस तरह हमने पांच से छह बूथों का दौरा किया. और फिर अंत में हमें जो कारण बताया गया, वो यह था कि शायद घर-घर जाकर सर्वेक्षण के दौरान बीएलओ (बूथ लेवल ऑफिसर) को हम घर पर नहीं मिले इसलिए उन्होंने हमारे नाम काट दिए.”
|