Skip to content

test(591): pin comparator scores on Arabic and Persian text - #597

Merged
digalaviz merged 1 commit into
awslabs:devfrom
nursnaaz:test/591-arabic-comparator-tests
Nov 19, 2026
Merged

digalaviz merged 1 commit into
awslabs:devfrom
nursnaaz:test/591-arabic-comparator-tests

Conversation

@nursnaaz

Copy link
Copy Markdown
Collaborator

Issue Reference

Part of #591. This is the first step there, pinning today's scores. Which results are gaps is still an open question in #591, so the issue stays open.

Description of Changes

Adds tests/common/comparators/test_arabic_text.py. It records what the text, date and phone comparators score today on Arabic and Persian text, so a later change to any of them is deliberate. Tests only: no source change and no score change.

Changes Made

What the tests pin, on dev at 9fc70de:

  • Written variants. Exact and Normalized score 0.0 for seven pairs that are the same word or value written two ways:

    • hamza on alef;
    • ta marbuta and ha;
    • alef maqsura and ya;
    • diacritics;
    • tatweel;
    • Persian and Arabic letter forms;
    • Arabic-Indic digits in an ID.

    Levenshtein, Fuzzy and ANLS* give these partial credit, pinned as fractions. For example, diacritics score 1/2 with Levenshtein. ANLS* scores 0.0 once Levenshtein falls below its 0.5 leaf threshold: مُحَمَّدٌ/محمد is 4/9 with Levenshtein and 0.0 with ANLS*.

  • Presentation forms. Every text comparator, Levenshtein and Fuzzy included, scores 0.0 for presentation forms against the letters they display: positional forms of محمد, and the lam-alef ligature against لا. Text extracted from some PDFs contains these.

  • Unicode handling in Normalized.

    • It applies NFC, so a precomposed and a decomposed hamza match. The other four apply no NFC or NFKC normalization.
    • It removes Arabic punctuation (،, ؟) and all whitespace.
    • It keeps format characters, so a right-to-left mark (U+200F) or a Persian zero-width non-joiner (U+200C) makes it score 0.0.
  • Dates and phone numbers.

    • DateComparator matches dates written in Arabic-Indic and Persian digits, and reads a year-first date such as ٢٠٢٤-٠٣-٠٤ as year, month, day.
    • It scores 0.0 for a different day, and for a date with an Arabic month name (14 مايو 2024). The same date with May matches.
    • It scores 0.0 for CLDR's Arabic short date. For ar-EG, ar-SA and ar-AE, CLDR puts a right-to-left mark after the day and the month (١٤‏/٥‏/٢٠٢٤); without the marks, the same date matches.
    • It scores 0.0 for a Solar Hijri date, CLDR's short date for fa-IR (۱۴۰۳/۲/۲۵ for 14 May 2024).
    • PhoneComparator matches numbers in Arabic-Indic and Persian digits, and scores 0.0 for a different number.

The date tests depend on python-dateutil reading these digits, and on DateComparator's year-first check (_YEAR_FIRST_RE) matching them through \d. The phone tests depend on phonenumberslite, and the Fuzzy scores on rapidfuzz. If a dependency bump fails one of them, the user-visible score changed.

Why These Changes Are Needed

