Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update regex.py to correctly parse scripts with combining marks #71

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ajaykg
Copy link

@ajaykg ajaykg commented May 5, 2024

Fixing the problem that all tokenizers have with regard to all combining marks like diacritics, Indic Matras (vowels after consonants) Indic Halant, Arabic, Hebrew etc. This was probably breaking most languages except English and CJKs. Verified for Indic languages.

Fixing the problem that all tokenizers have with regard to all combining marks like diacritics, Indic Matras (vowels after consonants) Indic Halant, Arabic vowels, Hebrew vowels etc.  This was breaking most languages except English and CJKs.
@ajaykg
Copy link
Author

ajaykg commented May 5, 2024

>>> import regex as re
>>> gpt2pat = re.compile(r"""'(?i:[sdmt]|ll|ve|re)|[^\r\n\p{L}\p{N}]?+\p{L}+|\p{N}{1,3}| ?[^\s\p{L}\p{N}]++[\r\n]*|\s*[\r\n]|\s+(?!\S)|\s+""" )
>>> str = r"""हहिन्दी विकिपीडिया"""
>>> print (re.findall(gpt2pat, str ))
['हह', 'िन', '्द', 'ी', ' व', 'िक', 'िप', 'ीड', 'िय', 'ा']
>>> # The above got broken at every vovel combining mark
>>> # It can be fixed by including \p{M} wherever there is \p{L}
>>> gpt2pat = re.compile(r"""'(?i:[sdmt]|ll|ve|re)|[^\r\n\p{L}\p{N}]?+[\p{L}\p{M}]+|\p{N}{1,3}| ?[^\s\p{L}\p{M}\p{N}]++[\r\n]*|\s*[\r\n]|\s+(?!\S)|\s+""" )
>>> print (re.findall(gpt2pat, str ))
['हहिन्दी', ' विकिपीडिया']
>>> The above keep it as is and correctly breaks at word boundaries 

ajaykg added 2 commits May 7, 2024 21:52
This is the utf8 tokenizer that skips the successive utf codepage byte in the word and the chunk to increase the token density.
This was a botched approach. Let's skip.
@ajaykg ajaykg changed the title Update regex.py Update regex.py to correctly parse scripts with combining marks May 12, 2024
@ajaykg
Copy link
Author

ajaykg commented May 12, 2024

#73

@ajaykg
Copy link
Author

ajaykg commented May 22, 2024

bump.

@dustinwloring1988
Copy link

Dose this merge negatively effect anything?

@ajaykg
Copy link
Author

ajaykg commented Jun 15, 2024

Should not. Given we are telling the regular expression to not split words between a character and a combining mark after the character. The combining marks in all scripts should by definition not exist independently. Atleast for southasian languages all the vovels following a consonent are combining marks and hence it should significantly improve tokenization that is making it act almost like a character level model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants