File size: 2,478 Bytes
79a1445
 
 
 
2c6575f
79a1445
b27d547
79a1445
b27d547
79a1445
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
559e876
 
 
 
2c6575f
 
559e876
 
 
 
 
 
 
 
79a1445
4c03bb9
79a1445
 
 
4c03bb9
79a1445
 
 
 
 
 
4c03bb9
559e876
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
52
53
54
55
56
57
58
59
60
61
import pandas as pd
import spacy
import gradio as gr
import re
import json

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(dataset['text']):
        similarity_score = ref_sentence.similarity(nlp(doc))
        similar_sentence = doc
        surah_name = dataset['SurahNameArabic'][i]
        ayah_no = int(dataset['AyahNo'][i])
        surah_no = int(dataset['SurahNo'][i])
        similar_sentences.append({
            "similar_sentence": similar_sentence,
            "similarity_score": similarity_score,
            "surahName": surah_name,
            "AyahNo": ayah_no,
            "SurahNumber" : surah_no
        })
    similar_sentences.sort(key=lambda x: x['similarity_score'], reverse=True)
    top_10 = similar_sentences[:10]
    return top_10

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

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

intf = gr.Interface(fn = get_similar_sentences , inputs = text_input , outputs =output_text, examples=examples )
intf.launch(debug = True)