Skip to main content

Toolkits for text processing and augmentation for Bangla NLP

Project description

Bangla NLP Toolkit

Created by A F M Mahfuzul Kabir
mahfuzulkabir.com
https://www.linkedin.com/in/mahfuzulkabir \

Installation

Install the requirements first with:

pip install -r requirements.txt

install the package with

pip install banglanlptoolkit

Introduction

This package contains several toolkits for Bangla NLP text processing and augmentation. The available tools are listed below.

  • Bangla Text Normalizer
  • Bangla Punctuation Generator
  • Bangla Text Augmentation

Bangla Text Normalizer

The package uses two normalization toolkits for Bangla text processing. The unicode normalizer is used from here. The other normalizer is specifically used for BanglaT5 translation module and taken from here.

Use:

from banglanlptoolkit import BnNLPNormalizer
normalizer = BnNLPNormalizer()

normalizer.normalize_bn(['পাশে অবস্থিত সংক্ষিপ্ত পূর্ব-পশ্চিম অভিমুখি অনিয়মিত অর্ধবৃত্তাকার সড়ক।'])

You can also use only the unicode normalizer

from banglanlptoolkit import BnNLPNormalizer
normalizer = BnNLPNormalizer()

normalizer.unicode_normalize(['পাশে অবস্থিত সংক্ষিপ্ত পূর্ব-পশ্চিম অভিমুখি অনিয়মিত অর্ধবৃত্তাকার সড়ক।'])

To allow English, change the code as below. By default, the normalizer module deletes any English words or pronunciations present. You can also set the module to translate English words to Bengali by changing translate_en attribute to True.

normalizer = BnNLPNormalizer(allow_en=True, translate_en=True)

Bangla Punctuation Generator

The package has one punctuation generation model for Bangla. The model was used from this notebook. I currently have this model in my huggingface for ease of use without any token. You can replace with any model of your like if you want.

Use:

from banglanlptoolkit import BanglaPunctuation

punct_agent = BanglaPunctuation()
print(punct_agent.add_punctuation(raw_text = 'আমার নাম কবির আপনাকে ধন্যবাদ আমার প্যাকেজ ব্যবহার করার জন্য'))

Bangla Text Augmentation

The package uses three kind of text augmentation techniques.

  • Bangla Token Replacement
  • Back Translation
  • Bangla Paraphrasing

The token replacement method uses fill-mask method to replace random tokens from a sentence and then replace them. The package uses BanglishBERT Generator model by CSEBUETNLP for this task. The model can be found in here.

The back translation method translates the sentences from Bangla to English and then to Bangla again. The package uses bn-en and en-bn models of BanglaT5 by CSEBUETNLP for this task. The models can be found here: bn2en, en2bn.

The paraphrasing toolkit uses Bangla paraphrase model of BanglaT5 by CSEBUETNLP. The model can be found in here.

Use:

from banglanlptoolkit.BanglaAugmentation import AugmentationBangla
augmentations = AugmentationBangla()

test_data=['পাশে অবস্থিত একটি সংক্ষিপ্ত পূর্ব-পশ্চিম অভিমুখি অনিয়মিত অর্ধবৃত্তাকার সড়ক।',
            'সড়কটি অপর অঙ্গরাজ্য সড়ক ৭৯ হতে উদ্ভুত হয়ে বাক-আই হ্রদের সমান্তরালে থেকে পুনরায় একই সড়কে মিশেছে।',
            'এসআর ৩৬০ সড়কের বেশিরভাগ অংশই ফেয়ারফিল্ড কাউন্টিতে, পাশাপাশি লিকিং কাউন্টিতেও এর কিছু অংশ রয়েছে।',
            'এটি বাকআই হ্রদের উত্তর তীরের একটি অংশের সাথে সমান্তরালে']

augmentations.Unmasking(test_data)
augmentations.BackTranslation(test_data)
augmentations.ParaPhrase(test_data)

Bangla Sequence Classification and Sequence to Sequence Data Augmentation

By using the methods mentioned and explained above, both sequence classification and sequence to sequence augmentation toolkit takes a dataframe as input and returns a dictionary of augmented data.

Use:

from banglanlptoolkit import SequenceClassificationAug
seq2seq = SequenceClassificationAug(allow_en=True, translate_en=False, punct_replacement_token=None)
seq2seq = Seq2SeqAug(allow_en=True,translate_en=False,punct_replacement_token=None)

