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

Fix collate() to consider tonemark in ordering #926

Merged
merged 3 commits into from
Oct 10, 2024

Conversation

WTFPUn
Copy link
Contributor

@WTFPUn WTFPUn commented Oct 8, 2024

What does this changes

Update Thai tone ordering sorted from #558

What was wrong

pythainlp.util.collate() results a wrong ordering,
from

from pythainlp.util import collate

collate(["ก้วย", "ก๋วย", "ก่วย", "กวย", "ก้วย", "ก่วย", "ก๊วย"])

collate function expect.

['กวย', 'ก่วย', 'ก่วย', 'ก้วย', 'ก้วย', 'ก๊วย', 'ก๋วย']

but before my pull request is

['ก้วย', 'ก๋วย', 'ก่วย', 'ก้วย', 'ก่วย', 'ก๊วย', 'กวย']

How this fixes it

Fixed it by use search() method from Pattern class in re module. Then concat Thai tone with current lead vowel strings. at
from

def _thkey(word: str) -> str:
    cv = _RE_TONE.sub("", word)  # remove tone
    cv = _RE_LV_C.sub("\\2\\1", cv)  # switch lead vowel
    tone = _RE_TONE.sub(" ", word)  # just tone
    return cv + tone

to

def _thkey(word: str) -> str:
    cv = _RE_TONE.sub("", word)  # remove tone
    cv = _RE_LV_C.sub("\\2\\1", cv)  # switch lead vowel

    tone_match = _RE_TONE.search(word)
    tone = tone_match.group() if tone_match else ""
    return cv + tone

Your checklist for this pull request

🚨Please review the guidelines for contributing to this repository.

  • Passed code styles and structures
  • Passed code linting checks and unit test
    P.S. some of unit test on collate function is pass but other unitest of normalize, maiyamok not pass

Copy link

sonarqubecloud bot commented Oct 8, 2024

@wannaphong wannaphong added the hacktoberfest-accepted hacktoberfest accepted pull requests. label Oct 10, 2024
@wannaphong wannaphong linked an issue Oct 10, 2024 that may be closed by this pull request
Copy link
Member

@wannaphong wannaphong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@wannaphong wannaphong merged commit 836e7f1 into PyThaiNLP:dev Oct 10, 2024
7 of 12 checks passed
@bact bact mentioned this pull request Nov 2, 2024
@bact bact changed the title Tone collate fixed Fix collate() to consider tonemark in ordering Nov 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest-accepted hacktoberfest accepted pull requests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong ordering from collate()
2 participants