or run

tessl search
Log in

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/python-bidi@0.6.x
tile.json

tessl/pypi-python-bidi

tessl install tessl/pypi-python-bidi@0.6.0

Python Bidi layout wrapping the Rust crate unicode-bidi

Agent Success

Agent success rate when using this tile

93%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.09x

Baseline

Agent success rate without this tile

85%

task.mdevals/scenario-4/

Text Direction Analyzer

Build a text direction analyzer that processes multilingual text snippets and determines their base paragraph direction. The analyzer should be able to identify whether a given text is primarily left-to-right (LTR) or right-to-left (RTL).

Requirements

Implement a module that provides the following functionality:

1. Direction Detection Function

Create a function analyze_text_direction(text: str) -> str that:

  • Takes a string of text as input
  • Returns "LTR" if the text has a left-to-right base direction
  • Returns "RTL" if the text has a right-to-left base direction
  • Handles empty strings by returning "LTR" as the default

2. Batch Analysis Function

Create a function analyze_batch(texts: list[str]) -> dict[str, int] that:

  • Takes a list of text strings as input
  • Returns a dictionary with counts: {"LTR": <count>, "RTL": <count>}
  • The counts should reflect how many texts are LTR vs RTL

3. Direction Classification Function

Create a function classify_by_direction(texts: list[str]) -> dict[str, list[str]] that:

  • Takes a list of text strings as input
  • Returns a dictionary with two keys: "LTR" and "RTL"
  • Each key maps to a list of texts that have that base direction
  • Preserves the original order of texts within each category

Implementation Notes

  • The analyzer should determine direction based on the first strong directional character in the text
  • Latin characters (English, etc.) are LTR
  • Hebrew and Arabic characters are RTL
  • Numbers and punctuation are neutral and don't determine direction
  • When no strong directional character is present, default to LTR

Test Cases { .test }

Your implementation should pass these test cases:

# test_direction_analyzer.py

def test_ltr_text():
    """Test detection of left-to-right text"""
    assert analyze_text_direction("Hello World") == "LTR"
    assert analyze_text_direction("123 numbers") == "LTR"

def test_rtl_text():
    """Test detection of right-to-left text"""
    assert analyze_text_direction("שלום") == "RTL"
    assert analyze_text_direction("مرحبا") == "RTL"

def test_mixed_text():
    """Test mixed text - first strong character determines direction"""
    assert analyze_text_direction("Hello שלום") == "LTR"
    assert analyze_text_direction("שלום Hello") == "RTL"

def test_batch_analysis():
    """Test batch counting"""
    texts = ["Hello", "שלום", "World", "مرحبا"]
    result = analyze_batch(texts)
    assert result["LTR"] == 2
    assert result["RTL"] == 2

def test_classify_by_direction():
    """Test classification grouping"""
    texts = ["Hello", "שלום", "World", "مرحبا"]
    result = classify_by_direction(texts)
    assert result["LTR"] == ["Hello", "World"]
    assert result["RTL"] == ["שלום", "مرحبا"]

Dependencies { .dependencies }

python-bidi { .dependency }

Provides bidirectional text processing support.