The attributes allow_en and translate_en are used during normalization and punct_replacement allows the user to replace punctuations to any character of his choice. If set to None, the punctuations will not be replaced at all.

For sequence classification augmentation use like this.

import pandas as pd

test_data=pd.DataFrame({
    'sentence':['পাশে অবস্থিত একটি সংক্ষিপ্ত পূর্ব-পশ্চিম অভিমুখি অনিয়মিত অর্ধবৃত্তাকার সড়ক।',
                'সড়কটি অপর অঙ্গরাজ্য সড়ক ৭৯ হতে উদ্ভুত হয়ে বাক-আই হ্রদের সমান্তরালে থেকে পুনরায় একই সড়কে মিশেছে।',
                'এসআর ৩৬০ সড়কের বেশিরভাগ অংশই ফেয়ারফিল্ড কাউন্টিতে, পাশাপাশি লিকিং কাউন্টিতেও এর কিছু অংশ রয়েছে।',
                'এটি বাকআই হ্রদের উত্তর তীরের একটি অংশের সাথে সমান্তরালে'],
    'label':[0,1,2,3]})


seq2seq.BnAugSeqClassification(df=test_data,iters=1)

For sequence to sequence augmentation use like this.

test_data=pd.DataFrame({
    'sentence1':['পাশে অবস্থিত একটি সংক্ষিপ্ত পূর্ব-পশ্চিম অভিমুখি অনিয়মিত অর্ধবৃত্তাকার সড়ক।',
                'সড়কটি অপর অঙ্গরাজ্য সড়ক ৭৯ হতে উদ্ভুত হয়ে বাক-আই হ্রদের সমান্তরালে থেকে পুনরায় একই সড়কে মিশেছে।',
                'এসআর ৩৬০ সড়কের বেশিরভাগ অংশই ফেয়ারফিল্ড কাউন্টিতে, পাশাপাশি লিকিং কাউন্টিতেও এর কিছু অংশ রয়েছে।',
                'এটি বাকআই হ্রদের উত্তর তীরের একটি অংশের সাথে সমান্তরালে'],
            
    'sentence2':['পাশে অবস্থিত একটি সংক্ষিপ্ত পূর্ব-পশ্চিম অভিমুখি অনিয়মিত অর্ধবৃত্তাকার সড়ক।',
                'সড়কটি অপর অঙ্গরাজ্য সড়ক ৭৯ হতে উদ্ভুত হয়ে বাক-আই হ্রদের সমান্তরালে থেকে পুনরায় একই সড়কে মিশেছে।',
                'এসআর ৩৬০ সড়কের বেশিরভাগ অংশই ফেয়ারফিল্ড কাউন্টিতে, পাশাপাশি লিকিং কাউন্টিতেও এর কিছু অংশ রয়েছে।',
                'এটি বাকআই হ্রদের উত্তর তীরের একটি অংশের সাথে সমান্তরালে']
                })

seq2seq.BnAugSeq2Seq(df=test_data,iters=1)

Inspired from

If you use this package, please don't forget to cite the links and papers mentioned.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

banglanlptoolkit-1.1.4.dev0.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

banglanlptoolkit-1.1.4.dev0-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file banglanlptoolkit-1.1.4.dev0.tar.gz.

File metadata

  • Download URL: banglanlptoolkit-1.1.4.dev0.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for banglanlptoolkit-1.1.4.dev0.tar.gz
Algorithm Hash digest
SHA256 3b62f3bb0056044f352951071da6e70b4da44fb4e1c2f6d47c0599d7756a7165
MD5 9b98edfd5646d67c4d00cce3e1e92b18
BLAKE2b-256 05203e04293b76676a372e3f89c520a0b228ef62af98e4addce76234f3b73703

See more details on using hashes here.

File details

Details for the file banglanlptoolkit-1.1.4.dev0-py3-none-any.whl.

File metadata

File hashes

Hashes for banglanlptoolkit-1.1.4.dev0-py3-none-any.whl
Algorithm Hash digest
SHA256 e8c2a7fee23ab10d5f38afea7df58ab14c169802a7fb259ad73a46065b6f9096
MD5 fc4c6dcef4471a55811a507ebbb2c094
BLAKE2b-256 ca45de3069f83e12dda3d64736e207a6c259c475fbc047ad0e709340925625fc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page