|
| 1 | +.. _data-curator-text-cleaning: |
| 2 | + |
| 3 | +========================= |
| 4 | +Text Cleaning |
| 5 | +========================= |
| 6 | + |
| 7 | +-------------------- |
| 8 | +Overview |
| 9 | +-------------------- |
| 10 | +Use NeMo Curator's text cleaning modules to remove undesirable text such as improperly decoded unicode characters, inconsistent line spacing, or excessive URLs from documents being pre-processed for dataset. |
| 11 | + |
| 12 | +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. |
| 13 | + |
| 14 | +-------------------- |
| 15 | +Use Cases |
| 16 | +-------------------- |
| 17 | +* Fix improperly decoded Unicode characters from webpages. |
| 18 | +* Standardize document layout by removing excessive newlines. |
| 19 | +* Remove URLs in documents. |
| 20 | + |
| 21 | +-------------------- |
| 22 | +Modules |
| 23 | +-------------------- |
| 24 | +NeMo Curator provides the following modules for cleaning text: |
| 25 | + |
| 26 | +- ``UnicodeReformatter()``: Uses [ftfy](https://ftfy.readthedocs.io/en/latest/) to fix broken Unicode characters. Modifies the "text" field of the dataset by default. |
| 27 | +- ``NewlineNormalizer()``: Uses regex to replace 3 or more consecutive newline characters in each document with only 2 newline characters. |
| 28 | +- ``UrlRemover()``: Uses regex to remove all urls in each document. |
| 29 | + |
| 30 | +You can use these modules individually or sequentially in a cleaning pipeline. |
| 31 | + |
| 32 | +Consider the following example, which loads a dataset (`books.jsonl`), steps through each module in a cleaning pipeline, and outputs the processed dataset as `cleaned_books.jsonl`: |
| 33 | + |
| 34 | + |
| 35 | +.. code-block:: python |
| 36 | +
|
| 37 | + from nemo_curator import Sequential, Modify, get_client |
| 38 | + from nemo_curator.datasets import DocumentDataset |
| 39 | + from nemo_curator.modifiers import UnicodeReformatter, UrlRemover, NewlineNormalizer |
| 40 | +
|
| 41 | + def main(): |
| 42 | + client = get_client(cluster_type="cpu") |
| 43 | +
|
| 44 | + dataset = DocumentDataset.read_json("books.jsonl") |
| 45 | + cleaning_pipeline = Sequential([ |
| 46 | + Modify(UnicodeReformatter()), |
| 47 | + Modify(NewlineNormalizer()), |
| 48 | + Modify(UrlRemover()), |
| 49 | + ]) |
| 50 | +
|
| 51 | + cleaned_dataset = cleaning_pipeline(dataset) |
| 52 | +
|
| 53 | + cleaned_dataset.to_json("cleaned_books.jsonl") |
| 54 | +
|
| 55 | + if __name__ == "__main__": |
| 56 | + main() |
| 57 | +
|
| 58 | +You can also perform text cleaning operations using the CLI by running the `text_cleaning` command: |
| 59 | + |
| 60 | +.. code-block:: bash |
| 61 | +
|
| 62 | + text_cleaning \ |
| 63 | + --input-data-dir=/path/to/input/ \ |
| 64 | + --output-clean-dir=/path/to/output/ \ |
| 65 | + --normalize-newlines \ |
| 66 | + --remove-urls |
| 67 | +
|
| 68 | +By default, the CLI will only perform unicode reformatting. Adding the ``--normalize-newlines`` and ``--remove-urls`` options add the other text cleaning options. |
| 69 | + |
| 70 | +------------------------ |
| 71 | +Custom Text Cleaner |
| 72 | +------------------------ |
| 73 | +It's easy to write your own custom text cleaner. The implementation of ``UnicodeReformatter`` can be used as an example. |
| 74 | + |
| 75 | +.. code-block:: python |
| 76 | + import ftfy |
| 77 | +
|
| 78 | + from nemo_curator.modifiers import DocumentModifier |
| 79 | +
|
| 80 | +
|
| 81 | + class UnicodeReformatter(DocumentModifier): |
| 82 | + def __init__(self): |
| 83 | + super().__init__() |
| 84 | +
|
| 85 | + def modify_document(self, text: str) -> str: |
| 86 | + return ftfy.fix_text(text) |
| 87 | +
|
| 88 | +Simply define a new class that inherits from ``DocumentModifier`` and define the constructor and ``modify_text`` method. |
| 89 | +Also, like the ``DocumentFilter`` class, ``modify_document`` can be annotated with ``batched`` to take in a pandas series of documents instead of a single document. |
| 90 | +See the :ref:`document filtering page <data-curator-qualityfiltering>` for more information. |
| 91 | + |
| 92 | +--------------------------- |
| 93 | +Additional Resources |
| 94 | +--------------------------- |
| 95 | +* `Single GPU Tutorial <https://github.com/NVIDIA/NeMo-Curator/blob/main/tutorials/single_node_tutorial/single_gpu_tutorial.ipynb>`_ |
| 96 | +* `ftfy <https://ftfy.readthedocs.io/en/latest/>`_ |
| 97 | +* `Refined Web Paper <https://arxiv.org/abs/2306.01116>`_ |
| 98 | +* `Nemotron-CC Paper <https://arxiv.org/abs/2412.02595>`_ |
0 commit comments