Views
No views yet
normalize_text methoddetect_nsw method for detailed analysisAutoModelForMaskedLMpip install transformers torch1from transformers import AutoTokenizer, AutoModelForMaskedLM
2
3# Load model and tokenizer
4model_repo = "hadung1802/visobert-normalizer"
5tokenizer = AutoTokenizer.from_pretrained(model_repo)
6model = AutoModelForMaskedLM.from_pretrained(model_repo, trust_remote_code=True)
7
8# Normalize text
9text = "sv dh gia dinh chua cho di lam :))"
10normalized_text, source_tokens, predicted_tokens = model.normalize_text(
11 tokenizer, text, device='cpu'
12)
13
14print(f"Original: {text}")
15print(f"Normalized: {normalized_text}")1# Detect Non-Standard Words (NSW) in text
2text = "nhìn thôi cung thấy đau long quá đi :))"
3nsw_results = model.detect_nsw(tokenizer, text, device='cpu')
4
5print(f"Text: {text}")
6for result in nsw_results:
7 print(f"NSW: '{result['nsw']}' → '{result['prediction']}' (confidence: {result['confidence_score']})")1texts = [
2 "sv dh gia dinh chua cho di lam :))",
3 "chúng nó bảo em là ctrai",
4 "t vs b chơi vs nhau đã lâu"
5]
6
7for text in texts:
8 normalized_text, _, _ = model.normalize_text(tokenizer, text, device='cpu')
9 print(f"{text} → {normalized_text}")sv dh gia dinh chua cho di lam :)) → sinh viên đại học gia đình chưa cho đi làm :))
chúng nó bảo em là ctrai → chúng nó bảo em là con trai
t vs b chơi vs nhau đã lâu → tôi với bạn chơi với nhau đã lâu1# Input: "nhìn thôi cung thấy đau long quá đi :))"
2[
3 {
4 "index": 3,
5 "start_index": 10,
6 "end_index": 14,
7 "nsw": "cung",
8 "prediction": "cũng",
9 "confidence_score": 0.9415
10 },
11 {
12 "index": 6,
13 "start_index": 24,
14 "end_index": 28,
15 "nsw": "long",
16 "prediction": "lòng",
17 "confidence_score": 0.7056
18 }
19]detect_nsw method returns a list of dictionaries with the following structure:index: Position of the token in the sequencestart_index: Start character position in the original textend_index: End character position in the original textnsw: The original non-standard word (detokenized)prediction: The predicted normalized word (detokenized)confidence_score: Combined confidence score (0.0 to 1.0)