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

Add arabic normalizer for search #21724

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

Add arabic normalizer for search #21724

wants to merge 2 commits into from

Conversation

ivanPyrohivskyi
Copy link
Contributor

To issue #11709

public static boolean cmatches(Collator collator, String fullName, String part, StringMatcherMode mode){
String withoutDiacritic = ArabicNormalizer.normalize(fullName);
Copy link
Member

Choose a reason for hiding this comment

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

Very ineffective too many calls for comparision plus overhead of 10 replaceAll methods

Copy link
Member

Choose a reason for hiding this comment

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

Yes, exactly. Some hints:

  • Simplify the Kashida replacement by using 'replace' instead of 'replaceAll', more efficient (when no regex is needed).
  • Use a single single map-based lookup to reduce the redundancy of multiple 'replace' calls
  • Use a Stream to initialize the character replacements, makes the code easier to maintain.

Copy link
Member

Choose a reason for hiding this comment

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

How's this:

public class ArabicNormalizer {

    private static final Pattern DIACRITICS_PATTERN = Pattern.compile("\\p{Mn}");

    private static final Map<String, String> CHAR_REPLACEMENTS = Stream.of(new String[][] {
        {"إ", "ا"}, {"أ", "ا"}, {"ئ", "ي"}, {"ؤ", "و"},
        {"آ", "ا"}, {"ى", "ي"}, {"ة", "ه"}
    }).collect(Collectors.toMap(data -> data[0], data -> data[1]));

    public static String normalize(String text) {
        if (text == null) {
            return null;  // Handle null input
        }

        String normalized = Normalizer.normalize(text, Normalizer.Form.NFD);
        normalized = DIACRITICS_PATTERN.matcher(normalized).replaceAll(""); // Remove diacritics efficiently

        // Hamza and other normalizations
        for (Map.Entry<String, String> entry : CHAR_REPLACEMENTS.entrySet()) {
            normalized = normalized.replace(entry.getKey(), entry.getValue());
        }

        // Kashida
        normalized = normalized.trim().replace("\u0640", ""); // Kashida

        return normalized;
    }
}

@ivanPyrohivskyi ivanPyrohivskyi marked this pull request as draft January 15, 2025 17:05
@ivanPyrohivskyi ivanPyrohivskyi marked this pull request as ready for review January 15, 2025 17:51
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.

3 participants