Skip to content

BertTokenizer recomposes Korean after stripping accents, so it gives [UNK] with uncased multilingual BERT #7957

Todo

Description

@krwq

System Information (please complete the following information):

  • OS & Version: Windows 11, with both ICU and NLS
  • ML.NET Version: Microsoft.ML.Tokenizers 3.0.0, and main has the same code
  • .NET Version: .NET 10.0

Describe the bug
With RemoveNonSpacingMarks = true, BertNormalizer decomposes the text (NFD) and drops the non-spacing marks. NFD splits each Hangul syllable into jamo, which aren't marks, so they stay. But Normalize ends with an NFC, which puts the jamo back together into syllables.

Hugging Face doesn't recompose, so its uncased vocabularies have Korean as jamo. The vocab.txt of bert-base-multilingual-uncased has 1,459 tokens with jamo and none with Hangul syllables, so every Korean word becomes [UNK].

To Reproduce

  1. Create a console app:
    dotnet new console
    dotnet add package Microsoft.ML.Tokenizers --version 3.0.0
    
  2. Download vocab.txt of bert-base-multilingual-uncased into the project folder. Its tokenizer lowercases and strips accents, which in ML.NET is RemoveNonSpacingMarks = true (lowercasing is on by default).
  3. Replace Program.cs with:
    using Microsoft.ML.Tokenizers;
    
    var vocab = File.ReadAllLines("vocab.txt");
    var tokenizer = BertTokenizer.Create("vocab.txt", new BertOptions { RemoveNonSpacingMarks = true });
    
    var ids = tokenizer.EncodeToIds("서울은 한국의 수도입니다.");
    Console.WriteLine(string.Join(" ", ids.Select(id => vocab[id])));
    Console.WriteLine(string.Join(" ", ids));
  4. dotnet run prints:
    [CLS] [UNK] [UNK] [UNK] . [SEP]
    101 100 100 100 119 102
    

Expected behavior
The same tokens as Hugging Face. With its tokenizers package (pip install tokenizers):

from tokenizers import Tokenizer

tokenizer = Tokenizer.from_pretrained("google-bert/bert-base-multilingual-uncased")
encoding = tokenizer.encode("서울은 한국의 수도입니다.")
print(" ".join(encoding.tokens))
print(" ".join(map(str, encoding.ids)))
[CLS] 서울 ##은 한국 ##의 수도 ##입 ##니다 . [SEP]
101 47403 11375 47529 10576 66548 62776 82566 119 102

The Korean tokens look like syllables, but they're jamo: 서울 is U+1109 U+1165 U+110B U+116E U+11AF.

Screenshots, Code, Sample Projects
The NFC is at the end of BertNormalizer.Normalize:

string result = index == 0 ? string.Empty : new string(buffer, 0, index).Normalize(NormalizationForm.FormC);

Hugging Face's BertNormalizer decomposes and strips the marks, but doesn't recompose: bert.rs.

If I skip that NFC, with a custom Normalizer that otherwise does the same for this text, I get exactly Hugging Face's IDs, so removing it should fix this.

Additional context

Activity

  1. added
    bug
    area-Tokenizers
    on Nov 25, 2026
  2. self-assigned this
    on Nov 25, 2026
  3. added
    untriaged
    on Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-Tokenizersbuguntriaged

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions