Text Data Augmentation. A brief introduction to TextAttack and... | by Chetan Ambi | Towards Data Science

Data Augmentation is the process that enables us to increase the size of the training data without actually collecting the data. But why do we need more data? The answer is simple — the more data we have, the better the performance of the model.
Image data augmentation steps such as flipping, cropping, rotation, blurring, zooming, etc. helped tremendously in computer vision. Also, it is relatively easy to create augmented images but the same is not the case with Natural Language Processing (NLP) due to the complexities inherent in the language. (For example, we can not replace every word by its synonym, and even if we replace it, the meaning of the sentence might change completely). Based on my findings/research, I haven’t seen as much research around text data augmentation as image augmentation.
However, in this article, we will go thru 2 libraries TextAttack & Googletrans I have come across recently when I was trying augmentation for text data. So, let’s get started.
We will apply the augmentation techniques we are going to learn on these quotes of Simon Sinek.
“Leadership requires two things: a vision of the world that does not yet exist and the ability to communicate it.”
“The role of a leader is not to come up with all the great ideas. The role of a leader is to create an environment in which great ideas can happen”
- Simon Sinek
TextAttack is a Python framework for adversarial attacks, adversarial training, and data augmentation in NLP. In this article, we will focus only on data augmentation.
!pip install textattack
The textattack.Augmenter class provides six methods for data augmentation. You can find the detailed description of these methods in the official documentation.
WordNetAugmenterEmbeddingAugmenterCharSwapAugmenterEasyDataAugmenterCheckListAugmenterCLAREAugmenter
WordNetAugmenter
EmbeddingAugmenter
CharSwapAugmenter
EasyDataAugmenter
CheckListAugmenter
CLAREAugmenter
Let’s look at the data augmentation results using these four methods. Note that pct_words_to_swap=0.1, transformations_per_example=4 are passed to each of these methods by default. We can modify these default values as needed.
!pip install pyarrow==0.16.0

Collecting pyarrow==0.16.0
  Downloading https://files.pythonhosted.org/packages/00/d2/695bab1e1e7a4554b6dbd287d55cca096214bd441037058a432afd724bb1/pyarrow-0.16.0-cp36-cp36m-manylinux2014_x86_64.whl (63.1MB)
     |████████████████████████████████| 63.2MB 63kB/s 
Requirement already satisfied: six>=1.0.0 in /usr/local/lib/python3.6/dist-packages (from pyarrow==0.16.0) (1.15.0)
Requirement already satisfied: numpy>=1.14 in /usr/local/lib/python3.6/dist-packages (from pyarrow==0.16.0) (1.18.5)
Installing collected packages: pyarrow
  Found existing installation: pyarrow 0.14.1
    Uninstalling pyarrow-0.14.1:
      Successfully uninstalled pyarrow-0.14.1
Successfully installed pyarrow-0.16.0

!pip install textattack

Collecting textattack
  Downloading https://files.pythonhosted.org/packages/df/eb/bb9793f290f0ca40c725f15144171318c1b1e1952ee249cecb66ed5fe841/textattack-0.2.4-py3-none-any.whl (207kB)
     |████████████████████████████████| 215kB 2.7MB/s 
Collecting flair>=0.5.1
  Downloading https://files.pythonhosted.org/packages/60/8f/51d1f2eb5f9b09beb1e6858b4c174a087bc3b05893438e2cd3ffbf9c6e8b/flair-0.5.1-py3-none-any.whl (201kB)
     |████████████████████████████████| 204kB 8.0MB/s 
Requirement already satisfied: scikit-learn in /usr/local/lib/python3.6/dist-packages (from textattack) (0.22.2.post1)
Requirement already satisfied: tqdm in /usr/local/lib/python3.6/dist-packages (from textattack) (4.41.1)
Requirement already satisfied: pandas>=1.0.1 in /usr/local/lib/python3.6/dist-packages (from textattack) (1.0.5)
Collecting visdom
  Downloading https://files.pythonhosted.org/packages/c9/75/e078f5a2e1df7e0d3044749089fc2823e62d029cc027ed8ae5d71fafcbdc/visdom-0.1.8.9.tar.gz (676kB)
     |████████████████████████████████| 686kB 11.4MB/s 
Collecting wandb
  Downloading https://files.pythonhosted.org/packages/94/19/f8db9eff4b0173adf6dd2e8b0c3d8de0bfe10ec9ed63d247665980d82258/wandb-0.9.4-py2.py3-none-any.whl (1.4MB)
     |████████████████████████████████| 1.4MB 17.9MB/s 
Collecting tensorboardX
  Downloading https://files.pythonhosted.org/packages/af/0c/4f41bcd45db376e6fe5c619c01100e9b7531c55791b7244815bac6eac32c/tensorboardX-2.1-py2.py3-none-any.whl (308kB)
     |████████████████████████████████| 317kB 22.5MB/s 
Collecting lemminflect
  Downloading https://files.pythonhosted.org/packages/8d/c5/62e8dd0b6cbfea212cf55a2338838d85a819dbda9462ba53a415dcf19b86/lemminflect-0.2.1-py3-none-any.whl (769kB)
     |████████████████████████████████| 778kB 26.6MB/s 
Collecting tokenizers==0.8.1-rc1
  Downloading https://files.pythonhosted.org/packages/40/d0/30d5f8d221a0ed981a186c8eb986ce1c94e3a6e87f994eae9f4aa5250217/tokenizers-0.8.1rc1-cp36-cp36m-manylinux1_x86_64.whl (3.0MB)
     |████████████████████████████████| 3.0MB 37.0MB/s 
Collecting nlp
  Downloading https://files.pythonhosted.org/packages/09/e3/bcdc59f3434b224040c1047769c47b82705feca2b89ebbc28311e3764782/nlp-0.4.0-py3-none-any.whl (1.7MB)
     |████████████████████████████████| 1.7MB 33.8MB/s 
Requirement already satisfied: scipy==1.4.1 in /usr/local/lib/python3.6/dist-packages (from textattack) (1.4.1)
Requirement already satisfied: pyarrow<1.0 in /usr/local/lib/python3.6/dist-packages (from textattack) (0.16.0)
Requirement already satisfied: tensorflow>=2 in /usr/local/lib/python3.6/dist-packages (from textattack) (2.3.0)
Collecting terminaltables
  Downloading https://files.pythonhosted.org/packages/9b/c4/4a21174f32f8a7e1104798c445dacdc1d4df86f2f26722767034e4de4bff/terminaltables-3.1.0.tar.gz
Collecting language-tool-python
  Downloading https://files.pythonhosted.org/packages/d2/4a/95ee8562f40bb8e72f6da43f888e3577711cb49cf62ffedff2b6316d25b5/language_tool_python-2.3.1-py3-none-any.whl
Requirement already satisfied: editdistance in /usr/local/lib/python3.6/dist-packages (from textattack) (0.5.3)
Requirement already satisfied: torch in /usr/local/lib/python3.6/dist-packages (from textattack) (1.6.0+cu101)
Collecting transformers==3.0.2
  Downloading https://files.pythonhosted.org/packages/27/3c/91ed8f5c4e7ef3227b4119200fc0ed4b4fd965b1f0172021c25701087825/transformers-3.0.2-py3-none-any.whl (769kB)
     |████████████████████████████████| 778kB 37.4MB/s 
Requirement already satisfied: nltk in /usr/local/lib/python3.6/dist-packages (from textattack) (3.2.5)
Requirement already satisfied: filelock in /usr/local/lib/python3.6/dist-packages (from textattack) (3.0.12)
Collecting sentence-transformers>0.2.6
  Downloading https://files.pythonhosted.org/packages/ee/71/acfb3f1016f83d90590130dc2ee0d8cd36b005aa7afa45b465837b711070/sentence-transformers-0.3.3.tar.gz (65kB)
     |████████████████████████████████| 71kB 7.9MB/s 
Collecting lru-dict
  Downloading https://files.pythonhosted.org/packages/00/a5/32ed6e10246cd341ca8cc205acea5d208e4053f48a4dced2b1b31d45ba3f/lru-dict-1.1.6.tar.gz
Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from textattack) (1.18.5)
Requirement already satisfied: tensorflow-hub in /usr/local/lib/python3.6/dist-packages (from textattack) (0.8.0)
Collecting bert-score
  Downloading https://files.pythonhosted.org/packages/50/8c/70244d0f351176c9984643e2037edc18b9124491b47ad19191039a18d2bd/bert_score-0.3.5-py3-none-any.whl (52kB)
     |████████████████████████████████| 61kB 6.9MB/s 
Collecting langdetect
  Downloading https://files.pythonhosted.org/packages/56/a3/8407c1e62d5980188b4acc45ef3d94b933d14a2ebc9ef3505f22cf772570/langdetect-1.0.8.tar.gz (981kB)
     |████████████████████████████████| 983kB 43.8MB/s 
Collecting pytest>=5.3.2
  Downloading https://files.pythonhosted.org/packages/98/db/712bee56f9ab20373508a5a1c662e1db49b407dacf03b5224c6171ed0a3d/pytest-6.0.1-py3-none-any.whl (270kB)
     |████████████████████████████████| 276kB 44.2MB/s 
Collecting segtok>=1.5.7
  Downloading https://files.pythonhosted.org/packages/41/08/582dab5f4b1d5ca23bc6927b4bb977c8ff7f3a87a3b98844ef833e2f5623/segtok-1.5.10.tar.gz
Collecting sqlitedict>=1.6.0
  Downloading https://files.pythonhosted.org/packages/0f/1c/c757b93147a219cf1e25cef7e1ad9b595b7f802159493c45ce116521caff/sqlitedict-1.6.0.tar.gz
Requirement already satisfied: matplotlib>=2.2.3 in /usr/local/lib/python3.6/dist-packages (from flair>=0.5.1->textattack) (3.2.2)
Collecting mpld3==0.3
  Downloading https://files.pythonhosted.org/packages/91/95/a52d3a83d0a29ba0d6898f6727e9858fe7a43f6c2ce81a5fe7e05f0f4912/mpld3-0.3.tar.gz (788kB)
     |████████████████████████████████| 798kB 40.0MB/s 
Requirement already satisfied: python-dateutil>=2.6.1 in /usr/local/lib/python3.6/dist-packages (from flair>=0.5.1->textattack) (2.8.1)
Requirement already satisfied: gensim>=3.4.0 in /usr/local/lib/python3.6/dist-packages (from flair>=0.5.1->textattack) (3.6.0)
Collecting deprecated>=1.2.4
  Downloading https://files.pythonhosted.org/packages/76/a1/05d7f62f956d77b23a640efc650f80ce24483aa2f85a09c03fb64f49e879/Deprecated-1.2.10-py2.py3-none-any.whl
Requirement already satisfied: hyperopt>=0.1.1 in /usr/local/lib/python3.6/dist-packages (from flair>=0.5.1->textattack) (0.1.2)
Collecting bpemb>=0.2.9
  Downloading https://files.pythonhosted.org/packages/91/77/3f0f53856e86af32b1d3c86652815277f7b5f880002584eb30db115b6df5/bpemb-0.3.2-py3-none-any.whl
Requirement already satisfied: regex in /usr/local/lib/python3.6/dist-packages (from flair>=0.5.1->textattack) (2019.12.20)
Collecting sentencepiece!=0.1.92
  Downloading https://files.pythonhosted.org/packages/d4/a4/d0a884c4300004a78cca907a6ff9a5e9fe4f090f5d95ab341c53d28cbc58/sentencepiece-0.1.91-cp36-cp36m-manylinux1_x86_64.whl (1.1MB)
     |████████████████████████████████| 1.1MB 30.7MB/s 
