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

Recognize the primary alphabet for different alphabets per text #17

Open
JohannesRie opened this issue Feb 12, 2025 · 0 comments
Open

Comments

@JohannesRie
Copy link

JohannesRie commented Feb 12, 2025

Hi,

I didn't know exactly where to post it. It's not a problem as such, just an addition.

For an offline translator based on Meta AI's NLLB, I use this detector in conjunction with langdetect, among other things, to recognize text modules if no source language is specified. This occasionally causes problems if Latin characters are present in a predominantly Arabic, Cyrillic or other alphabet, as all alphabets are returned by the detection. The following code provides a remedy if the text is predominantly written in one alphabet:

def alphabet_detect(text : str) -> str:
    ad = AlphabetDetector()
    result = ad.detect_alphabet(u'{}'.format(text))
    if len(result) > 1:
        result = []
        for word in text:
            alpha = ad.detect_alphabet(u'{}'.format(word))
            if len(alpha) == 1:
                result.append(next(iter(alpha)))
            alphabet_with_highest_count = max(set(result), key=result.count) 
        return(alphabet_with_highest_count)
    else:
        return next(iter(result))
@JohannesRie JohannesRie changed the title For multiple alphabets per Text Recognize the primary alphabet for different alphabets per text Feb 12, 2025
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

No branches or pull requests

1 participant