File size: 2,200 Bytes
7782f2b
 
 
 
9bd3c01
 
7782f2b
4d67792
7782f2b
9bd3c01
7782f2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6f533a
 
9bd3c01
 
a78bbad
9bd3c01
 
 
7782f2b
eb8e114
7782f2b
 
 
eb8e114
4bf1010
7782f2b
 
 
 
 
 
caa90ac
9bd3c01
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
import pandas as pd
import spacy
import gradio as gr
import re
import json
import random

dataset = pd.read_csv('hatith_all_2.csv')
nlp = spacy.load('aravec_model')
all_docs = [nlp(doc) for doc in dataset['hadith']]

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 = []
    #sampled_docs = random.sample(all_docs , 15000)
    for i, doc in enumerate(all_docs):
        similarity_score = ref_sentence.similarity(doc)
        similar_sentences.append({
            "similar_sentence": dataset['reference'][i]+str(doc),
            "similarity_score": similarity_score,
        })
    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)