Create encdec.py
Browse files- test_bg/encdec.py +100 -0
test_bg/encdec.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModel
|
| 2 |
+
import numpy as np
|
| 3 |
+
|
| 4 |
+
class HindiEnglishEncodeDecode:
|
| 5 |
+
def __init__(self, model_name):
|
| 6 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
self.model = AutoModel.from_pretrained(model_name)
|
| 8 |
+
|
| 9 |
+
def test_languages(self):
|
| 10 |
+
test_texts = {
|
| 11 |
+
'Hindi': [
|
| 12 |
+
'नमस्ते, मैं भारत से हूँ। दिल्ली बहुत बड़ा शहर है।',
|
| 13 |
+
'हिंदी भाषा बहुत सुंदर है।',
|
| 14 |
+
'मुझे किताबें पढ़ना पसंद है।',
|
| 15 |
+
'यह एक उदाहरण वाक्य है।'
|
| 16 |
+
],
|
| 17 |
+
'English': [
|
| 18 |
+
'Hello, I am from India. Delhi is a big city.',
|
| 19 |
+
'The English language is widely spoken.',
|
| 20 |
+
'I enjoy reading books.',
|
| 21 |
+
'This is an example sentence.'
|
| 22 |
+
]
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
results = {}
|
| 26 |
+
|
| 27 |
+
for language, texts in test_texts.items():
|
| 28 |
+
results[language] = []
|
| 29 |
+
for text in texts:
|
| 30 |
+
try:
|
| 31 |
+
token_ids = self.tokenizer.encode(text, add_special_tokens=True)
|
| 32 |
+
token_strings = self.tokenizer.tokenize(text)
|
| 33 |
+
|
| 34 |
+
decoded_text = self.tokenizer.decode(token_ids, skip_special_tokens=True)
|
| 35 |
+
|
| 36 |
+
token_stats = {
|
| 37 |
+
'min': min(token_ids),
|
| 38 |
+
'max': max(token_ids),
|
| 39 |
+
'mean': np.mean(token_ids)
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
# Append results for this text
|
| 43 |
+
results[language].append({
|
| 44 |
+
'original_text': text,
|
| 45 |
+
'token_ids_count': len(token_ids),
|
| 46 |
+
'token_strings_count': len(token_strings),
|
| 47 |
+
'decoded_text': decoded_text,
|
| 48 |
+
'text_match': text == decoded_text,
|
| 49 |
+
'token_id_stats': token_stats
|
| 50 |
+
})
|
| 51 |
+
|
| 52 |
+
print(f"\n{language} Analysis:")
|
| 53 |
+
print(f"Original Text: {text}")
|
| 54 |
+
print(f"Token IDs Count: {len(token_ids)}")
|
| 55 |
+
print(f"Token Strings: {token_strings}")
|
| 56 |
+
print(f"Decoded Text: {decoded_text}")
|
| 57 |
+
print(f"Text Reconstruction: {text == decoded_text}")
|
| 58 |
+
|
| 59 |
+
except Exception as e:
|
| 60 |
+
results[language].append({'error': str(e)})
|
| 61 |
+
print(f"{language} Error: {e}")
|
| 62 |
+
|
| 63 |
+
return results
|
| 64 |
+
|
| 65 |
+
def detailed_token_analysis(self, text):
|
| 66 |
+
token_ids = self.tokenizer.encode(text, add_special_tokens=True)
|
| 67 |
+
token_strings = self.tokenizer.tokenize(text)
|
| 68 |
+
|
| 69 |
+
analysis = {
|
| 70 |
+
'original_text': text,
|
| 71 |
+
'original_length': len(text),
|
| 72 |
+
'tokens': {
|
| 73 |
+
'ids': token_ids,
|
| 74 |
+
'strings': token_strings
|
| 75 |
+
},
|
| 76 |
+
'token_stats': {
|
| 77 |
+
'total_tokens': len(token_ids),
|
| 78 |
+
'unique_tokens': len(set(token_ids)),
|
| 79 |
+
'avg_token_length': np.mean([len(token) for token in token_strings])
|
| 80 |
+
}
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
return analysis
|
| 84 |
+
|
| 85 |
+
def main():
|
| 86 |
+
MODEL_NAME = 'tinycompany/ShawtyIsBad-bgem3'
|
| 87 |
+
|
| 88 |
+
tokenizer_model = HindiEnglishEncodeDecode(MODEL_NAME)
|
| 89 |
+
|
| 90 |
+
results = tokenizer_model.test_languages()
|
| 91 |
+
|
| 92 |
+
sample_text = 'नमस्ते, मैं भारत से हूँ। दिल्ली बहुत बड़ा शहर है।'
|
| 93 |
+
detailed_result = tokenizer_model.detailed_token_analysis(sample_text)
|
| 94 |
+
|
| 95 |
+
import json
|
| 96 |
+
with open('hindi_english_tokenization_results.json', 'w', encoding='utf-8') as f:
|
| 97 |
+
json.dump(results, f, ensure_ascii=False, indent=4)
|
| 98 |
+
|
| 99 |
+
if __name__ == "__main__":
|
| 100 |
+
main()
|