neuralmind-ai / portuguese-bert

Portuguese pre-trained BERT models
Other
792 stars 122 forks source link

Issue in tokenizer with "ÃO" subtokens #45

Closed monilouise closed 1 year ago

monilouise commented 1 year ago

The tokenizer ignores the last letter of the last word in the text below. I tried to add space, \n, a special "[FIM]" suffix, but it didn't work. The tokenization only works as expected if I add a longer text after that (about three words).

For the text:

"MINISTÉRIO DA EDUCAÇÃO\nDECRETO DE 31 DE DEZEMBRO DE 2022\nO VICE-PRESIDENTE DA REPÚBLICA, no exercício do cargo dePRESIDENTE DA REPÚBLICA,no uso da atribuição que lhe confere o art. 84,caput, inciso I, da Constituição, resolve:\nEXONERAR\nVICTOR GODOY VEIGA do cargo de Ministro de Estado da Educação.\nBrasília, 31 de dezembro de 2022; 201º da Independência e 134º da República.\nANTÔNIO HAMILTON MARTINS MOURÃO"

The following output is generated by the tokenizer:

['[CLS]', 'M', '##IN', '##IS', '##T', '##É', '##RI', '##O', 'D', '##A', 'E', '##D', '##UC', '##A', '##Ç', '##ÃO', 'DE', '##C', '##RE', '##TO', 'DE', '31', 'DE', 'DE', '##Z', '##EM', '##BR', '##O', 'DE', '202', '##2', 'O', 'VI', '##CE', '-', 'PR', '##ES', '##ID', '##EN', '##TE', 'D', '##A', 'R', '##EP', '##Ú', '##B', '##L', '##IC', '##A', ',', 'no', 'exercício', 'do', 'cargo', 'de', '##PR', '##ES', '##ID', '##EN', '##TE', 'D', '##A', 'R', '##EP', '##Ú', '##B', '##L', '##IC', '##A', ',', 'no', 'uso', 'da', 'atribuição', 'que', 'lhe', 'confere', 'o', 'art', '.', '84', ',', 'cap', '##ut', ',', 'inc', '##iso', 'I', ',', 'da', 'Constituição', ',', 'resolve', ':', 'E', '##X', '##ON', '##ER', '##AR', 'VI', '##CT', '##OR', 'G', '##O', '##DO', '##Y', 'V', '##EI', '##GA', 'do', 'cargo', 'de', 'Ministro', 'de', 'Estado', 'da', 'Educação', '.', 'Brasília', ',', '31', 'de', 'dezembro', 'de', '202', '##2', ';', '[UNK]', 'da', 'Independência', 'e', '[UNK]', 'da', 'República', '.', 'AN', '##T', '##Ô', '##N', '##IO', 'H', '##AM', '##IL', '##TO', '##N', 'MA', '##RT', '##IN', '##S', 'M', '##O', '##UR', '##Ã', '[SEP]', '[PAD]'

The last "O" letter is ignored and the ##Ã subtoken is used instead of "##ÃO".

I used the following code for tokenization:


tokenized = tokenizer([texto],
                        max_length=self.max_length, stride=self.stride, padding='max_length',
                        return_overflowing_tokens=True, return_offsets_mapping=True,
                        truncation=True, is_split_into_words=True, return_tensors='pt').to(device)
rodrigonogueira4 commented 1 year ago

It seems that to work as expected with the default parameters:

import transformers

tokenizer = transformers.AutoTokenizer.from_pretrained("neuralmind/bert-base-portuguese-cased")

texto = "MINISTÉRIO DA EDUCAÇÃO\nDECRETO DE 31 DE DEZEMBRO DE 2022\nO VICE-PRESIDENTE DA REPÚBLICA, no exercício do cargo dePRESIDENTE DA REPÚBLICA,no uso da atribuição que lhe confere o art. 84,caput, inciso I, da Constituição, resolve:\nEXONERAR\nVICTOR GODOY VEIGA do cargo de Ministro de Estado da Educação.\nBrasília, 31 de dezembro de 2022; 201º da Independência e 134º da República.\nANTÔNIO HAMILTON MARTINS MOURÃO"

token_ids = tokenizer([texto]).input_ids
print(tokenizer.convert_ids_to_tokens(token_ids[0]))

['[CLS]', 'M', '##IN', '##IS', '##T', '##É', '##RI', '##O', 'D', '##A', 'E', '##D', '##UC', '##A', '##Ç', '##ÃO', 'DE', '##C', '##RE', '##TO', 'DE', '31', 'DE', 'DE', '##Z', '##EM', '##BR', '##O', 'DE', '202', '##2', 'O', 'VI', '##CE', '-', 'PR', '##ES', '##ID', '##EN', '##TE', 'D', '##A', 'R', '##EP', '##Ú', '##B', '##L', '##IC', '##A', ',', 'no', 'exercício', 'do', 'cargo', 'de', '##PR', '##ES', '##ID', '##EN', '##TE', 'D', '##A', 'R', '##EP', '##Ú', '##B', '##L', '##IC', '##A', ',', 'no', 'uso', 'da', 'atribuição', 'que', 'lhe', 'confere', 'o', 'art', '.', '84', ',', 'cap', '##ut', ',', 'inc', '##iso', 'I', ',', 'da', 'Constituição', ',', 'resolve', ':', 'E', '##X', '##ON', '##ER', '##AR', 'VI', '##CT', '##OR', 'G', '##O', '##DO', '##Y', 'V', '##EI', '##GA', 'do', 'cargo', 'de', 'Ministro', 'de', 'Estado', 'da', 'Educação', '.', 'Brasília', ',', '31', 'de', 'dezembro', 'de', '202', '##2', ';', '[UNK]', 'da', 'Independência', 'e', '[UNK]', 'da', 'República', '.', 'AN', '##T', '##Ô', '##N', '##IO', 'H', '##AM', '##IL', '##TO', '##N', 'MA', '##RT', '##IN', '##S', 'M', '##O', '##UR', '##ÃO', '[SEP]']

Could you please check if it works without these extra parameters (return_overflowing_tokens, is_split_into_words, etc)?

monilouise commented 1 year ago

As I cannot remove the other parameters (it would break other parts in the code), I only removed is_split_into_words and replaced "[texto]" by "texto".

Same problem...

rodrigonogueira4 commented 1 year ago

Ok, I can try to reproduce myself if you tell me the values for self.max_length and stride=self.stride

monilouise commented 1 year ago

max_length=512 stride=384

rodrigonogueira4 commented 1 year ago

Also couldn't reproduce your error. Please try the colab here: https://colab.research.google.com/drive/12UMX7ykY-irkkWv5EJhu5IZTWoVhMHSL?usp=sharing

monilouise commented 1 year ago

I discovered and corrected a problem in a legacy code that keeps word indices for output highlighting and the problem stopped occurring. So the problem wasn't in the tokenizer - it only appeared to be related to tokenization because it didn't occur when I added more text to the end of the string, masking the true issue.

Anyway, thanks for the feedback!