File size: 15,757 Bytes
8a02978 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
#!/usr/bin/env python3
import argparse
import os
import re
import sys
from dataclasses import dataclass
from typing import List, Sequence, Set, Tuple, Dict, Union, Optional
from hindi_xlit import HindiTransliterator
import torch
from transformers import AutoTokenizer, AutoModelForTokenClassification
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
MODEL_DIR = os.path.join(BASE_DIR, 'hing_bert_module', 'hing-bert-lid')
DICTIONARY_PATH = os.path.join(BASE_DIR, 'hing_bert_module', 'dictionary.txt')
OUTPUT_PATH = os.path.join(BASE_DIR, 'output2.txt')
LOG_INITIALIZED = False
LABEL_MAP = None
LABEL_TO_ID = None
TOKEN_RE = re.compile(r"[A-Za-zĀāĪīŪūṚṛṝḶḷḸḹēēōōṃḥśṣṭḍṇñṅ'’-]+")
COMMON_ENGLISH_STOPWORDS = {
'a','he', 'an', 'and', 'are', 'as', 'at', 'be', 'because', 'been', 'but', 'by', 'for', 'from',
'had', 'has', 'have', 'he', 'her', 'here', 'him', 'his', 'how', 'i', 'in', 'is', 'it',
'its', 'me', 'my', 'no', 'not', 'of', 'on', 'or', 'our', 'she', 'so', 'that', 'the',
'their', 'them', 'there', 'they', 'this', 'those', 'to', 'was', 'we', 'were', 'what',
'when', 'where', 'which', 'who', 'whom', 'why', 'will', 'with', 'you', 'your'
}
@dataclass
class TokenPrediction:
token: str
label: str
confidence: float
def load_model(device: str | None = None):
if device:
dev = torch.device(device)
else:
dev = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR, local_files_only=True)
model = AutoModelForTokenClassification.from_pretrained(MODEL_DIR, local_files_only=True)
model.to(dev)
model.eval()
global LABEL_MAP, LABEL_TO_ID
config = model.config
if hasattr(config, 'id2label') and config.id2label:
LABEL_MAP = {int(k): v for k, v in config.id2label.items()}
else:
LABEL_MAP = {i: str(i) for i in range(config.num_labels)}
if hasattr(config, 'label2id') and config.label2id:
LABEL_TO_ID = {str(k): int(v) for k, v in config.label2id.items()}
else:
LABEL_TO_ID = {v: k for k, v in LABEL_MAP.items()}
return tokenizer, model, dev
def _tokenize(text: str) -> List[str]:
tokens = [m.group(0) for m in TOKEN_RE.finditer(text)]
if tokens:
return tokens
return text.strip().split()
def _hindi_pattern_score(token: str) -> float:
t = token.lower()
if len(t) <= 1:
return 0.0
clusters = ['bh', 'chh', 'ch', 'dh', 'gh', 'jh', 'kh', 'ksh', 'ph', 'sh', 'th', 'tr', 'shr', 'str', 'vr', 'kr', 'gy', 'ny', 'arj', 'rj']
vowels = ['aa', 'ai', 'au', 'ee', 'ii', 'oo', 'ou']
suffixes = ['a', 'aa', 'am', 'an', 'as', 'aya', 'ana', 'ara', 'iya', 'ika', 'tra']
score = 0.0
for c in clusters:
if c in t:
score += 0.4
for v in vowels:
if v in t:
score += 0.2
for suf in suffixes:
if t.endswith(suf) and len(t) > len(suf):
score += 0.3
if t.endswith(('a', 'i', 'o', 'u')):
score += 0.1
if re.search(r'[kgcjtdpb]h', t):
score += 0.2
return score
def classify_text(
text: str,
tokenizer,
model,
device,
threshold: float,
) -> List[TokenPrediction]:
words = _tokenize(text)
if not words:
return []
batch = tokenizer(
words,
return_tensors='pt',
padding=True,
truncation=True,
is_split_into_words=True
)
word_ids = batch.word_ids(batch_index=0)
batch = {k: v.to(device) for k, v in batch.items()}
with torch.no_grad():
outputs = model(**batch)
logits = outputs.logits.squeeze(0)
word_logits: dict[int, torch.Tensor] = {}
word_counts: dict[int, int] = {}
for idx, word_id in enumerate(word_ids):
if word_id is None:
continue
if word_id not in word_logits:
word_logits[word_id] = logits[idx]
word_counts[word_id] = 1
else:
word_logits[word_id] += logits[idx]
word_counts[word_id] += 1
predictions: List[TokenPrediction] = []
for word_index, word in enumerate(words):
logits_sum = word_logits.get(word_index)
if logits_sum is None:
predictions.append(TokenPrediction(word, 'N/A', 0.0))
continue
avg_logits = logits_sum / word_counts[word_index]
probs = torch.softmax(avg_logits, dim=-1)
conf, idx = torch.max(probs, dim=-1)
raw_label = LABEL_MAP.get(int(idx), str(int(idx)))
hi_idx = LABEL_TO_ID.get('HI') if LABEL_TO_ID else None
en_idx = LABEL_TO_ID.get('EN') if LABEL_TO_ID else None
hi_prob = float(probs[hi_idx]) if hi_idx is not None else 0.0
en_prob = float(probs[en_idx]) if en_idx is not None else float(conf)
final_label = raw_label
conf_value = float(conf)
if hi_idx is not None and hi_prob >= threshold:
final_label = 'HI'
conf_value = hi_prob
elif raw_label == 'HI':
final_label = 'HI'
conf_value = hi_prob
else:
lower = word.lower()
pattern_score = _hindi_pattern_score(word)
is_capitalized = word[:1].isupper() and not word.isupper()
override = False
if hi_prob >= threshold - 0.05:
override = True
elif hi_prob >= 0.60 and pattern_score >= 0.5:
override = True
elif hi_prob >= 0.45 and pattern_score >= 0.6 and is_capitalized:
override = True
elif pattern_score >= 0.8 and hi_prob >= 0.40 and lower not in COMMON_ENGLISH_STOPWORDS:
override = True
if override and lower not in COMMON_ENGLISH_STOPWORDS:
final_label = 'HI'
conf_value = max(hi_prob, threshold - 0.05)
else:
final_label = 'EN'
conf_value = en_prob
if conf_value < 0.97:
final_label = 'HI'
conf_value = max(conf_value, 0.96)
predictions.append(TokenPrediction(word, final_label, conf_value))
return predictions
def _print_predictions(predictions: Sequence[TokenPrediction]):
print("Token\tLabel\tConfidence")
for pred in predictions:
print(f"{pred.token}\t{pred.label}\t{pred.confidence:.4f}")
def _init_output_log():
global LOG_INITIALIZED
if LOG_INITIALIZED:
return
with open(OUTPUT_PATH, 'w', encoding='utf-8') as f:
f.write('HingBERT-LID session log\n')
LOG_INITIALIZED = True
def load_dictionary(filename: str = None) -> Dict[str, str]:
"""
Load the mythology dictionary from file.
Returns a dictionary mapping English words to Hindi transliterations.
"""
filename = filename or DICTIONARY_PATH
dictionary = {}
try:
with open(filename, 'r', encoding='utf-8') as f:
in_dict = False
for line in f:
line = line.strip()
# Skip empty lines and comments
if not line or line.startswith('#'):
continue
# Check if we're in the dictionary section
if 'MYTHOLOGY_DICTIONARY = {' in line:
in_dict = True
line = line.split('{', 1)[1].strip()
if not line: # If the line ends after {
continue
if not in_dict:
continue
# Process key-value pairs
if ':' in line:
# Handle multi-line entries
while not line.rstrip().endswith(','):
next_line = next(f, '').strip()
if not next_line:
break
line += ' ' + next_line
# Handle the last line which might end with }
line = line.split('}')[0].strip()
# Split into key-value pairs
entries = [e.strip() for e in line.split(',') if ':' in e]
for entry in entries:
try:
key_part, value_part = entry.split(':', 1)
key = key_part.strip().strip("'\"")
value = value_part.strip().strip("'\"").rstrip('}')
if key and value:
dictionary[key.lower()] = value
except (ValueError, IndexError):
continue
# Check for end of dictionary
if '}' in line and in_dict:
break
print(f"✓ Dictionary loaded successfully: {len(dictionary)} words")
return dictionary
except FileNotFoundError:
print(f"Warning: Dictionary file '{filename}' not found.")
print("Proceeding with model-only transliteration.")
return {}
except Exception as e:
print(f"Warning: Error loading dictionary: {str(e)}")
print("Proceeding with model-only transliteration.")
return {}
def get_transliteration(word: str, dictionary: Dict[str, str], transliterator, show_source: bool = False) -> Union[str, tuple]:
"""
Get transliteration for a word.
First checks dictionary, then falls back to model.
Args:
word: English word to transliterate
dictionary: Dictionary mapping English to Hindi
transliterator: HindiTransliterator instance
show_source: If True, returns (transliteration, source)
Returns:
Transliteration string, or tuple (transliteration, source) if show_source=True
"""
word_lower = word.lower().strip()
# Check dictionary first
if word_lower in dictionary:
result = dictionary[word_lower]
if show_source:
return result, "dictionary"
return result
# Fall back to model
try:
model_result = transliterator.transliterate(word)
# Handle if model returns a list
if isinstance(model_result, list):
result = model_result[0] # Take first (best) result
else:
result = model_result
if show_source:
return result, "model"
return result
except Exception as e:
if show_source:
return word, "error"
return word
def _write_predictions(predictions: Sequence[TokenPrediction], source_text: str):
"""Write predictions to output file."""
global LOG_INITIALIZED
_init_output_log()
with open(OUTPUT_PATH, 'a', encoding='utf-8') as f:
if not LOG_INITIALIZED:
f.write('\n' + '='*80 + '\n')
LOG_INITIALIZED = True
f.write(f'\nSource: {source_text}\n')
for pred in predictions:
f.write(f"{pred.token}\t{pred.label}\t{pred.confidence:.4f}\n")
def main():
parser = argparse.ArgumentParser(description='Test l3cube-pune/hing-bert-lid on text (token-level).')
parser.add_argument('--device', type=str, default=None, help='torch device (cpu or cuda)')
parser.add_argument('--text', type=str, default=None, help='Text to classify.')
parser.add_argument('--threshold', type=float, default=0.80, help='Confidence threshold for Hindi override heuristics (default=0.80)')
parser.add_argument('--dictionary', type=str, default=DICTIONARY_PATH, help='Path to dictionary file (default: dictionary.txt)')
args = parser.parse_args()
tokenizer, model, device = load_model(args.device)
# Load dictionary for transliteration
dictionary = load_dictionary(args.dictionary)
transliterator = HindiTransliterator()
hindi_words = set() # To store unique Hindi words
if args.text:
preds = classify_text(args.text, tokenizer, model, device, args.threshold)
_print_predictions(preds)
_write_predictions(preds, args.text)
# Add Hindi words to the set
hindi_words.update(pred.token for pred in preds if pred.label == 'HI')
print("\nHindi words found:", ", ".join(hindi_words) if hindi_words else "None")
return
print("Interactive mode. Type text lines (QUIT to exit).")
_init_output_log()
all_input = [] # Store all input text
try:
for line in sys.stdin:
line = line.rstrip('\n')
if not line:
continue
if line.strip().upper() == 'QUIT':
break
all_input.append(line) # Add to full input
preds = classify_text(line, tokenizer, model, device, args.threshold)
_print_predictions(preds)
_write_predictions(preds, line)
# Add Hindi words to the set
hindi_words.update(pred.token for pred in preds if pred.label == 'HI')
print()
except (KeyboardInterrupt, EOFError):
pass
finally:
# Print all collected Hindi words before exiting
if hindi_words:
print("\nAll Hindi words found:", ", ".join(sorted(hindi_words)))
# Create a mapping of Hindi words to their Devanagari transliterations
print("\nTransliterated to Devanagari:")
hindi_to_devanagari = {}
for word in sorted(hindi_words):
try:
devanagari = get_transliteration(word, dictionary, transliterator)
hindi_to_devanagari[word] = devanagari
source = " (dictionary)" if word.lower() in dictionary else " (model)"
print(f"{word} -> {devanagari}{source}")
except Exception as e:
print(f"{word} -> [Error: {str(e)}]")
# Save the output to a file
output_file = os.path.join(BASE_DIR, 'final_output.txt')
with open(output_file, 'w', encoding='utf-8') as f:
# Write original full text
f.write("=== Original Text ===\n")
f.write('\n'.join(all_input) + '\n\n')
# Write reconstructed text
f.write("=== Reconstructed Text with Devanagari ===\n")
for line in all_input:
reconstructed = line
for word, devanagari in hindi_to_devanagari.items():
# Use a more precise replacement that preserves punctuation and spacing
reconstructed = re.sub(
rf'(?<![\w\-])({re.escape(word)})(?![\w\-])',
devanagari,
reconstructed,
flags=re.IGNORECASE
)
f.write(reconstructed + '\n')
# Write Hindi words and their transliterations
f.write("\n=== Hindi Words and Transliterations ===\n")
for word, devanagari in sorted(hindi_to_devanagari.items()):
f.write(f"{word} -> {devanagari}\n")
print(f"\nOutput saved to: {output_file}")
else:
print("\nNo Hindi words found.")
if __name__ == '__main__':
if sys.platform == 'win32':
try:
sys.stdout.reconfigure(encoding='utf-8')
except (AttributeError, TypeError):
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
main()
|