Requirement already satisfied: tabulate in /usr/local/lib/python3.6/dist-packages (from flair>=0.5.1->textattack) (0.8.7)
Requirement already satisfied: joblib>=0.11 in /usr/local/lib/python3.6/dist-packages (from scikit-learn->textattack) (0.16.0)
Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.6/dist-packages (from pandas>=1.0.1->textattack) (2018.9)
Requirement already satisfied: requests in /usr/local/lib/python3.6/dist-packages (from visdom->textattack) (2.23.0)
Requirement already satisfied: tornado in /usr/local/lib/python3.6/dist-packages (from visdom->textattack) (5.1.1)
Requirement already satisfied: pyzmq in /usr/local/lib/python3.6/dist-packages (from visdom->textattack) (19.0.2)
Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from visdom->textattack) (1.15.0)
Collecting jsonpatch
  Downloading https://files.pythonhosted.org/packages/4f/d0/34b0f59ac08de9c1e07876cfecd80aec650600177b4bd445124c755499a7/jsonpatch-1.26-py2.py3-none-any.whl
Collecting torchfile
  Downloading https://files.pythonhosted.org/packages/91/af/5b305f86f2d218091af657ddb53f984ecbd9518ca9fe8ef4103a007252c9/torchfile-0.1.0.tar.gz
Collecting websocket-client
  Downloading https://files.pythonhosted.org/packages/4c/5f/f61b420143ed1c8dc69f9eaec5ff1ac36109d52c80de49d66e0c36c3dfdf/websocket_client-0.57.0-py2.py3-none-any.whl (200kB)
     |████████████████████████████████| 204kB 38.4MB/s 
Requirement already satisfied: pillow in /usr/local/lib/python3.6/dist-packages (from visdom->textattack) (7.0.0)
Collecting subprocess32>=3.5.3
  Downloading https://files.pythonhosted.org/packages/32/c8/564be4d12629b912ea431f1a50eb8b3b9d00f1a0b1ceff17f266be190007/subprocess32-3.5.4.tar.gz (97kB)
     |████████████████████████████████| 102kB 11.4MB/s 
Collecting watchdog>=0.8.3
  Downloading https://files.pythonhosted.org/packages/0e/06/121302598a4fc01aca942d937f4a2c33430b7181137b35758913a8db10ad/watchdog-0.10.3.tar.gz (94kB)
     |████████████████████████████████| 102kB 10.0MB/s 
Collecting docker-pycreds>=0.4.0
  Downloading https://files.pythonhosted.org/packages/f5/e8/f6bd1eee09314e7e6dee49cbe2c5e22314ccdb38db16c9fc72d2fa80d054/docker_pycreds-0.4.0-py2.py3-none-any.whl
Collecting GitPython>=1.0.0
  Downloading https://files.pythonhosted.org/packages/f9/1e/a45320cab182bf1c8656107b3d4c042e659742822fc6bff150d769a984dd/GitPython-3.1.7-py3-none-any.whl (158kB)
     |████████████████████████████████| 163kB 34.9MB/s 
Requirement already satisfied: PyYAML>=3.10 in /usr/local/lib/python3.6/dist-packages (from wandb->textattack) (3.13)
Requirement already satisfied: nvidia-ml-py3>=7.352.0 in /usr/local/lib/python3.6/dist-packages (from wandb->textattack) (7.352.0)
Collecting shortuuid>=0.5.0
  Downloading https://files.pythonhosted.org/packages/25/a6/2ecc1daa6a304e7f1b216f0896b26156b78e7c38e1211e9b798b4716c53d/shortuuid-1.0.1-py3-none-any.whl
Collecting configparser>=3.8.1
  Downloading https://files.pythonhosted.org/packages/4b/6b/01baa293090240cf0562cc5eccb69c6f5006282127f2b846fad011305c79/configparser-5.0.0-py3-none-any.whl
Requirement already satisfied: psutil>=5.0.0 in /usr/local/lib/python3.6/dist-packages (from wandb->textattack) (5.4.8)
Collecting gql==0.2.0
  Downloading https://files.pythonhosted.org/packages/c4/6f/cf9a3056045518f06184e804bae89390eb706168349daa9dff8ac609962a/gql-0.2.0.tar.gz
Collecting sentry-sdk>=0.4.0
  Downloading https://files.pythonhosted.org/packages/4b/23/811fcdfc9d67fea7e47c91dd553081218d53dda744c28384f4d2f69206c9/sentry_sdk-0.16.3-py2.py3-none-any.whl (110kB)
     |████████████████████████████████| 112kB 40.6MB/s 
Requirement already satisfied: Click>=7.0 in /usr/local/lib/python3.6/dist-packages (from wandb->textattack) (7.1.2)
Requirement already satisfied: protobuf>=3.8.0 in /usr/local/lib/python3.6/dist-packages (from tensorboardX->textattack) (3.12.4)
Requirement already satisfied: dataclasses; python_version < "3.7" in /usr/local/lib/python3.6/dist-packages (from nlp->textattack) (0.7)
Requirement already satisfied: dill in /usr/local/lib/python3.6/dist-packages (from nlp->textattack) (0.3.2)
Collecting xxhash
  Downloading https://files.pythonhosted.org/packages/f7/73/826b19f3594756cb1c6c23d2fbd8ca6a77a9cd3b650c9dec5acc85004c38/xxhash-2.0.0-cp36-cp36m-manylinux2010_x86_64.whl (242kB)
     |████████████████████████████████| 245kB 41.7MB/s 
