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import GoogleNews
|
| 6 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 7 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 8 |
+
import pandas as pd
|
| 9 |
+
import numpy as np
|
| 10 |
+
import string
|
| 11 |
+
import re
|
| 12 |
+
from nltk.corpus import stopwords
|
| 13 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 14 |
+
import sklearn
|
| 15 |
+
import time
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
googlenews = GoogleNews()
|
| 19 |
+
googlenews = GoogleNews(lang='ar')
|
| 20 |
+
googlenews.clear()
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
st.write("""
|
| 25 |
+
Arabic Fake News Detection System
|
| 26 |
+
A system designed as a part of master project
|
| 27 |
+
done by Reem AlFouzan
|
| 28 |
+
Supervised by : Dr, Abdulla al mutairi
|
| 29 |
+
""")
|
| 30 |
+
#df = pd.read_csv('News.csv')
|
| 31 |
+
text_input = st.text_input (''' **Enter the text** ''')
|
| 32 |
+
if len(text_input) != 0:
|
| 33 |
+
inputt = []
|
| 34 |
+
inputt = pd.DataFrame([text_input])
|
| 35 |
+
|
| 36 |
+
googlenews.search(inputt.iloc[0,0])
|
| 37 |
+
googlenews.get_news(inputt.iloc[0,0])
|
| 38 |
+
|
| 39 |
+
result_0 = googlenews.page_at(1)
|
| 40 |
+
print("Data")
|
| 41 |
+
print(result_0, "data 2")
|
| 42 |
+
# time.sleep(100)
|
| 43 |
+
if len(result_0) == 0:
|
| 44 |
+
desc_1 = ['لا يوجد نتائج للخبر ']
|
| 45 |
+
link_1 = ['لا يوجد مصدر']
|
| 46 |
+
if len(result_0) != 0:
|
| 47 |
+
desc_1 = googlenews.get_texts()
|
| 48 |
+
link_1 = googlenews.get_links()
|
| 49 |
+
for i in list(range(2, 70)):
|
| 50 |
+
|
| 51 |
+
result = googlenews.page_at(i)
|
| 52 |
+
desc = googlenews.get_texts()
|
| 53 |
+
link = googlenews.get_links()
|
| 54 |
+
|
| 55 |
+
desc_1 = desc_1 + desc
|
| 56 |
+
link_1 = link_1 + link
|
| 57 |
+
|
| 58 |
+
column_names = ["text", 'link']
|
| 59 |
+
df = pd.DataFrame(columns = column_names)
|
| 60 |
+
|
| 61 |
+
df['text'] = desc_1
|
| 62 |
+
df['link'] = link_1
|
| 63 |
+
|
| 64 |
+
for letter in '#.][!XR':
|
| 65 |
+
df['text'] = df['text'].astype(str).str.replace(letter,'')
|
| 66 |
+
inputt[0] = inputt[0].astype(str).str.replace(letter,'')
|
| 67 |
+
|
| 68 |
+
arabic_punctuations = '''`÷×؛<>_()*&^%][ـ،/:"؟.,'{}~¦+|!”…“–ـ'''
|
| 69 |
+
english_punctuations = string.punctuation
|
| 70 |
+
punctuations_list = arabic_punctuations + english_punctuations
|
| 71 |
+
|
| 72 |
+
def remove_punctuations(text):
|
| 73 |
+
translator = str.maketrans('', '', punctuations_list)
|
| 74 |
+
return text.translate(translator)
|
| 75 |
+
|
| 76 |
+
def normalize_arabic(text):
|
| 77 |
+
text = re.sub("[إأآا]", "ا", text)
|
| 78 |
+
text = re.sub("ى", "ي", text)
|
| 79 |
+
text = re.sub("ة", "ه", text)
|
| 80 |
+
text = re.sub("گ", "ك", text)
|
| 81 |
+
return text
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def remove_repeating_char(text):
|
| 85 |
+
return re.sub(r'(.)\1+', r'\1', text)
|
| 86 |
+
|
| 87 |
+
def processPost(text):
|
| 88 |
+
|
| 89 |
+
#Replace @username with empty string
|
| 90 |
+
text = re.sub('@[^\s]+', ' ', text)
|
| 91 |
+
|
| 92 |
+
#Convert www.* or https?://* to " "
|
| 93 |
+
text = re.sub('((www\.[^\s]+)|(https?://[^\s]+))',' ',text)
|
| 94 |
+
|
| 95 |
+
#Replace #word with word
|
| 96 |
+
text = re.sub(r'#([^\s]+)', r'\1', text)
|
| 97 |
+
|
| 98 |
+
# remove punctuations
|
| 99 |
+
text= remove_punctuations(text)
|
| 100 |
+
|
| 101 |
+
# normalize the text
|
| 102 |
+
text= normalize_arabic(text)
|
| 103 |
+
|
| 104 |
+
# remove repeated letters
|
| 105 |
+
text=remove_repeating_char(text)
|
| 106 |
+
|
| 107 |
+
return text
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
df['text'] = df['text'].apply(lambda x: processPost(x))
|
| 111 |
+
inputt[0] = inputt[0].apply(lambda x: processPost(x))
|
| 112 |
+
|
| 113 |
+
st.markdown(f"my input is : { inputt.iloc[0,0] }")
|
| 114 |
+
#input=input.apply(lambda x: processPost(x))
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
vectorizer = TfidfVectorizer()
|
| 118 |
+
vectors = vectorizer.fit_transform(df['text'])
|
| 119 |
+
|
| 120 |
+
text_tfidf = pd.DataFrame(vectors.toarray())
|
| 121 |
+
|
| 122 |
+
traninput = vectorizer.transform(inputt[0])
|
| 123 |
+
traninput = traninput.toarray()
|
| 124 |
+
cosine_sim = cosine_similarity(traninput,text_tfidf)
|
| 125 |
+
top = np.max(cosine_sim)
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
if top >= .85 :
|
| 129 |
+
prediction = 'الخبر صحيح'
|
| 130 |
+
elif (top < .85) and (top >= .6) :
|
| 131 |
+
prediction = 'الخبر مظلل '
|
| 132 |
+
elif top < .6 :
|
| 133 |
+
prediction = 'الخبر كاذب '
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
st.markdown(f"most similar news is: { df['text'].iloc[np.argmax(np.array(cosine_sim[0]))] }")
|
| 137 |
+
st.markdown(f"Source url : {df['link'].iloc[np.argmax(np.array(cosine_sim[0]))]}")
|
| 138 |
+
st.markdown(f"Credibility rate : { np.max(cosine_sim)}")
|
| 139 |
+
st.markdown(f"system prediction: { prediction}")
|
| 140 |
+
df.to_csv('Students.csv', sep ='\t')
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
st.sidebar.markdown('مواقع اخباريه معتمده ')
|
| 144 |
+
st.sidebar.markdown("[العربية](https://www.alarabiya.net/)")
|
| 145 |
+
st.sidebar.markdown("[الجزيرة نت](https://www.aljazeera.net/news/)")
|
| 146 |
+
st.sidebar.markdown("[وكالة الانباء الكويتية](https://www.kuna.net.kw/Default.aspx?language=ar)")
|
| 147 |
+
|
| 148 |
+
#st.markdown('test')
|