bangla-text-processing-kit
A python tool kit for processing Bangla texts.
How to use
Installing
There are three installation options of the bkit package. These are:
bkit
: The most basic version of bkit with the normalization, cleaning and tokenization capabilities.
pip install bkit
bkit[lemma]
: Everything in the basic version plus lemmatization capability.
pip install bkit[lemma]
bkit[all]
: Everything that are available in bkit including normalization, cleaning, tokenization, lemmatization, NER, POS and shallow parsing.
pip install bkit[all]
Checking text
bkit.utils.is_bangla(text) -> bool
: Checks if text contains only Bangla characters, digits, spaces, punctuations and some symbols. Returns true if so, else return false.
bkit.utils.is_digit(text) -> bool
: Checks if text contains only Bangla digit characters. Returns true if so, else return false.
bkit.utils.contains_digit(text, check_english_digits) -> bool
: Checks if text contains any digits. By default checks only Bangla digits. Returns true if so, else return false.
bkit.utils.contains_bangla(text) -> bool
: Checks if text contains any Bangla character. Returns true if so, else return false.
Transforming text
Text transformation includes the normalization and cleaning procedures. To transform text, use the bkit.transform
module. Supported functionalities are:
Normalizer
This module normalize Bangla text using the following steps:
import bkit
text = 'অাামাব় । '
print(list(text))
normalizer = bkit.transform.Normalizer(
normalize_characters=True,
normalize_zw_characters=True,
normalize_halant=True,
normalize_vowel_kar=True,
normalize_punctuation_spaces=True
)
clean_text = normalizer(text)
print(clean_text, list(clean_text))
Character Normalization
This module performs character normalization in Bangla text. It performs nukta normalization, Assamese normalization, Kar normalization, legacy character normalization and Punctuation normalization sequentially.
import bkit
text = 'আমাব়'
print(list(text))
text = bkit.transform.normalize_characters(text)
print(list(text))
Punctuation space normalization
Normalizes punctuation spaces i.e. adds necessary spaces before or after specific punctuations, also removes if necessary.
import bkit
text = 'রহিম(২৩)এ কথা বলেন ।তিনি ( রহিম ) আরও জানান, ১,২৪,৩৫,৬৫৪.৩২৩ কোটি টাকা ব্যায়ে...'
clean_text = bkit.transform.normalize_punctuation_spaces(text)
print(clean_text)
Zero width characters normalization
There are two zero-width characters. These are Zero Width Joiner (ZWJ) and Zero Width Non Joiner (ZWNJ) characters. Generally ZWNJ is not used with Bangla texts and ZWJ joiner is used with র
only. So, these characters are normalized based on these intuitions.
import bkit
text = 'র্যাকেট'
print(f"text: {text} \t Characters: {list(text)}")
clean_text = bkit.transform.normalize_zero_width_chars(text)
print(f"text: {clean_text} \t Characters: {list(clean_text)}")
Halant (হসন্ত) normalization
This function normalizes halant (হসন্ত) [0x09CD
] in Bangla text. While using this function, it is recommended to normalize the zero width characters at first, e.g. using the bkit.transform.normalize_zero_width_chars()
function.
During the normalization it also handles the ত্ -> ৎ
conversion. For a valid conjunct letter (যুক্তবর্ণ) where 'ত' is the former character, can take one of 'ত', 'থ', 'ন', 'ব', 'ম', 'য', and 'র' as the next character. The conversion is perform based on this intuition.
During the halant normalization, the following cases are handled.
- Remove any leading and tailing halant of a word and/or text.
- Replace two or more consecutive occurrences of halant by a single halant.
- Remove halant between any characters that do not follow or precede a halant character. Like a halant that follows or precedes a vowel, kar, য়, etc will be removed.
- Remove multiple fola (multiple ref, ro-fola and jo-fola)
import bkit
text = 'আসন্্্ন আসফাকুল্লাহ্ আলবত্ আলবত্ র্যাব ই্সি'
print(list(text))
clean_text = bkit.transform.normalize_zero_width_chars(text)
clean_text = bkit.transform.normalize_halant(clean_text)
print(clean_text, list(clean_text))
Kar ambiguity
Normalizes kar ambiguity with vowels, ঁ, ং, and ঃ. It removes any kar that is preceded by a vowel or consonant diacritics like: আা
will be normalized to আ
. In case of consecutive occurrence of kars like: কাাাী
, only the first kar will be kept like: কা
.
import bkit
text = 'অংশইে অংশগ্রহণইে আাারো এখনওো আলবার্তোে সাধুু কাাাী'
print(list(text))
clean_text = bkit.transform.normalize_kar_ambiguity(text)
print(clean_text, list(clean_text))
Clean text
Clean text using the following steps sequentially:
import bkit
text = '<a href=some_URL>বাংলাদেশ</a>\nবাংলাদেশের আয়তন ১.৪৭ লক্ষ কিলোমিটার!!!'
clean_text = bkit.transform.clean_text(text)
print(clean_text)
Clean punctuations
Remove punctuations with the given replace_with
character/string.
import bkit
text = 'আমরা মাঠে ফুটবল খেলতে পছন্দ করি!'
clean_text = bkit.transform.clean_punctuations(text)
print(clean_text)
clean_text = bkit.transform.clean_punctuations(text, replace_with=' PUNC ')
print(clean_text)
Clean digits
Remove any bangla digit from text by replacing with the given replace_with
character/string.
import bkit
text = 'তার বাসা ৭৯ নাম্বার রোডে।'
clean_text = bkit.transform.clean_digits(text)
print(clean_text)
clean_text = bkit.transform.clean_digits(text, replace_with='#')
print(clean_text)
Multiple spaces
Clean multiple consecutive whitespace characters including space, newlines, tabs, vertical tabs, etc. It also removes leading and trailing whitespace characters.
import bkit
text = 'তার বাসা ৭৯ \t\t নাম্বার রোডে।\nসে খুব \v ভালো ছেলে।'
clean_text = bkit.transform.clean_multiple_spaces(text)
print(clean_text)
clean_text = bkit.transform.clean_multiple_spaces(text, keep_new_line=True)
print(clean_text)
URLs
Clean URLs from text and replace the URLs with any given string.
import bkit
text = 'আমি https://xyz.abc সাইটে ব্লগ লিখি। এই ftp://10.17.5.23/books সার্ভার থেকে আমার বইগুলো পাবে। এই https://bn.wikipedia.org/wiki/%E0%A6%A7%E0%A6%BE%E0%A6%A4%E0%A7%81_(%E0%A6%AC%E0%A6%BE%E0%A6%82%E0%A6%B2%E0%A6%BE_%E0%A6%AC%E0%A7%8D%E0%A6%AF%E0%A6%BE%E0%A6%95%E0%A6%B0%E0%A6%A3) লিঙ্কটিতে ভালো তথ্য আছে।'
clean_text = bkit.transform.clean_urls(text)
print(clean_text)
clean_text = bkit.transform.clean_urls(text, replace_with='URL')
print(clean_text)
Emojis
Clean emoji and emoticons from text and replace those with any given string.
import bkit
text = 'কিছু ইমোজি হল: 😀🫅🏾🫅🏿🫃🏼🫃🏽🫃🏾🫃🏿🫄🫄🏻🫄🏼🫄🏽🫄🏾🫄🏿🧌🪸🪷🪹🪺🫘🫗🫙🛝🛞🛟🪬🪩🪫🩼🩻🫧🪪🟰'
clean_text = bkit.transform.clean_emojis(text, replace_with='<EMOJI>')
print(clean_text)
HTML tags
Clean HTML tags from text and replace those with any given string.
import bkit
text = '<a href=some_URL>বাংলাদেশ</a>'
clean_text = bkit.transform.clean_html(text)
print(clean_text)
Multiple punctuations
Remove multiple consecutive punctuations and keep the first punctuation only.
import bkit
text = 'কি আনন্দ!!!!!'
clean_text = bkit.transform.clean_multiple_punctuations(text)
print(clean_text)
Special characters
Remove special characters like $
, #
, @
, etc and replace them with the given string. If no character list is passed, [$, #, &, %, @]
are removed by default.
import bkit
text = '#বাংলাদেশ$'
clean_text = bkit.transform.clean_special_characters(text, characters=['#', '$'], replace_with='')
print(clean_text)
Non Bangla characters
Non Bangla characters include characters and punctuation not used in Bangla like english or other language's alphabets and replace them with the given string.
import bkit
text = 'এই শূককীট হাতিশুঁড় Heliotropium indicum, অতসী, আকন্দ Calotropis gigantea গাছের পাতার রসালো অংশ আহার করে।'
clean_text = bkit.transform.clean_non_bangla(text, replace_with='')
print(clean_text)
Text Analysis
Word count
The bkit.analysis.count_words
function can be used to get the word counts. It has the following paramerts:
"""
Args:
text (Tuple[str, List[str]]): The text to count words from. If a string is provided,
it will be split into words. If a list of strings is provided, each string will
be split into words and counted separately.
clean_punctuation (bool, optional): Whether to clean punctuation from the words count. Defaults to False.
punct_replacement (str, optional): The replacement for the punctuation. Only applicable if
clean_punctuation is True. Defaults to "".
return_dict (bool, optional): Whether to return the word count as a dictionary.
Defaults to False.
ordered (bool, optional): Whether to return the word count in descending order. Only
applicable if return_dict is True. Defaults to False.
Returns:
Tuple[int, Dict[str, int]]: If return_dict is True, returns a tuple containing the
total word count and a dictionary where the keys are the words and the values
are their respective counts. If return_dict is False, returns only the total
word count as an integer.
"""
import bkit
text='অভিষেকের আগের দিন গতকাল রোববার ওয়াশিংটনে বিশাল এক সমাবেশে হাজির হন ট্রাম্প। তিনি উচ্ছ্বসিত ভক্ত-সমর্থকদের আমেরিকার পতনের যবনিকা ঘটানোর অঙ্গীকার করেন।'
total_words=bkit.analysis.count_words(text)
print(total_words)
Sentence Count
The bkit.analysis.count_sentences function can be used to get the word counts. It has the following paramerts:
"""
Counts the number of sentences in the given text or list of texts.
Args:
text (Tuple[str, List[str]]): The text or list of texts to count sentences from.
return_dict (bool, optional): Whether to return the result as a dictionary. Defaults to False.
ordered (bool, optional): Whether to order the result in descending order.
Only applicable if return_dict is True. Defaults to False.
Returns:
int or dict: The count of sentences. If return_dict is True, returns a dictionary with sentences as keys
and their counts as values. If return_dict is False, returns the total count of sentences.
Raises:
AssertionError: If ordered is True but return_dict is False.
"""
import bkit
text = 'তুমি কোথায় থাক? ঢাকা বাংলাদেশের\n রাজধানী। কি অবস্থা তার! ১২/০৩/২০২২ তারিখে সে ৪/ক ঠিকানায় গিয়ে ১২,৩৪৫.২৩ টাকা দিয়েছিল।'
count = bkit.analysis.count_sentences(text)
print(count)
count = bkit.analysis.count_sentences(text, return_dict=True, ordered=True)
print(count)
Lemmatization
Lemmatization is implemented based on our this paper BanLemma: A Word Formation Dependent Rule and Dictionary Based Bangla Lemmatizer
For more details
Lemmatize text
Lemmatize a given text. Generally expects the text to be a sentence.
import bkit
text = 'পৃথিবীর জনসংখ্যা ৮ বিলিয়নের কিছু কম'
lemmatized = bkit.lemmatizer.lemmatize(text)
print(lemmatized)
Lemmatize word
Lemmatize a word given the PoS information.
import bkit
text = 'পৃথিবীর'
lemmatized = bkit.lemmatizer.lemmatize_word(text, 'noun')
print(lemmatized)
Stemmer
Stemming is the process of reducing words to their base or root form. Our implementation achieves this by conditionally stripping away predefined prefixes and suffixes from each word.
Stem word
import bkit
stemmer = bkit.stemmer.SimpleStemmer()
stemmer.word_stemer('নগরবাসী')
Stem Sentence
import bkit
stemmer = bkit.stemmer.SimpleStemmer()
stemmer.sentence_stemer('বিকেলে রোদ কিছুটা কমেছে।')
Tokenization
Tokenize a given text. The bkit.tokenizer
module is used to tokenizer text into tokens. It supports three types of tokenization.
Word tokenization
Tokenize text into words. Also separates some punctuations including comma, danda (।), question mark, etc.
import bkit
text = 'তুমি কোথায় থাক? ঢাকা বাংলাদেশের রাজধানী। কি অবস্থা তার! ১২/০৩/২০২২ তারিখে সে ৪/ক ঠিকানায় গিয়ে ১২,৩৪৫ টাকা দিয়েছিল।'
tokens = bkit.tokenizer.tokenize(text)
print(tokens)
Word and Punctuation tokenization
Tokenize text into words and any punctuation.
import bkit
text = 'তুমি কোথায় থাক? ঢাকা বাংলাদেশের রাজধানী। কি অবস্থা তার! ১২/০৩/২০২২ তারিখে সে ৪/ক ঠিকানায় গিয়ে ১২,৩৪৫ টাকা দিয়েছিল।'
tokens = bkit.tokenizer.tokenize_word_punctuation(text)
print(tokens)
Sentence tokenization
Tokenize text into sentences.
import bkit
text = 'তুমি কোথায় থাক? ঢাকা বাংলাদেশের রাজধানী। কি অবস্থা তার! ১২/০৩/২০২২ তারিখে সে ৪/ক ঠিকানায় গিয়ে ১২,৩৪৫ টাকা দিয়েছিল।'
tokens = bkit.tokenizer.tokenize_sentence(text)
print(tokens)
Named Entity Recognition (NER)
Predicts the tags of the Named Entities of a given text.
import bkit
text = 'তুমি কোথায় থাক? ঢাকা বাংলাদেশের রাজধানী। কি অবস্থা তার! ১২/০৩/২০২২ তারিখে সে ৪/ক ঠিকানায় গিয়ে ১২,৩৪৫.২৩ টাকা দিয়েছিল।'
ner = bkit.ner.Infer('ner-noisy-label')
predictions = ner(text)
print(predictions)
Named Entity Recognition (NER) Visualization
It takes the model's output and visualizes the NER tag for every word in the text.
import bkit
text = 'তুমি কোথায় থাক? ঢাকা বাংলাদেশের রাজধানী। কি অবস্থা তার! ১২/০৩/২০২২ তারিখে সে ৪/ক ঠিকানায় গিয়ে ১২,৩৪৫.২৩ টাকা দিয়েছিল।'
ner = bkit.ner.Infer('ner-noisy-label')
predictions = ner(text)
bkit.ner.visualize(predictions)

Parts of Speech (PoS) tagging
Predicts the tags of the parts of speech of a given text.
import bkit
text = 'গত কিছুদিন ধরেই জ্বালানিহীন অবস্থায় একটি ছোট মাছ ধরার নৌকায় ১৫০ জন রোহিঙ্গা আন্দামান সাগরে ভাসমান অবস্থায় রয়েছে ।'
pos = bkit.pos.Infer('pos-noisy-label')
predictions = pos(text)
print(predictions)
Parts of Speech (PoS) Visualization
"It takes the model's output and visualizes the Part-of-Speech tag for every word in the text.
import bkit
text = 'গত কিছুদিন ধরেই জ্বালানিহীন অবস্থায় একটি ছোট মাছ ধরার নৌকায় ১৫০ জন রোহিঙ্গা আন্দামান সাগরে ভাসমান অবস্থায় রয়েছে ।'
pos = bkit.pos.Infer('pos-noisy-label')
predictions = pos(text)
bkit.pos.visualize(predictions)

Shallow Parsing (Constituency Parsing)
Predicts the shallow parsing tags of a given text.
import bkit
text = 'তুমি কোথায় থাক? ঢাকা বাংলাদেশের রাজধানী। কি অবস্থা তার! ১২/০৩/২০২২ তারিখে সে ৪/ক ঠিকানায় গিয়ে ১২,৩৪৫.২৩ টাকা দিয়েছিল।'
shallow = bkit.shallow.Infer(pos_model='pos-noisy-label')
predictions = shallow(text)
print(predictions)
Shallow Parsing Visualization
It converts model predictions into an interactive shallow parsing Tree for clear and intuitive analysis
from bkit import shallow
text = "কাতার বিশ্বকাপে আর্জেন্টিনার বিশ্বকাপ জয়ে মার্তিনেজের অবদান অনেক।"
shallow = shallow.Infer(pos_model='pos-noisy-label')
predictions = shallow(text)
shallow.visualize(predictions)

Dependency Parsing
Predicts the dependency parsing tags of a given text.
from bkit import dependency
text = "কাতার বিশ্বকাপে আর্জেন্টিনার বিশ্বকাপ জয়ে মার্তিনেজের অবদান অনেক।"
dep =dependency.Infer('dependency-parsing')
predictions = dep(text)
print(predictions)
Dependency Parsing Visualization
It converts model predictions into an interactive dependency graph for clear and intuitive analysis
from bkit import dependency
text = "কাতার বিশ্বকাপে আর্জেন্টিনার বিশ্বকাপ জয়ে মার্তিনেজের অবদান অনেক।"
dep = dependency.Infer('dependency-parsing')
predictions = dep(text)
dependency.visualize(predictions)

Coreference Resolution
Predicts the coreferent clusters of a given text.
import bkit
text = "তারাসুন্দরী ( ১৮৭৮ - ১৯৪৮ ) অভিনেত্রী । ১৮৮৪ সালে বিনোদিনীর সহায়তায় স্টার থিয়েটারে যোগদানের মাধ্যমে তিনি অভিনয় শুরু করেন । প্রথমে তিনি গিরিশচন্দ্র ঘোষের চৈতন্যলীলা নাটকে এক বালক ও সরলা নাটকে গোপাল চরিত্রে অভিনয় করেন ।"
coref = bkit.coref.Infer('coref')
predictions = coref(text)
print(predictions)
Coreference Resolution Visualization
It takes the model's output and creates an interactive visualization to clearly depict coreference resolution, highlighting the relationships between entities in the text
from bkit import coref
text = "তারাসুন্দরী ( ১৮৭৮ - ১৯৪৮ ) অভিনেত্রী । ১৮৮৪ সালে বিনোদিনীর সহায়তায় স্টার থিয়েটারে যোগদানের মাধ্যমে তিনি অভিনয় শুরু করেন । প্রথমে তিনি গিরিশচন্দ্র ঘোষের চৈতন্যলীলা নাটকে এক বালক ও সরলা নাটকে গোপাল চরিত্রে অভিনয় করেন ।"
coref = coref.Infer('coref')
predictions = coref(text)
coref.visualize(predictions)