Requirement already satisfied: astunparse==1.6.3 in /usr/local/lib/python3.6/dist-packages (from tensorflow>=2->textattack) (1.6.3)
Requirement already satisfied: opt-einsum>=2.3.2 in /usr/local/lib/python3.6/dist-packages (from tensorflow>=2->textattack) (3.3.0)
Requirement already satisfied: tensorboard<3,>=2.3.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow>=2->textattack) (2.3.0)
Requirement already satisfied: gast==0.3.3 in /usr/local/lib/python3.6/dist-packages (from tensorflow>=2->textattack) (0.3.3)
Requirement already satisfied: absl-py>=0.7.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow>=2->textattack) (0.9.0)
Requirement already satisfied: wheel>=0.26 in /usr/local/lib/python3.6/dist-packages (from tensorflow>=2->textattack) (0.34.2)
Requirement already satisfied: h5py<2.11.0,>=2.10.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow>=2->textattack) (2.10.0)
Requirement already satisfied: termcolor>=1.1.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow>=2->textattack) (1.1.0)
Requirement already satisfied: keras-preprocessing<1.2,>=1.1.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow>=2->textattack) (1.1.2)
Requirement already satisfied: tensorflow-estimator<2.4.0,>=2.3.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow>=2->textattack) (2.3.0)
Requirement already satisfied: grpcio>=1.8.6 in /usr/local/lib/python3.6/dist-packages (from tensorflow>=2->textattack) (1.30.0)
Requirement already satisfied: google-pasta>=0.1.8 in /usr/local/lib/python3.6/dist-packages (from tensorflow>=2->textattack) (0.2.0)
Requirement already satisfied: wrapt>=1.11.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow>=2->textattack) (1.12.1)
Requirement already satisfied: future in /usr/local/lib/python3.6/dist-packages (from torch->textattack) (0.16.0)
Requirement already satisfied: packaging in /usr/local/lib/python3.6/dist-packages (from transformers==3.0.2->textattack) (20.4)
Collecting sacremoses
  Downloading https://files.pythonhosted.org/packages/7d/34/09d19aff26edcc8eb2a01bed8e98f13a1537005d31e95233fd48216eed10/sacremoses-0.0.43.tar.gz (883kB)
     |████████████████████████████████| 890kB 36.1MB/s 
Requirement already satisfied: more-itertools>=4.0.0 in /usr/local/lib/python3.6/dist-packages (from pytest>=5.3.2->flair>=0.5.1->textattack) (8.4.0)
Collecting pluggy<1.0,>=0.12
  Downloading https://files.pythonhosted.org/packages/a0/28/85c7aa31b80d150b772fbe4a229487bc6644da9ccb7e427dd8cc60cb8a62/pluggy-0.13.1-py2.py3-none-any.whl
Requirement already satisfied: toml in /usr/local/lib/python3.6/dist-packages (from pytest>=5.3.2->flair>=0.5.1->textattack) (0.10.1)
Requirement already satisfied: py>=1.8.2 in /usr/local/lib/python3.6/dist-packages (from pytest>=5.3.2->flair>=0.5.1->textattack) (1.9.0)
Requirement already satisfied: iniconfig in /usr/local/lib/python3.6/dist-packages (from pytest>=5.3.2->flair>=0.5.1->textattack) (1.0.1)
Requirement already satisfied: attrs>=17.4.0 in /usr/local/lib/python3.6/dist-packages (from pytest>=5.3.2->flair>=0.5.1->textattack) (19.3.0)
Requirement already satisfied: importlib-metadata>=0.12; python_version < "3.8" in /usr/local/lib/python3.6/dist-packages (from pytest>=5.3.2->flair>=0.5.1->textattack) (1.7.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib>=2.2.3->flair>=0.5.1->textattack) (1.2.0)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.6/dist-packages (from matplotlib>=2.2.3->flair>=0.5.1->textattack) (2.4.7)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.6/dist-packages (from matplotlib>=2.2.3->flair>=0.5.1->textattack) (0.10.0)
Requirement already satisfied: smart-open>=1.2.1 in /usr/local/lib/python3.6/dist-packages (from gensim>=3.4.0->flair>=0.5.1->textattack) (2.1.0)
Requirement already satisfied: networkx in /usr/local/lib/python3.6/dist-packages (from hyperopt>=0.1.1->flair>=0.5.1->textattack) (2.4)
Requirement already satisfied: pymongo in /usr/local/lib/python3.6/dist-packages (from hyperopt>=0.1.1->flair>=0.5.1->textattack) (3.11.0)
Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests->visdom->textattack) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests->visdom->textattack) (1.24.3)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests->visdom->textattack) (2020.6.20)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests->visdom->textattack) (2.10)
Collecting jsonpointer>=1.9
  Downloading https://files.pythonhosted.org/packages/18/b0/a80d29577c08eea401659254dfaed87f1af45272899e1812d7e01b679bc5/jsonpointer-2.0-py2.py3-none-any.whl
Collecting pathtools>=0.1.1
  Downloading https://files.pythonhosted.org/packages/e7/7f/470d6fcdf23f9f3518f6b0b76be9df16dcc8630ad409947f8be2eb0ed13a/pathtools-0.1.2.tar.gz
Collecting gitdb<5,>=4.0.1
  Downloading https://files.pythonhosted.org/packages/48/11/d1800bca0a3bae820b84b7d813ad1eff15a48a64caea9c823fc8c1b119e8/gitdb-4.0.5-py3-none-any.whl (63kB)
     |████████████████████████████████| 71kB 7.6MB/s 
Collecting graphql-core<2,>=0.5.0
  Downloading https://files.pythonhosted.org/packages/b0/89/00ad5e07524d8c523b14d70c685e0299a8b0de6d0727e368c41b89b7ed0b/graphql-core-1.1.tar.gz (70kB)
     |████████████████████████████████| 71kB 6.5MB/s 
Requirement already satisfied: promise<3,>=2.0 in /usr/local/lib/python3.6/dist-packages (from gql==0.2.0->wandb->textattack) (2.3)
Requirement already satisfied: setuptools in /usr/local/lib/python3.6/dist-packages (from protobuf>=3.8.0->tensorboardX->textattack) (49.2.0)
Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in /usr/local/lib/python3.6/dist-packages (from tensorboard<3,>=2.3.0->tensorflow>=2->textattack) (1.7.0)
Requirement already satisfied: werkzeug>=0.11.15 in /usr/local/lib/python3.6/dist-packages (from tensorboard<3,>=2.3.0->tensorflow>=2->textattack) (1.0.1)
Requirement already satisfied: markdown>=2.6.8 in /usr/local/lib/python3.6/dist-packages (from tensorboard<3,>=2.3.0->tensorflow>=2->textattack) (3.2.2)
Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in /usr/local/lib/python3.6/dist-packages (from tensorboard<3,>=2.3.0->tensorflow>=2->textattack) (0.4.1)
Requirement already satisfied: google-auth<2,>=1.6.3 in /usr/local/lib/python3.6/dist-packages (from tensorboard<3,>=2.3.0->tensorflow>=2->textattack) (1.17.2)
Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.6/dist-packages (from importlib-metadata>=0.12; python_version < "3.8"->pytest>=5.3.2->flair>=0.5.1->textattack) (3.1.0)
Requirement already satisfied: boto3 in /usr/local/lib/python3.6/dist-packages (from smart-open>=1.2.1->gensim>=3.4.0->flair>=0.5.1->textattack) (1.14.33)
Requirement already satisfied: boto in /usr/local/lib/python3.6/dist-packages (from smart-open>=1.2.1->gensim>=3.4.0->flair>=0.5.1->textattack) (2.49.0)
Requirement already satisfied: decorator>=4.3.0 in /usr/local/lib/python3.6/dist-packages (from networkx->hyperopt>=0.1.1->flair>=0.5.1->textattack) (4.4.2)
Collecting smmap<4,>=3.0.1
  Downloading https://files.pythonhosted.org/packages/b0/9a/4d409a6234eb940e6a78dfdfc66156e7522262f5f2fecca07dc55915952d/smmap-3.0.4-py2.py3-none-any.whl
