Skip to content

Add TrimWhitespaceProcessor for text normalization #19

@AdityaTeltia

Description

@AdityaTeltia

Description of the feature request:

I propose creating a TrimWhitespaceProcessor and adding it to the genai_processors/contrib/ directory. This processor would remove leading and trailing whitespace from incoming text-based ProcessorParts, which is a common text normalization task.

Proposed API:

  • Location: genai_processors/contrib/trim_whitespace_processor.py
  • Class: TrimWhitespaceProcessor
  • Inherits from: PartProcessor
  • Logic:
    • If the part is text (is_text(part.mimetype)), it should apply .strip() to the text content.
    • If the part is not text, it should be yielded unchanged.

What problem are you trying to solve with this feature?

Raw text data often contains extraneous whitespace, which can lead to issues in downstream processing:

  1. Inconsistent Matching: Text-based lookups or comparisons can fail. For example, "COMPLETE" would not
    match "COMPLETE ".
  2. Tokenization Inefficiency: Language models may treat " text" and "text" as different tokens, leading
    to inconsistent outputs and wasted resources.
  3. Silent Bugs: Conditional logic that depends on exact string matches can fail unexpectedly.

Adding a TrimWhitespaceProcessor provides a simple, reusable component to build more robust and reliable text-processing pipelines.

Any other information you'd like to share?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions