|
| 1 | +# Bengali Natural Language Processing(BNLP) |
| 2 | +BNLP is a natural language processing toolkit for Bengali Language. This tool will help you to **tokenize Bengali text**, **Embedding Bengali words**, **construct neural model** for Bengali NLP purposes. |
| 3 | + |
| 4 | + |
| 5 | +# Contents |
| 6 | +- [Current Features](#current-features) |
| 7 | +- [Installation](#installation) |
| 8 | +- [Pretrained Model](#pretrained-model) |
| 9 | +- [Tokenization](#tokenization) |
| 10 | +- [Embedding](#word-embedding) |
| 11 | +- [Issue](#issue) |
| 12 | +- [Contributor Guide](#contributor-guide) |
| 13 | +- [Contributor List](#contributor-list) |
| 14 | + |
| 15 | + |
| 16 | +## Current Features |
| 17 | +* [Bengali Tokenization](#tokenization) |
| 18 | + - SentencePiece Tokenizer |
| 19 | + - Basic Tokenizer |
| 20 | + - NLTK Tokenizer |
| 21 | +* [Bengali Word Embedding](#word-embedding) |
| 22 | + - Bengali Word2Vec |
| 23 | + - Bengali Fasttext |
| 24 | + - Bengali GloVe |
| 25 | + |
| 26 | + |
| 27 | +## Installation |
| 28 | + |
| 29 | +* pypi package installer(python 3.5, 3.6, 3.7 tested okay) |
| 30 | + |
| 31 | + ```pip install bnlp_toolkit``` |
| 32 | + |
| 33 | +* Local |
| 34 | + ``` |
| 35 | + $git clone https://github.com/sagorbrur/bnlp.git |
| 36 | + $cd bnlp |
| 37 | + $python setup.py install |
| 38 | + ``` |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +## Pretrained Model |
| 43 | + |
| 44 | +### Download Link |
| 45 | + |
| 46 | +* [Bengali SentencePiece](https://github.com/sagorbrur/bnlp/tree/master/model) |
| 47 | +* [Bengali Word2Vec](https://drive.google.com/open?id=1DxR8Vw61zRxuUm17jzFnOX97j7QtNW7U) |
| 48 | +* [Bengali FastText](https://drive.google.com/open?id=1CFA-SluRyz3s5gmGScsFUcs7AjLfscm2) |
| 49 | +* [Bengali GloVe Wordvectors](https://github.com/sagorbrur/GloVe-Bengali) |
| 50 | + |
| 51 | +### Training Details |
| 52 | +* All three model trained with **Bengali Wikipedia Dump Dataset** |
| 53 | + - [Bengali Wiki Dump](https://dumps.wikimedia.org/bnwiki/latest/) |
| 54 | +* SentencePiece Training Vocab Size=50000 |
| 55 | +* Fasttext trained with total words = 20M, vocab size = 1171011, epoch=50, embedding dimension = 300 and the training loss = 0.318668, |
| 56 | +* Word2Vec word embedding dimension = 300 |
| 57 | +* To Know Bengali GloVe Wordvector and training process follow [this](https://github.com/sagorbrur/GloVe-Bengali) repository |
| 58 | + |
| 59 | + |
| 60 | +## Tokenization |
| 61 | + |
| 62 | +* **Bengali SentencePiece Tokenization** |
| 63 | + |
| 64 | + - tokenization using trained model |
| 65 | + ```py |
| 66 | + from bnlp.sentencepiece_tokenizer import SP_Tokenizer |
| 67 | + |
| 68 | + bsp = SP_Tokenizer() |
| 69 | + model_path = "./model/bn_spm.model" |
| 70 | + input_text = "আমি ভাত খাই। সে বাজারে যায়।" |
| 71 | + tokens = bsp.tokenize(model_path, input_text) |
| 72 | + print(tokens) |
| 73 | + |
| 74 | + ``` |
| 75 | + - Training SentencePiece |
| 76 | + ```py |
| 77 | + from bnlp.sentencepiece_tokenizer import SP_Tokenizer |
| 78 | + |
| 79 | + bsp = SP_Tokenizer(is_train=True) |
| 80 | + data = "test.txt" |
| 81 | + model_prefix = "test" |
| 82 | + vocab_size = 5 |
| 83 | + bsp.train_bsp(data, model_prefix, vocab_size) |
| 84 | + |
| 85 | + ``` |
| 86 | + |
| 87 | +* **Basic Tokenizer** |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + ```py |
| 92 | + from bnlp.basic_tokenizer import BasicTokenizer |
| 93 | + basic_t = BasicTokenizer(False) |
| 94 | + raw_text = "আমি বাংলায় গান গাই।" |
| 95 | + tokens = basic_t.tokenize(raw_text) |
| 96 | + print(tokens) |
| 97 | + |
| 98 | + # output: ["আমি", "বাংলায়", "গান", "গাই", "।"] |
| 99 | + |
| 100 | + ``` |
| 101 | + |
| 102 | +* **NLTK Tokenization** |
| 103 | + |
| 104 | + ```py |
| 105 | + from bnlp.nltk_tokenizer import NLTK_Tokenizer |
| 106 | + |
| 107 | + text = "আমি ভাত খাই। সে বাজারে যায়। তিনি কি সত্যিই ভালো মানুষ?" |
| 108 | + bnltk = NLTK_Tokenizer(text) |
| 109 | + word_tokens = bnltk.word_tokenize() |
| 110 | + sentence_tokens = bnltk.sentence_tokenize() |
| 111 | + print(word_tokens) |
| 112 | + print(sentence_tokens) |
| 113 | + |
| 114 | + # output |
| 115 | + # word_token: ["আমি", "ভাত", "খাই", "।", "সে", "বাজারে", "যায়", "।", "তিনি", "কি", "সত্যিই", "ভালো", "মানুষ", "?"] |
| 116 | + # sentence_token: ["আমি ভাত খাই।", "সে বাজারে যায়।", "তিনি কি সত্যিই ভালো মানুষ?"] |
| 117 | + |
| 118 | + ``` |
| 119 | + |
| 120 | + |
| 121 | +## Word Embedding |
| 122 | + |
| 123 | +* **Bengali Word2Vec** |
| 124 | + |
| 125 | + - Generate Vector using pretrain model |
| 126 | + |
| 127 | + ```py |
| 128 | + from bnlp.bengali_word2vec import Bengali_Word2Vec |
| 129 | + |
| 130 | + bwv = Bengali_Word2Vec() |
| 131 | + model_path = "model/bengali_word2vec.model" |
| 132 | + word = 'আমার' |
| 133 | + vector = bwv.generate_word_vector(model_path, word) |
| 134 | + print(vector.shape) |
| 135 | + print(vector) |
| 136 | + |
| 137 | + ``` |
| 138 | + |
| 139 | + - Find Most Similar Word Using Pretrained Model |
| 140 | + |
| 141 | + ```py |
| 142 | + from bnlp.bengali_word2vec import Bengali_Word2Vec |
| 143 | + |
| 144 | + bwv = Bengali_Word2Vec() |
| 145 | + model_path = "model/bengali_word2vec.model" |
| 146 | + word = 'আমার' |
| 147 | + similar = bwv.most_similar(model_path, word) |
| 148 | + print(similar) |
| 149 | + |
| 150 | + ``` |
| 151 | + - Train Bengali Word2Vec with your own data |
| 152 | + |
| 153 | + ```py |
| 154 | + from bnlp.bengali_word2vec import Bengali_Word2Vec |
| 155 | + bwv = Bengali_Word2Vec(is_train=True) |
| 156 | + data_file = "test.txt" |
| 157 | + model_name = "test_model.model" |
| 158 | + vector_name = "test_vector.vector" |
| 159 | + bwv.train_word2vec(data_file, model_name, vector_name) |
| 160 | + |
| 161 | + |
| 162 | + ``` |
| 163 | + |
| 164 | + * **Bengali FastText** |
| 165 | + |
| 166 | + |
| 167 | + - Generate Vector Using Pretrained Model |
| 168 | + |
| 169 | + |
| 170 | + ```py |
| 171 | + from bnlp.bengali_fasttext import Bengali_Fasttext |
| 172 | + |
| 173 | + bft = Bengali_Fasttext() |
| 174 | + word = "গ্রাম" |
| 175 | + model_path = "model/bengali_fasttext.bin" |
| 176 | + word_vector = bft.generate_word_vector(model_path, word) |
| 177 | + print(word_vector.shape) |
| 178 | + print(word_vector) |
| 179 | + |
| 180 | + |
| 181 | + ``` |
| 182 | + - Train Bengali FastText Model |
| 183 | + |
| 184 | + ```py |
| 185 | + from bnlp.bengali_fasttext import Bengali_Fasttext |
| 186 | + |
| 187 | + bft = Bengali_Fasttext(is_train=True) |
| 188 | + data = "data.txt" |
| 189 | + model_name = "saved_model.bin" |
| 190 | + epoch = 50 |
| 191 | + bft.train_fasttext(data, model_name, epoch) # epoch not implement in pypi yet |
| 192 | + # bft.train_fasttext(data, model_name) in pypi now |
| 193 | + |
| 194 | + ``` |
| 195 | + |
| 196 | +* **Bengali GloVe Word Vectors** |
| 197 | + |
| 198 | + We trained glove model with bengali data(wiki+news articles) and published bengali glove word vectors</br> |
| 199 | + You can download and use it on your different machine learning purposes. |
| 200 | + |
| 201 | + ```py |
| 202 | + from bnlp.glove_wordvector import BN_Glove |
| 203 | + glove_path = "bn_glove.39M.100d.txt" |
| 204 | + word = "গ্রাম" |
| 205 | + bng = BN_Glove() |
| 206 | + res = bng.closest_word(glove_path, word) |
| 207 | + print(res) |
| 208 | + vec = bng.word2vec(glove_path, word) |
| 209 | + print(vec) |
| 210 | + |
| 211 | + ``` |
| 212 | + |
| 213 | +## Issue |
| 214 | +* if `ModuleNotFoundError: No module named 'fasttext'` problem arise please do the next line |
| 215 | + |
| 216 | +```pip install fasttext``` |
| 217 | +* if `nltk` issue arise please do the following line before importing `bnlp` |
| 218 | + |
| 219 | +```py |
| 220 | +import nltk |
| 221 | +nltk.download("punkt") |
| 222 | +``` |
0 commit comments