Repository navigation
feat: Add TextCleaner component - #6997
Conversation
Pull Request Test Coverage Report for Build 7917717273Warning: 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
💛 - Coveralls |
| :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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
Let's expand the docstring to correctly specify the input and outputs, like we do here - https://github.com/deepset-ai/haystack-core-integrations/pull/346/files#diff-7bf9e971a50e4d69eff9ee4866eab2af1d5e2720deb4a49a629cec0e017b4be2R54
| combined_regex = "|".join(self._remove_regexps) | ||
| texts = [re.sub(combined_regex, "", text, flags=re.IGNORECASE) for text in texts] |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
| This is mostly useful to clean generators predictions before evaluation. | |
| This is mostly useful to clean generator predictions before evaluation. |
Related Issues
Proposed Changes:
Add
TextCleanercomponent. 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
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:.