File size: 2,189 Bytes
79a1445
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import pandas as pd
import spacy
import gradio as gr
import re


dataset = pd.read_excel('Dataset-Verse-by-Verse.xlsx')
dataset.rename(columns={'ArabicText': 'text'}, inplace=True)
nlp = spacy.load('aravec_model')
all_docs = [nlp(doc) for doc in dataset['text']]

def clean_text(text):
    # remove tashkeel
    text = re.sub('[~ًٌٍَُِّْ]', '', text)
    text = re.sub('[ًٌٍَُِّْـ]', '', text)
    # ozbot el alef
    text = re.sub('إ', 'ا', text)
    text = re.sub('أ', 'ا', text)
    text = re.sub('آ', 'ا', text)
    # remove longation
    text = re.sub(r'(.)\1+', r'\1\1', text)
    # remove extra spaces
    text = re.sub(' +', ' ', text)
    text = text.strip()
    text = re.sub('[\s]+', ' ', text)
    # remove punctuations
    text = re.sub(r'[^\w\s]', '', text)
    return text

def get_similar_sentences(text):
    text = clean_text(text)
    ref_sentence = nlp(text)
    similar_sentences = []
    for i,doc in enumerate(all_docs):
        similar_sentences.append((doc, ref_sentence.similarity(doc) , i))
    similar_sentences.sort(key=lambda x: x[1], reverse=True)
    top_10 = similar_sentences[:10]
    # add the surahnamearabic to text
    return dict(zip([' [ ' + dataset['SurahNameArabic'][i] + ' ] ' + doc.text for doc, _, i in top_10], [similarity for _, similarity, _ in top_10]))

text_input = gr.inputs.Textbox(lines = 1 , label = "Enter a Quran Verse" )

label = gr.outputs.Label()
examples = ['الحمدلله رب العالمين',
            'مثلهم كمثل الذي استوقد نارًا فلما أضاءت ما حوله ذهب الله بنورهم وتركهم في ظلماتٍ لا يبصرون',
            'إن الذين كفروا سواء عليهم أأنذرتهم أم لم تنذرهم لا يؤمنون',
            'ونادى أصحاب الجنة أصحاب النار أن قد وجدنا ما وعدنا ربنا حقا فهل وجدتم ما وعد ربكم حقا ۖ قالوا نعم ۚ فأذن مؤذن بينهم أن لعنة الله على الظالمين'
            ]

intf = gr.Interface(fn = get_similar_sentences , inputs = text_input , outputs = label , examples=examples )
intf.launch()