Requirement already satisfied: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.6/dist-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard<3,>=2.3.0->tensorflow>=2->textattack) (1.3.0)
Requirement already satisfied: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.6/dist-packages (from google-auth<2,>=1.6.3->tensorboard<3,>=2.3.0->tensorflow>=2->textattack) (0.2.8)
Requirement already satisfied: rsa<5,>=3.1.4; python_version >= "3" in /usr/local/lib/python3.6/dist-packages (from google-auth<2,>=1.6.3->tensorboard<3,>=2.3.0->tensorflow>=2->textattack) (4.6)
Requirement already satisfied: cachetools<5.0,>=2.0.0 in /usr/local/lib/python3.6/dist-packages (from google-auth<2,>=1.6.3->tensorboard<3,>=2.3.0->tensorflow>=2->textattack) (4.1.1)
Requirement already satisfied: botocore<1.18.0,>=1.17.33 in /usr/local/lib/python3.6/dist-packages (from boto3->smart-open>=1.2.1->gensim>=3.4.0->flair>=0.5.1->textattack) (1.17.33)
Requirement already satisfied: jmespath<1.0.0,>=0.7.1 in /usr/local/lib/python3.6/dist-packages (from boto3->smart-open>=1.2.1->gensim>=3.4.0->flair>=0.5.1->textattack) (0.10.0)
Requirement already satisfied: s3transfer<0.4.0,>=0.3.0 in /usr/local/lib/python3.6/dist-packages (from boto3->smart-open>=1.2.1->gensim>=3.4.0->flair>=0.5.1->textattack) (0.3.3)
Requirement already satisfied: oauthlib>=3.0.0 in /usr/local/lib/python3.6/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<3,>=2.3.0->tensorflow>=2->textattack) (3.1.0)
Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in /usr/local/lib/python3.6/dist-packages (from pyasn1-modules>=0.2.1->google-auth<2,>=1.6.3->tensorboard<3,>=2.3.0->tensorflow>=2->textattack) (0.4.8)
Requirement already satisfied: docutils<0.16,>=0.10 in /usr/local/lib/python3.6/dist-packages (from botocore<1.18.0,>=1.17.33->boto3->smart-open>=1.2.1->gensim>=3.4.0->flair>=0.5.1->textattack) (0.15.2)
Building wheels for collected packages: visdom, terminaltables, sentence-transformers, lru-dict, langdetect, segtok, sqlitedict, mpld3, torchfile, subprocess32, watchdog, gql, sacremoses, pathtools, graphql-core
  Building wheel for visdom (setup.py) ... done
  Created wheel for visdom: filename=visdom-0.1.8.9-cp36-none-any.whl size=655250 sha256=021ecf255f934787d40fdc3808b5cb6f59e53c7bdd18f1aa58119bb494c8230b
  Stored in directory: /root/.cache/pip/wheels/70/19/a7/6d589ed967f4dfefd33bc166d081257bd4ed0cb618dccfd62a
  Building wheel for terminaltables (setup.py) ... done
  Created wheel for terminaltables: filename=terminaltables-3.1.0-cp36-none-any.whl size=15356 sha256=11bd424019d47ef03b9b055f4ae0c5de6f5b953e14a30ef54218857a638c2613
  Stored in directory: /root/.cache/pip/wheels/30/6b/50/6c75775b681fb36cdfac7f19799888ef9d8813aff9e379663e
  Building wheel for sentence-transformers (setup.py) ... done
  Created wheel for sentence-transformers: filename=sentence_transformers-0.3.3-cp36-none-any.whl size=97299 sha256=d1d4f8047c37c1616c94c6f24f0e2ee327c3768200f4f13256bd6d306ccbcddc
  Stored in directory: /root/.cache/pip/wheels/75/d6/0a/cab163b21d0597cc1580bc344487b11ad405e0d1d314725f2b
  Building wheel for lru-dict (setup.py) ... done
  Created wheel for lru-dict: filename=lru_dict-1.1.6-cp36-cp36m-linux_x86_64.whl size=25864 sha256=9f0c1af722bacbb05e34202cf6d062f406cf508e65b623a56ba51c9a3747462e
  Stored in directory: /root/.cache/pip/wheels/b7/ef/06/fbdd555907a7d438fb33e4c8675f771ff1cf41917284c51ebf
  Building wheel for langdetect (setup.py) ... done
  Created wheel for langdetect: filename=langdetect-1.0.8-cp36-none-any.whl size=993193 sha256=f6806cc32d2d54c50fd8cd5eb9c4ee3634002c089e479143004248b6213c4324
  Stored in directory: /root/.cache/pip/wheels/8d/b3/aa/6d99de9f3841d7d3d40a60ea06e6d669e8e5012e6c8b947a57
  Building wheel for segtok (setup.py) ... done
  Created wheel for segtok: filename=segtok-1.5.10-cp36-none-any.whl size=25020 sha256=f3bb5a27e2e5f9411305807610b17562f95896e7cb1de79520e393f17153e09f
  Stored in directory: /root/.cache/pip/wheels/b4/39/f6/9ca1c5cabde964d728023b5751c3a206a5c8cc40252321fb6b
  Building wheel for sqlitedict (setup.py) ... done
  Created wheel for sqlitedict: filename=sqlitedict-1.6.0-cp36-none-any.whl size=14689 sha256=dadddd2a944c7e9e4b9e25deb4a0661d03f882ed71f84c73c5938900f5a02397
  Stored in directory: /root/.cache/pip/wheels/bd/57/d3/907c3ee02d35e66f674ad0106e61f06eeeb98f6ee66a6cc3fe
  Building wheel for mpld3 (setup.py) ... done
  Created wheel for mpld3: filename=mpld3-0.3-cp36-none-any.whl size=116679 sha256=083b76dbbce87fafde4618a05fb9f26a3c88856b4e01b9158b82fa0db216ac1d
  Stored in directory: /root/.cache/pip/wheels/c0/47/fb/8a64f89aecfe0059830479308ad42d62e898a3e3cefdf6ba28
  Building wheel for torchfile (setup.py) ... done
  Created wheel for torchfile: filename=torchfile-0.1.0-cp36-none-any.whl size=5712 sha256=e181fea9479102f59cc0172804beea0549594ba49f2b0e6a2e4d20316e732089
  Stored in directory: /root/.cache/pip/wheels/b1/c3/d6/9a1cc8f3a99a0fc1124cae20153f36af59a6e683daca0a0814
  Building wheel for subprocess32 (setup.py) ... done
  Created wheel for subprocess32: filename=subprocess32-3.5.4-cp36-none-any.whl size=6489 sha256=cee805c7291b55f24d0b4e4fcfa06915bfbe93e6bf9a4eaa9cab87633fd34ff2
  Stored in directory: /root/.cache/pip/wheels/68/39/1a/5e402bdfdf004af1786c8b853fd92f8c4a04f22aad179654d1
  Building wheel for watchdog (setup.py) ... done
  Created wheel for watchdog: filename=watchdog-0.10.3-cp36-none-any.whl size=73870 sha256=1a2896b9c4d89700d00421232ec98119340bbbf2bfb98daba4d4c947c5f33624
  Stored in directory: /root/.cache/pip/wheels/a8/1d/38/2c19bb311f67cc7b4d07a2ec5ea36ab1a0a0ea50db994a5bc7
  Building wheel for gql (setup.py) ... done
  Created wheel for gql: filename=gql-0.2.0-cp36-none-any.whl size=7630 sha256=f90f7f403b9d2af02e220bd61968c7f7d4aeeb52a756f6527e8c751662a4c743
  Stored in directory: /root/.cache/pip/wheels/ce/0e/7b/58a8a5268655b3ad74feef5aa97946f0addafb3cbb6bd2da23
  Building wheel for sacremoses (setup.py) ... done
  Created wheel for sacremoses: filename=sacremoses-0.0.43-cp36-none-any.whl size=893260 sha256=7a42b3f6968d9cd8a928f232911a1e4c188a1d737be201eb142410cba4c28641
  Stored in directory: /root/.cache/pip/wheels/29/3c/fd/7ce5c3f0666dab31a50123635e6fb5e19ceb42ce38d4e58f45
  Building wheel for pathtools (setup.py) ... done
  Created wheel for pathtools: filename=pathtools-0.1.2-cp36-none-any.whl size=8784 sha256=40e6d9de6b3e8584a9780536a67a3276ca90f8f63e13faa1e5cadcfaf2f336c5
  Stored in directory: /root/.cache/pip/wheels/0b/04/79/c3b0c3a0266a3cb4376da31e5bfe8bba0c489246968a68e843
  Building wheel for graphql-core (setup.py) ... done
  Created wheel for graphql-core: filename=graphql_core-1.1-cp36-none-any.whl size=104650 sha256=44a8e1a804ae3d4a17feb1b3ba836f04df2aa7d51acb2fe79ee72e8316ec2496
  Stored in directory: /root/.cache/pip/wheels/45/99/d7/c424029bb0fe910c63b68dbf2aa20d3283d023042521bcd7d5
