Skip to content

feat: Add TextCleaner component - #6997

Merged
silvanocerza merged 3 commits into
mainfrom
text-cleaner
Feb 15, 2024
Merged

silvanocerza merged 3 commits into
mainfrom
text-cleaner

Conversation

@silvanocerza

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

Add TextCleaner component. This is mostly useful between a generator and an evaluator to cleanup the generated LLM response.

How did you test it?

I added unit tests.

Notes for the reviewer

This stems from feedback received in PR #6980.

Checklist

@silvanocerza silvanocerza self-assigned this Feb 15, 2024
@silvanocerza
silvanocerza requested review from a team as code owners February 15, 2024 09:58
@silvanocerza
silvanocerza requested review from dfokina, shadeMe and vblagoje and removed request for a team and vblagoje February 15, 2024 09:58
@coveralls

coveralls commented Feb 15, 2024 •

Copy link
Copy Markdown
Collaborator

Pull Request Test Coverage Report for Build 7917717273

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.1%) to 89.156%

Totals Coverage Status
Change from base Build 7904082418: 0.1%
Covered Lines: 4966
Relevant Lines: 5570

💛 - Coveralls

Comment on lines +26 to +34
:param remove_regexps: A list of regular expressions. If provided, it removes substrings
matching these regular expressions from the text. Defaults to None.
:type remove_regexps: Optional[List[str]], optional
:param convert_to_lowercase: If True, converts all characters to lowercase. Defaults to False.
:type convert_to_lowercase: bool, optional
:param remove_punctuation: If True, removes punctuation from the text. Defaults to False.
:type remove_punctuation: bool, optional
:param remove_numbers: If True, removes numerical digits from the text. Defaults to False.
:type remove_numbers: bool, optional

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.

Superfluous annotations for types in the docstring.

@component.output_types(texts=List[str])
def run(self, texts: List[str]) -> Dict[str, Any]:
"""
Run the TextCleaner on the given list of strings.

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.

Comment on lines +47 to +48
combined_regex = "|".join(self._remove_regexps)
texts = [re.sub(combined_regex, "", text, flags=re.IGNORECASE) for text in texts]

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.

We should be able to compile the combined reg exps into a single RegEx instance in the ctor and pass it to re.sub, I think?

texts = [text.lower() for text in texts]

if self._remove_punctuation:
translator = str.maketrans("", "", string.punctuation)

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.

Can we cache the translator instance?

- |
Add `TextCleaner` Component to clean list of strings. It can remove substrings matching a list of regular expressions,
convert text to lowercase, remove punctuation, and remove numbers.
This is mostly useful to clean generators predictions before evaluation.

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.

Suggested change
This is mostly useful to clean generators predictions before evaluation.
This is mostly useful to clean generator predictions before evaluation.

@shadeMe shadeMe 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants