-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[LLM pipeline] Add normalize text component #246
Merged
RobbeSneyders
merged 4 commits into
ml6team:main
from
mrchtr:feature/text_normalization_rebased
Jul 5, 2023
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
FROM --platform=linux/amd64 python:3.8-slim | ||
|
||
## System dependencies | ||
RUN apt-get update && \ | ||
apt-get upgrade -y && \ | ||
apt-get install git -y | ||
|
||
# install requirements | ||
COPY requirements.txt / | ||
RUN pip3 install --no-cache-dir -r requirements.txt | ||
|
||
# Set the working directory to the component folder | ||
WORKDIR /component/src | ||
|
||
# Copy over src-files | ||
COPY src/ . | ||
|
||
ENTRYPOINT ["python", "main.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: Normalize text. | ||
description: A component that normalizes text. | ||
image: ghcr.io/ml6team/text_normalization:latest | ||
|
||
consumes: | ||
text: | ||
fields: | ||
data: | ||
type: string | ||
|
||
args: | ||
apply_nfc: | ||
description: If true apply nfc normalization | ||
type: bool | ||
do_lowercase: | ||
description: If true apply lowercasing | ||
type: bool | ||
characters_to_remove: | ||
description: List of characters which will be removed, e.g. [?,.!,@#%] | ||
type: list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
git+https://github.com/ml6team/fondant.git@main | ||
NielsRogge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pyarrow>=7.0 | ||
gcsfs==2023.4.00 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
"""A component that normalizes text.""" | ||
import logging | ||
import re | ||
import unicodedata | ||
from typing import List | ||
|
||
import pandas as pd | ||
|
||
from fondant.component import PandasTransformComponent | ||
from fondant.logger import configure_logging | ||
|
||
configure_logging() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is outdated, please rebase on / merge with main. |
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class TextNormalizationComponent(PandasTransformComponent): | ||
"""Component that normalizes text.""" | ||
|
||
def setup(self, *, apply_nfc: bool, do_lowercase: bool, characters_to_remove: List[str]): | ||
self.apply_nfc = apply_nfc | ||
self.do_lowercase = do_lowercase | ||
self.characters_to_remove = characters_to_remove | ||
|
||
@staticmethod | ||
def _do_nfc_normalization(text: str): | ||
"""Apply nfc normalization to the text of the dataframe.""" | ||
return unicodedata.normalize("NFC", text) | ||
|
||
@staticmethod | ||
def _remove_patterns(regex_patterns: List[str], text: str): | ||
"""Remove each regex pattern in the provided string.""" | ||
for pattern in regex_patterns: | ||
text = re.sub(pattern, "", text) | ||
return text | ||
|
||
def transform(self, dataframe: pd.DataFrame) -> pd.DataFrame: | ||
""" | ||
Apply normalization transformations. The component is capable of: | ||
- NFC normalization | ||
- Lowercasing | ||
- Removing of regex patterns. | ||
|
||
Args: | ||
dataframe: Pandas dataframe. | ||
|
||
Returns: | ||
Pandas dataframe | ||
""" | ||
if self.apply_nfc: | ||
dataframe["text"]["data"].apply(lambda x: self._do_nfc_normalization(x)) | ||
|
||
if self.do_lowercase: | ||
dataframe["text"]["data"].apply(lambda x: x.lower()) | ||
|
||
if len(self.characters_to_remove) > 0: | ||
dataframe["text"]["data"].apply( | ||
lambda x: self._remove_patterns( | ||
self.characters_to_remove, x, | ||
), | ||
) | ||
|
||
return dataframe | ||
|
||
|
||
if __name__ == "__main__": | ||
component = TextNormalizationComponent.from_args() | ||
component.run() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned in chat, agree with consistent naming, but would actually prefer
text_normalization
here as it will group all text components alphabetically.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm not necessarily agree on that one, we can group them ourselves in the docs?
Would prefer to have verb + noun for all our components