Apart from NumericComparator (#560), nothing tested the comparators on Arabic text, so a change to their scores there would go unnoticed. If #591 settles that some of these results are gaps, the fix PR will change the matching assertions here, on purpose. The tests are in one file, rather than spread across test_normalized.py, test_date.py and test_phone.py, so that the language-support docs in #592 can link to one place.

The alef maqsura pair is مصطفى/مصطفي rather than the على/علي pair in the issue, because على (the preposition) and علي (the name) are different words.

Testing

  • Unit tests added/updated

  • Integration tests added/updated

  • Manual testing completed

  • All existing tests pass

  • New file: 84 tests.

  • Full suite: CI's own commands pass on Python 3.10, 3.11, 3.12, 3.13 and 3.14. I ran uv sync --extra llm --extra bert --frozen, then uv run coverage run -m pytest -v -s. Each version gives 5,541 passed (84 of them new), 3 skipped and 1 xfailed.

  • Lint: ruff check . passes, and ruff format --check passes on the new file.

  • Mutation checks: I made 23 targeted source changes, one at a time, and each makes at least one new test fail.

The 23 source changes
  • NormalizedComparator:
    • no NFC;
    • NFKC instead of NFC;
    • also remove control and format characters (category C);
    • also remove the zero-width non-joiner;
    • remove combining marks;
    • keep punctuation;
    • keep whitespace.
  • ExactComparator: apply NFC; remove punctuation and whitespace.
  • LevenshteinComparator: apply NFC; remove tatweel.
  • FuzzyComparator: apply NFC; remove combining marks.
  • ANLSStarComparator: apply NFC; no leaf threshold; leaf threshold < becomes <=.
  • DateComparator:
    • reject non-ASCII digits;
    • year-first check matches ASCII digits only;
    • year-first check matches ASCII and Arabic-Indic digits only;
    • remove right-to-left marks before parsing;
    • read the Arabic month name مايو as May.
  • PhoneComparator: reject non-ASCII digits; any two valid numbers match.

Checklist

  • Code follows the project's coding standards
  • Self-review of code completed
  • Documentation updated (if applicable): the docs are Document which languages and scripts each comparator supports #592
  • CHANGELOG.md entry added under [Unreleased], or not needed because nothing user-visible changed
  • Tests added/updated for new functionality
  • All CI checks are passing

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@github-actions

Copy link
Copy Markdown

Security Scan Results

ASH Security Scan Report

  • Report generated: 2026-11-19T03:11:58+00:00
  • Time since scan: 3 minutes

Scan Metadata

  • Project: ASH
  • Scan executed: 2026-11-19T03:08:51+00:00
  • ASH version: 3.1.2

Summary

Scanner Results

The table below shows findings by scanner, with status based on severity thresholds and dependencies:

  • Severity levels:
    • Suppressed (S): Findings that have been explicitly suppressed and don't affect scanner status
    • Critical (C): Highest severity findings that require immediate attention
    • High (H): Serious findings that should be addressed soon
    • Medium (M): Moderate risk findings
    • Low (L): Lower risk findings
    • Info (I): Informational findings with minimal risk
  • Duration (Time): Time taken by the scanner to complete its execution
  • Actionable: Number of findings at or above the threshold severity level that require attention
  • Result:
    • PASSED = No findings at or above threshold
    • FAILED = Findings at or above threshold
    • MISSING = Required dependencies not available
    • SKIPPED = Scanner explicitly disabled
    • ERROR = Scanner execution error
  • Threshold: The minimum severity level that will cause a scanner to fail
    • Thresholds: ALL, LOW, MEDIUM, HIGH, CRITICAL
    • Source: Values in parentheses indicate where the threshold is set:
      • global (global_settings section in the ASH_CONFIG used)
      • config (scanner config section in the ASH_CONFIG used)
      • scanner (default configuration in the plugin, if explicitly set)
  • Statistics calculation:
    • All statistics are calculated from the final aggregated SARIF report
    • Suppressed findings are counted separately and do not contribute to actionable findings
    • Scanner status is determined by comparing actionable findings to the threshold
Scanner Suppressed Critical High Medium Low Info Actionable Result Threshold
bandit 0 0 0 0 7090 0 0 PASSED MEDIUM (global)
cdk-nag 0 0 0 0 0 0 0 PASSED MEDIUM (global)
cfn-nag 0 0 0 0 0 0 0 MISSING MEDIUM (global)
checkov 0 0 0 0 0 0 0 PASSED MEDIUM (global)
detect-secrets 0 0 0 0 0 0 0 PASSED MEDIUM (global)
grype 0 0 0 0 0 0 0 MISSING MEDIUM (global)
npm-audit 0 0 0 0 0 0 0 PASSED MEDIUM (global)
opengrep 0 0 0 0 0 0 0 MISSING MEDIUM (global)
semgrep 3 0 0 0 0 0 0 PASSED MEDIUM (global)
syft 0 0 0 0 0 0 0 MISSING MEDIUM (global)

Report generated by Automated Security Helper (ASH) at 2026-11-19T03:11:58+00:00

@vawsgit vawsgit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ LGTM!

I found no blocking problems. I measured everything below on the PR head f63ee34 with PYTHONPATH set to that checkout's src. inspect.getfile(stickler) confirmed the path.

What I verified

  • Diff: git diff origin/dev...f63ee34 changes only tests/common/comparators/test_arabic_text.py (+297). origin/dev is still at 9fc70de, which is the merge base. The branch is not behind dev, so the pinned scores match what will merge.
  • New file: 84 passed on Python 3.10 (Unicode 13), 3.12 and 3.14 (Unicode 16). It also passed with pytest -n 4, so the tests do not depend on run order.
  • Full suite: after uv sync --extra llm --extra bert --frozen and uv run coverage run -m pytest, the result was 5541 passed, 3 skipped, 1 xfailed. This matches the PR body. ruff check and ruff format --check pass, and CI is green.
  • Mutation testing: I applied all 23 source changes listed in the PR body, one at a time. Each one made at least one new test fail. None behaved the same as the original code. For example:
    • removing NFC from NormalizedComparator failed test_only_normalized_matches_composed_and_decomposed_hamza (I also ran that one by hand);
    • rejecting non-ASCII digits in DateComparator failed 8 tests;
    • matching only ASCII and Arabic-Indic digits in the year-first check failed the Persian case.
  • Scores: I called each comparator directly and reproduced every score in the PR body. Examples:
    • diacritics: Levenshtein 1/2;
    • مُحَمَّدٌ/محمد: Levenshtein 4/9, ANLS* 0.0;
    • presentation forms and the lam-alef ligature: 0.0 on all five text comparators;
    • CLDR short date with right-to-left marks: 0.0, and 1.0 without the marks;
    • Solar Hijri ۱۴۰۳/۲/۲۵: 0.0;
    • phone numbers in Arabic-Indic and Persian digits: 1.0.
  • Code points: I named every non-ASCII character with unicodedata.name. Each string is the variant its test ID claims, for example U+0623 against U+0627 U+0654, U+06CC/U+06A9 against U+064A/U+0643, and U+FEE3 U+FEA4 U+FEE4 U+FEAA for the positional forms of محمد. The only invisible characters (RLM and ZWNJ) are written as escapes.
  • Locale and calendar claims: Babel gives 14‏/5‏/2024 for ar_EG, ar_SA and ar_AE, with marks after the day and the month. 1403/2/25 in the Solar Hijri calendar is 14 May 2024.
  • Housekeeping: the partial scores use pytest.approx. The file placement and name follow tests/README.md. No CHANGELOG entry is needed for a tests-only change, per RELEASING.md. Part of #591 is the right link, because the step that decides which results are gaps is still open.

Non-blocking (optional): test_identical_arabic_text_matches would pass for almost any comparator. It works as a baseline and needs no change.

@digalaviz
digalaviz merged commit f492830 into awslabs:dev Nov 19, 2026
10 checks passed
@digalaviz

Copy link
Copy Markdown
Contributor

Merged, thank you for the thorough tests and for the mutation checks in the description. #591 stays open for deciding which of these results are gaps.

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