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.1.tar.gz (10.3 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.1-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file banglanlptoolkit-1.1.1.tar.gz.

File metadata

  • Download URL: banglanlptoolkit-1.1.1.tar.gz
  • Upload date:
  • Size: 10.3 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.1.tar.gz
Algorithm Hash digest
SHA256 ea6382eb96f4368813abfff2c4c128c708668bc8c00053289d1e7d4a94ba9b08
MD5 cbbc1da2c2d48655db1090a12486aee1
BLAKE2b-256 15428e7a36bb9a882bf32519122d88f6866206a52d85c61570507acd2eeeb0ec

See more details on using hashes here.

File details

Details for the file banglanlptoolkit-1.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for banglanlptoolkit-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b307f5933afc6798d4253b58e5e64d12b3382c3814302d10ad5356df9c890b1b
MD5 1c1ce0ccb037e973f1a5b3c743c32d49
BLAKE2b-256 c5b5236a43f150f274020205065ab3959f703a75fb5f1560b880b4be33f593cd

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