How can I remove foreign word from Bengali text in python

Viewed 518

I have a text data file which consist of raw bangla text data with so much foreign words I want to remove all foreign words from Bengali raw text.

input:

দেশের রাজনীতি দিনকে দিন পচে যাচ্ছে। पैरेनकाइमा कोशिकाएं . what a shame. সুস্থ থাকা দায়।

Output:

দেশের রাজনীতি দিনকে দিন পচে যাচ্ছে। সুস্থ থাকা দায়।

Any suggestion or idea will help me a lot.

thanks in advance.

regards

3 Answers

Use re with split() function to remove multiple whilespaces.

import re

a = "দেশের রাজনীতি দিনকে দিন পচে যাচ্ছে। पैरेनकाइमा कोशिकाएं . what a shame. সুস্থ থাকা দায়।"

a = "".join(i for i in a if i in ["।"] or 2432 <= ord(i) <= 2559 or ord(i)== 32)
a=" ".join(a.split())
print(a)

Outupt:

দেশের রাজনীতি দিনকে দিন পচে যাচ্ছে। সুস্থ থাকা দায়।

Using ord to filter out "western characters and punctuation" (sorry for lack of better word), and re to remove multiple spaces.

import re

a = "দেশের রাজনীতি দিনকে দিন পচে যাচ্ছে। पैरेनकाइमा कोशिकाएं . what a shame. সুস্থ থাকা দায়।"

a = "".join(i for i in a if ord(i) > ord('z') or ord(i)== 32)
re.sub(' +', ' ', a)

returns:

'দেশের রাজনীতি দিনকে দিন পচে যাচ্ছে। पैरेनकाइमा कोशिकाएं সুস্থ থাকা দায়।'

Here is what Christian Sloper mentioned in the comments. Apparently the correct ord range is 2432 to 2559 so something like this seems to work:

import re

a = "দেশের রাজনীতি দিনকে দিন পচে যাচ্ছে। पैरेनकाइमा कोशिकाएं . what a shame. সুস্থ থাকা দায়।"

a = "".join(i for i in a if i in [".","।"] or 2432 <= ord(i) <= 2559 or ord(i)== 32)
re.sub(' +', ' ', a)
Related