Successfully built visdom terminaltables sentence-transformers lru-dict langdetect segtok sqlitedict mpld3 torchfile subprocess32 watchdog gql sacremoses pathtools graphql-core
ERROR: datascience 0.10.6 has requirement folium==0.2.1, but you'll have folium 0.8.3 which is incompatible.
Installing collected packages: langdetect, pluggy, pytest, segtok, sqlitedict, mpld3, deprecated, sentencepiece, bpemb, tokenizers, sacremoses, transformers, flair, jsonpointer, jsonpatch, torchfile, websocket-client, visdom, subprocess32, pathtools, watchdog, docker-pycreds, smmap, gitdb, GitPython, shortuuid, configparser, graphql-core, gql, sentry-sdk, wandb, tensorboardX, lemminflect, xxhash, nlp, terminaltables, language-tool-python, sentence-transformers, lru-dict, bert-score, textattack
  Found existing installation: pluggy 0.7.1
    Uninstalling pluggy-0.7.1:
      Successfully uninstalled pluggy-0.7.1
  Found existing installation: pytest 3.6.4
    Uninstalling pytest-3.6.4:
      Successfully uninstalled pytest-3.6.4
Successfully installed GitPython-3.1.7 bert-score-0.3.5 bpemb-0.3.2 configparser-5.0.0 deprecated-1.2.10 docker-pycreds-0.4.0 flair-0.5.1 gitdb-4.0.5 gql-0.2.0 graphql-core-1.1 jsonpatch-1.26 jsonpointer-2.0 langdetect-1.0.8 language-tool-python-2.3.1 lemminflect-0.2.1 lru-dict-1.1.6 mpld3-0.3 nlp-0.4.0 pathtools-0.1.2 pluggy-0.13.1 pytest-6.0.1 sacremoses-0.0.43 segtok-1.5.10 sentence-transformers-0.3.3 sentencepiece-0.1.91 sentry-sdk-0.16.3 shortuuid-1.0.1 smmap-3.0.4 sqlitedict-1.6.0 subprocess32-3.5.4 tensorboardX-2.1 terminaltables-3.1.0 textattack-0.2.4 tokenizers-0.8.1rc1 torchfile-0.1.0 transformers-3.0.2 visdom-0.1.8.9 wandb-0.9.4 watchdog-0.10.3 websocket-client-0.57.0 xxhash-2.0.0

