Curate TextProcess DataContent Processing

Text Cleaning

View as Markdown

Remove undesirable text such as improperly decoded Unicode characters, inconsistent line spacing, or excessive URLs from documents being pre-processed for your dataset using NeMo Curator.

One common issue in text datasets is improper Unicode character encoding, which can result in garbled or unreadable text, particularly with special characters like apostrophes, quotes, or diacritical marks. For example, the input sentence "The Mona Lisa doesn't have eyebrows." from a given document may not have included a properly encoded apostrophe ('), resulting in the sentence decoding as "The Mona Lisa doesn’t have eyebrows.".

NeMo Curator enables you to easily run this document through the default UnicodeReformatter module to detect and remove the unwanted text, or you can define your own custom Unicode text cleaner tailored to your needs.

How it Works

NeMo Curator provides the following modules for cleaning text:

  • UnicodeReformatter: Uses ftfy to fix broken Unicode characters. Modifies the “text” field of the dataset by default. The module accepts extensive configuration options for fine-tuning Unicode repair behavior. Please see the ftfy documentation for more information about parameters used by the UnicodeReformatter.
  • NewlineNormalizer: Uses regex to replace 3 or more consecutive newline characters in each document with only 2 newline characters.
  • UrlRemover: Uses regex to remove all URLs in each document.

You can use these modules individually or sequentially in a cleaning pipeline.


Usage

Consider the following example, which loads a dataset from a directory (books/), steps through each module in a cleaning pipeline, and outputs the processed dataset to cleaned_books/:

1from nemo_curator.core.client import RayClient
2from nemo_curator.pipeline import Pipeline
3from nemo_curator.stages.text.io.reader import JsonlReader
4from nemo_curator.stages.text.io.writer import JsonlWriter
5from nemo_curator.stages.text.modifiers import Modify
6from nemo_curator.stages.text.modifiers.string import UrlRemover, NewlineNormalizer
7from nemo_curator.stages.text.modifiers.unicode import UnicodeReformatter
8
9def main():
10 # Initialize Ray client
11 ray_client = RayClient()
12 ray_client.start()
13
14 # Create processing pipeline
15 pipeline = Pipeline(
16 name="text_cleaning_pipeline",
17 description="Clean text data using Unicode reformatter, newline normalizer, and URL remover"
18 )
19
20 # Add reader stage
21 pipeline.add_stage(JsonlReader(file_paths="books/"))
22
23 # Add processing stages
24 pipeline.add_stage(Modify(UnicodeReformatter()))
25 pipeline.add_stage(Modify(NewlineNormalizer()))
26 pipeline.add_stage(Modify(UrlRemover()))
27
28 # Add writer stage
29 pipeline.add_stage(JsonlWriter(path="cleaned_books/"))
30
31 # Execute pipeline
32 results = pipeline.run()
33
34 # Stop Ray client
35 ray_client.stop()
36
37if __name__ == "__main__":
38 main()

Custom Text Cleaner

You can create your own custom text cleaner by extending the DocumentModifier class. The implementation of UrlRemover demonstrates this approach:

1import re
2
3from nemo_curator.stages.text.modifiers import DocumentModifier
4
5URL_REGEX = re.compile(r"https?://\S+|www\.\S+", flags=re.IGNORECASE)
6
7class UrlRemover(DocumentModifier):
8 """
9 Removes all URLs in a document.
10 """
11
12 def __init__(self):
13 super().__init__()
14
15 def modify_document(self, text: str) -> str:
16 return URL_REGEX.sub("", text)

To create a custom text cleaner, inherit from the DocumentModifier class and implement the constructor and modify_document method.