text_plain_output_1.png
from nltk.stem.isri import ISRIStemmer

import re

import string

def remove_punc(s):
    punctuations = '`÷×؛ʿˇ<>(‚)*&^%][،/:ღ"┈؟.,\'{}~¦+ ، 》《|﴾»«﴿!”…“–❒ـ۞✦✩☜ ̷ ﮼☻\U000fe334❥*،“¸.•°``°•.`•.¸.•♫♡—' + string.punctuation
    punctuations = ''.join(set(punctuations) - {'ـ'})
    for c in punctuations:
        s = s.replace(c, ' ')
    return s
def clean_text(text):
    arabic_diacritics = re.compile(' ّ| َ| ً| ُ| ٌ| ِ| ٍ| ْ| ۖ| ۠| ۘ| ۙ| ۚ| ۛ| ۜ| ۗ| ۡ| ۟| ۤ|ۥ| ۧ', re.VERBOSE)
    weridPatterns = re.compile('[😀-🙏🌀-🗿🚀-\U0001f6ff\U0001f1e0-🇿✂-➰Ⓜ-🉑🤦-🤷𐀀-\U0010ffff\u200d♀-♂☀-⭕⏏⏩⌚〰️\u2069\u2066\u200c\u2068\u2067]+', flags=re.UNICODE)
    text = weridPatterns.sub(' ', text)
    text = re.sub(arabic_diacritics, '', text)
    text = re.sub('\\d+', ' ', text)
    text = re.sub('ﷺ', 'صلى الله عليه وسلم', text)
    text = re.sub('[كق][رو]+ن[اهة]*', 'كورونا', text)
    text = re.sub('_', ' ', text)
    text = re.sub('\n', ' ', text)
    text = re.sub('ـ', '', text)
    text = re.sub('،', ' ', text)
    text = re.sub('وو', 'و', text)
    text = re.sub('يي', 'ي', text)
    text = re.sub('اا', 'ا', text)
    text = re.sub('أأ', 'أ', text)
    text = re.sub('URL', '', text)
    text = re.sub('USER', '', text)
    text = re.sub('[ٱٲٳٵ]', 'ا', text)
    text = re.sub('[پ]', 'ب', text)
    text = re.sub('[ٺټ]', 'ت', text)
    text = re.sub('[چ]', 'ج', text)
    text = re.sub('[ډڊ]', 'د', text)
    text = re.sub('[ڏ]', 'ذ', text)
    text = re.sub('[ڒړڕ]', 'ر', text)
    text = re.sub('[ژ]', 'ز', text)
    text = re.sub('[کڪګگڰڱڳڴؼػ]', 'ك', text)
    text = re.sub('[؏]', 'ع', text)
    text = re.sub('[ڛ]', 'س', text)
    text = re.sub('[ێېیێېےۓؽؾؿ]', 'ي', text)
    text = re.sub('[ڣڤڨᓅ]', 'ف', text)
    text = re.sub('[ۆۈۉۊۋ]', 'و', text)
    text = re.sub('[ھہۂۿ]', 'ه', text)
    text = re.sub('[ںڼݩ]', 'ن', text)
    text = re.sub('[۾ᓄ]', 'م', text)
    text = re.sub('[ڵ]', 'ل', text)
    text = re.sub('[ۃ]', 'ة', text)
    text = remove_punc(text)
    text = re.compile('(.)\\1{2,}', re.IGNORECASE).sub('\\1', text)
    text = re.sub('\\s+', ' ', text)
    return text

from nltk.stem.isri import ISRIStemmer
s = u'كم عدد مستشفيات العزل فى مصر وما هى اماكنها'
def stem(sentence):
    stemmed_sentence = []
    for i in sentence.split(' '):
        i = re.sub('^ال', '', i)
        stemmed_sentence.append(ISRIStemmer().suf32(i))
    return ' '.join(stemmed_sentence)
stem(s)