from textattack.augmentation import WordNetAugmenter, EmbeddingAugmenter, EasyDataAugmenter, CharSwapAugmenter

textattack: First time running textattack: downloading remaining required packages.
textattack: Downloading NLTK required packages.

[nltk_data] Downloading package wordnet to /root/nltk_data...
[nltk_data]   Unzipping corpora/wordnet.zip.
[nltk_data] Downloading package averaged_perceptron_tagger to
[nltk_data]     /root/nltk_data...
[nltk_data]   Unzipping taggers/averaged_perceptron_tagger.zip.
[nltk_data] Downloading package universal_tagset to /root/nltk_data...
[nltk_data]   Unzipping taggers/universal_tagset.zip.
[nltk_data] Downloading package stopwords to /root/nltk_data...
[nltk_data]   Unzipping corpora/stopwords.zip.

wandb: WARNING W&B installed but not logged in.  Run `wandb login` or set the WANDB_API_KEY env variable.

Example 1:
text = "Leadership requires two things: a vision of the world that does not yet exist and the ability to communicate it."

aug = WordNetAugmenter()
aug.augment(text)

['Leadership requires two things: a imaginativeness of the world that does not yet survive and the ability to communicate it.']
aug = EmbeddingAugmenter()
aug.augment(text)

textattack: Downloading https://textattack.s3.amazonaws.com/word_embeddings/paragramcf.
100%|██████████| 481M/481M [00:09<00:00, 52.4MB/s]
textattack: Unzipping file /root/.cache/textattack/tmppu06z0jb.zip to /root/.cache/textattack/word_embeddings/paragramcf.
textattack: Successfully saved word_embeddings/paragramcf to cache.

['Leadership requires deux things: a vision of the world that does not yet exist and the capacities to communicate it.']
aug = EasyDataAugmenter()
aug.augment(text)

['Leadership requires two things: a vision of the world that does not yet be and the power to communicate it.',
 'Leadership requires two: a of the world that does not yet exist and the ability to communicate it.',
 'Leadership requires two things: a vision of the world that it communicate yet exist and the ability to not does.',
 'Leadership requires two live things: a vision of the world that withal does not yet exist and the ability to communicate it.']
aug = CharSwapAugmenter()
aug.augment(text)

['Leadership requires two things: a visoin of the worl that does not yet exist and the ability to communicate it.']
Example 2:
text = "The role of a leader is not to come up with all the great ideas. The role of a leader is to create an environment in which great ideas can happen."

aug = WordNetAugmenter()
aug.augment(text)

['The persona of a leader is not to come up with all the great theme. The role of a leader is to create an environment in which great ideas can hap.']
aug = EmbeddingAugmenter()
aug.augment(text)

['The role of a leader is not to come up with all the great ideas. The role of a leader is to creations an environment in which grand insights can happen.']
aug = EasyDataAugmenter()
aug.augment(text)

['The role of a leader is not to come up with all the great estimate. The function of a leader is to make an environment in which great ideas can happen.',
 'The role environment great leader is not to come up with all the a ideas. The role of a leader is to create an of in which great ideas happen can.',
 'The role of a leader is not to come up with all the great improving ideas. The role of make a leader is to be create an environment in which great ideas can happen.',
 'role of a leader is not to come up with all the great ideas. The role of a leader is create an environment in which great can happen.']
aug = CharSwapAugmenter()
aug.augment(text)

['The role of a Sleader is not to come up with all the great ideas. The role of a lXader is to create an environment in which great ides can happen.']
We can apply these methods to real-world data to increase the size of the data. The sample code is given below. Here the originaltrain dataframe is copied to the train_aug dataframe and then augmentation is applied on train_aug. And finally, train_aug is appended to the original train dataset.
train_aug = train.copy()from textattack.augmentation import EmbeddingAugmenteraug = EmbeddingAugmenter()train_aug['text'] = train_aug['text'].apply(lambda x: str(aug.augment(x)))train = train.append(train_copy, ignore_index=True)
Googletrans is built on top of Google Translate API. This uses Google Translate Ajax API for language detection and translation.
!pip install googletrans
The key parameters to translate() method are:
src: source language. Optional parameter as googletrans will detect it.
dest: destination language. Mandatory parameter.
text: the text to be translated from source language to the destination language. Mandatory parameter.
As we can see, the given text is first translated from English to Italian and then translated to back to English. During this back translation, as we can see, there is a slight change in the sentence between the original text and the back-translated text but the overall meaning of the sentence is still retained.
!pip install googletrans

Collecting googletrans
  Downloading https://files.pythonhosted.org/packages/71/3a/3b19effdd4c03958b90f40fe01c93de6d5280e03843cc5adf6956bfc9512/googletrans-3.0.0.tar.gz
Collecting httpx==0.13.3
  Downloading https://files.pythonhosted.org/packages/54/b4/698b284c6aed4d7c2b4fe3ba5df1fcf6093612423797e76fbb24890dd22f/httpx-0.13.3-py3-none-any.whl (55kB)
     |████████████████████████████████| 61kB 2.9MB/s 
Requirement already satisfied: chardet==3.* in /usr/local/lib/python3.6/dist-packages (from httpx==0.13.3->googletrans) (3.0.4)
Collecting rfc3986<2,>=1.3
  Downloading https://files.pythonhosted.org/packages/78/be/7b8b99fd74ff5684225f50dd0e865393d2265656ef3b4ba9eaaaffe622b8/rfc3986-1.4.0-py2.py3-none-any.whl
Collecting hstspreload
  Downloading https://files.pythonhosted.org/packages/45/c1/732c4f4dd2f1340388e36d6fd9ae601013e93595890497d3854f533307c6/hstspreload-2020.8.8-py3-none-any.whl (930kB)
     |████████████████████████████████| 931kB 7.9MB/s 
Collecting httpcore==0.9.*
  Downloading https://files.pythonhosted.org/packages/dd/d5/e4ff9318693ac6101a2095e580908b591838c6f33df8d3ee8dd953ba96a8/httpcore-0.9.1-py3-none-any.whl (42kB)
     |████████████████████████████████| 51kB 7.4MB/s 
Requirement already satisfied: certifi in /usr/local/lib/python3.6/dist-packages (from httpx==0.13.3->googletrans) (2020.6.20)
Collecting sniffio
  Downloading https://files.pythonhosted.org/packages/b3/82/4bd4b7d9c0d1dc0fbfbc2a1e00138e7f3ab85bc239358fe9b78aa2ab586d/sniffio-1.1.0-py3-none-any.whl
Requirement already satisfied: idna==2.* in /usr/local/lib/python3.6/dist-packages (from httpx==0.13.3->googletrans) (2.10)
Collecting h11<0.10,>=0.8
  Downloading https://files.pythonhosted.org/packages/5a/fd/3dad730b0f95e78aeeb742f96fa7bbecbdd56a58e405d3da440d5bfb90c6/h11-0.9.0-py2.py3-none-any.whl (53kB)
     |████████████████████████████████| 61kB 7.5MB/s 
Collecting h2==3.*
  Downloading https://files.pythonhosted.org/packages/25/de/da019bcc539eeab02f6d45836f23858ac467f584bfec7a526ef200242afe/h2-3.2.0-py2.py3-none-any.whl (65kB)
     |████████████████████████████████| 71kB 8.3MB/s 
Collecting contextvars>=2.1; python_version < "3.7"
  Downloading https://files.pythonhosted.org/packages/83/96/55b82d9f13763be9d672622e1b8106c85acb83edd7cc2fa5bc67cd9877e9/contextvars-2.4.tar.gz
Collecting hpack<4,>=3.0
  Downloading https://files.pythonhosted.org/packages/8a/cc/e53517f4a1e13f74776ca93271caef378dadec14d71c61c949d759d3db69/hpack-3.0.0-py2.py3-none-any.whl
Collecting hyperframe<6,>=5.2.0
  Downloading https://files.pythonhosted.org/packages/19/0c/bf88182bcb5dce3094e2f3e4fe20db28a9928cb7bd5b08024030e4b140db/hyperframe-5.2.0-py2.py3-none-any.whl
Collecting immutables>=0.9
  Downloading https://files.pythonhosted.org/packages/99/e0/ea6fd4697120327d26773b5a84853f897a68e33d3f9376b00a8ff96e4f63/immutables-0.14-cp36-cp36m-manylinux1_x86_64.whl (98kB)
     |████████████████████████████████| 102kB 9.1MB/s 
Building wheels for collected packages: googletrans, contextvars
  Building wheel for googletrans (setup.py) ... done
  Created wheel for googletrans: filename=googletrans-3.0.0-cp36-none-any.whl size=15736 sha256=ff7b2a4e87aa0b1a99d73d7fc65f520eda57c43d965be132208c22d2ed63b9b8
  Stored in directory: /root/.cache/pip/wheels/28/1a/a7/eaf4d7a3417a0c65796c547cff4deb6d79c7d14c2abd29273e
  Building wheel for contextvars (setup.py) ... done
  Created wheel for contextvars: filename=contextvars-2.4-cp36-none-any.whl size=7666 sha256=20946ee05dbf98289686f0fd61c06e941485fac64829c40a417616d44bc2efae
  Stored in directory: /root/.cache/pip/wheels/a5/7d/68/1ebae2668bda2228686e3c1cf16f2c2384cea6e9334ad5f6de
Successfully built googletrans contextvars
Installing collected packages: rfc3986, hstspreload, immutables, contextvars, sniffio, h11, hpack, hyperframe, h2, httpcore, httpx, googletrans
Successfully installed contextvars-2.4 googletrans-3.0.0 h11-0.9.0 h2-3.2.0 hpack-3.0.0 hstspreload-2020.8.8 httpcore-0.9.1 httpx-0.13.3 hyperframe-5.2.0 immutables-0.14 rfc3986-1.4.0 sniffio-1.1.0

from googletrans import Translator
translator = Translator()

Example 1:
origin_text = "Leadership requires two things: a vision of the world that does not yet exist and the ability to communicate it."
print(origin_text)

Leadership requires two things: a vision of the world that does not yet exist and the ability to communicate it.

# translate from English to Italian
text_trans = translator.translate(text=origin_text, dest='it').text
print(text_trans)

La leadership richiede due cose: una visione del mondo che ancora non esiste e la capacità di comunicarlo.

# translate back to Englisht from Italian
translator.translate(text=text_trans, dest='en').text

'Leadership requires two things: a view of the world that does not yet exist and the ability to communicate it.'
Example 2:
origin_text = "The role of a leader is not to come up with all the great ideas. The role of a leader is to create an environment in which great ideas can happen"
print(origin_text)

The role of a leader is not to come up with all the great ideas. The role of a leader is to create an environment in which great ideas can happen

# translate from English to Italian
text_trans = translator.translate(text=origin_text, dest='it').text
print(text_trans)

Il ruolo di un leader non è quello di trovare tutte le grandi idee. Il ruolo di un leader è creare un ambiente in cui possano nascere grandi idee

# translate back to English from Italian
translator.translate(text=text_trans, dest='en').text

'The role of a leader is not to find all the great ideas. The role of a leader is to create an environment where great ideas can arise'
We can apply this technique to real-world data. The sample code is given below. Here the original train dataframe is copied to tran_aug dataframe and then back-translation is applied on train_aug dataframe. And finally, train_aug is appended to the original train dataframe. Note that we are translating original text from English to Italian and then from Italian to English.
train_aug = train.copy()from googletrans import Translatortranslator = Translator()train_aug['text'] = train_aug['text'].apply(lambda x: translator.translate(translator.translate(x, dest='it').text, dest='en').text)train = train.append(train_aug, ignore_index=True)
Now you know how to make use of TextAttack and Googletrans libraries for your data science projects for text data augmentation.
To read more such interesting articles on Python and Data Science, subscribe to my blog www.pythonsimplified.com. You can also reach me on LinkedIn.