-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall.py
305 lines (262 loc) · 9.09 KB
/
all.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
'''
from googletrans import Translator
translator = Translator()
tr = translator.translate("Danes sem jedel pašteto Argeto za zajtrk.", src='sl')
print(tr.text)
from textblob import TextBlob
from bs4 import BeautifulSoup
TextBlob(tr.text).sentiment
'''
''' list of political parties, and their memebers '''
#ime stranke, kratica stranke, predsednik ime in naziv, vplivni člani
df = pd.read_csv("/home/drew99/Documents/programs_code/neuravnotezenost_slovenskih_medjiev/stranke.csv")
df = df[['Stranka .1','Stranka .2','Predsednik ']].iloc[1:]
df = df.drop(19)
df['Stranka .1'] = df['Stranka .1'].str.replace('\[(.*?)\]','',regex=True)
df['Stranka .2'] = df['Stranka .2'].str.replace('\[(.*?)\]','',regex=True)
df['Predsednik '] = df['Predsednik '].str.replace('\[(.*?)\]','',regex=True)
df = df.rename(columns={'Stranka .1':'Kratica stranke', 'Stranka .2':'Ime stranke', 'Predsednik ':'Predsednik'})
import requests
base="https://www.24ur.com"
'''
html = requests.get("https://www.24ur.com/novice/volitve").content
soup = BeautifulSoup(html)
a_elems = soup.select("div.news-list__item a")
urls=[]
for a in a_elems:
url = a.get('href')
urls.append(url)
'''
''' get article urls '''
#export PATH=$PATH:/home/drew99/Documents/programs_code/neuravnotezenost_slovenskih_medjiev/
import time
from bs4 import BeautifulSoup
from selenium import webdriver
options = webdriver.FirefoxOptions()
options.add_argument('--headless')
#browser = webdriver.Firefox(options=options, executable_path='/home/drew99/Documents/programs_code/neuravnotezenost_slovenskih_medjiev/geckodriver')
browser = webdriver.Firefox(options=options)
urls=[]
for i in range(1,20):
#url="https://www.24ur.com/arhiv/novice/volitve?stran="+str(i)
url="https://www.24ur.com/iskanje?q=politika&stran="+str(i)
browser.get(url)
time.sleep(3)
html = browser.page_source
soup = BeautifulSoup(html, 'lxml')
#a_elems = soup.select("div.timeline__right a")
a_elems = soup.select("main div div:nth-child(3) a")
for a in a_elems:
url = a.get('href')
urls.append(url)
urls = list(filter(None, urls))
urls = list(filter(lambda x: ('/video?video=' not in x), urls))
#<div class="article__summary">
#<h1 class="article__title">
#<onl-article-body class="article__body-dynamic dev-article-contents"> span p
#<div class="article__tags"><a class="article__tag" href="/iskanje?q=volitve2022"> VOLITVE2022 </a><!----><!---->
#<meta name="keywords" content="soočenje, svet, odločitev 2022, dejstva">
''' get article text '''
import pandas as pd
df = pd.DataFrame(columns=['Url','Naslov','Povzetek','Besedilo'])
#keys=[]
for u in urls:
browser.get(base+u)
time.sleep(3)
html = browser.page_source
soup = BeautifulSoup(html, 'lxml')
titles = []
#try if any of these selectors catch the title
titles.append(soup.select('.article__title'))
titles.append(soup.select('.onl-article-title'))
titles.append(soup.select("div h1"))
filtered = list(filter(lambda x: (len(x)>0), titles))
if filtered:
title = filtered[0][0].getText()
else:
continue;
summary = soup.select('.article__summary')
if summary:
summary = summary[0].getText()
else:
summary = soup.select('.article__body .px-article-head')[0].getText()
texts = soup.select('.article__body-dynamic span p')
text=""
for para in texts:
text += para.getText() + "\n"
#es = soup.select('meta[name="keywords"]')
#if len(es)>0:
# keys.append(es[0].get('content'))
df = df.append({'Url':base+u, 'Naslov':title, 'Povzetek':summary, 'Besedilo':text}, ignore_index=True)
#keywords=[]
#for i in keys:
# keywords.extend(i.lower().split(', '))
#
#keywords=set(keywords)
ixs=[]
for i,r in df.iterrows():
if len(r['Besedilo'])<10:
ixs.append(i)
df = df.drop(ixs)
''' TRANSLATE text slo->eng'''
import pandas as pd
df=pd.read_csv("novice.csv")
from googletrans import Translator
translator = Translator()
dfeng = pd.DataFrame(columns=['Url','Title','Summary','Text'])
for i,r in df.iterrows():
title = translator.translate(r['Naslov'], src='sl').text
summary = translator.translate(r['Povzetek'], src='sl').text
besedilo = r['Besedilo']
txt=""
if len(besedilo)>15000:
txt = translate15k(translator,besedilo)
else:
txt = translator.translate(besedilo, src='sl').text
dfeng = dfeng.append({'Url':r['Url'], 'Title':title, 'Summary':summary, 'Text':txt}, ignore_index=True)
def translate15k(translator, text):
l = len(text)
r = l//15000 +1
isplit = l//r
trans = ""
s=0;e=0
while s<l and e<l:
e = s+isplit if s+isplit < l else l #be careful to not go index out of bounds
enew = text.rfind('\n',s,e) #find end of parapgraph
trans += translator.translate(text[s:enew]).text + '\n' #translate
s=enew
return trans
dfsloeng = pd.concat([df.reset_index(drop=True),dfeng.drop('Url',axis=1)], axis=1)
dfsloeng.to_csv("newsnovice.csv",index=False)
''' get stranke in text '''
allstr=""
for i in range(len(dfstr2)):
a = ','.join(dfstr2.iloc[i])
allstr += "{:<2525}".format(a) #2525 is length of the longest sequence, pad everything to that size
#re.findall('\((.*?)\)',txt)
#'sds' in txt.lower().split()
#'naša dežela' in txt.lower()
#allstr.index('dobra država')//2525
#print(e.text, e.label_) #PERSON, ORG, GPE(Geopolitical entity, i.e. countries, cities, states)
#kratice.extend(re.findall('\((.*?)\)',txt))
import re
import string
import regex #not same as re
import pandas as pd
import numpy as np
from textblob import TextBlob
import spacy
nlp = spacy.load("en_core_web_sm")
dfnew = pd.read_csv("newsnovice.csv")
dfstr2 = pd.read_csv("stranke_finall.csv")
dfstr2 = dfstr2.apply(lambda x: x.astype(str).str.lower())
dfnew2 = dfnew.copy()
dfnew2['Stranke']=""
dfnew2['Stevilo pojavitev']=""
dfnew2['Max stranka']=""
dfnew2['Sentiment']=np.nan
dfnew2['Subjektivnost']=-1.0
for i in range(len(dfnew2)):
txt = dfnew.loc[i,'Besedilo'].lower() #slovenian text
txte = dfnew.loc[i,'Text'].lower() #english text
kratice = []
kratice.extend(get_str_from_people(txte,dfstr2,allstr,nlp))
kratice.extend(get_str_from_brackets(txt,dfstr2))
kratice.extend(get_str_from_txt(txt,dfstr2))
kratice.extend(get_str_from_txt_name(txt,dfstr2))
print(i,kratice)
if len(kratice)<1:
continue;
values, counts = np.unique(kratice, return_counts=True)
print(values,counts)
#avg_str = values[np.where(counts>np.mean(counts))[0]]
#avg1_str = values[np.where(counts>np.mean(counts)+1)[0]]
max_str = values[np.argmax(counts)]
#
so = TextBlob(txte).sentiment
#
dfnew2.loc[i,'Stranke'] = ",".join(values)
dfnew2.loc[i,'Stevilo pojavitev'] = ",".join([str(x) for x in counts])
#list(map(int, st_pojavitev.split(","))) #reverse operation
dfnew2.loc[i,'Max stranka'] = max_str
dfnew2.loc[i,'Sentiment'] = so.polarity #-1 negative, 0 neutral, 1 positive
dfnew2.loc[i,'Subjektivnost'] = so.subjectivity #0 objective, 1 subjective
def get_str_from_people(txte,dfstr2,allstr,nlp):
doc = nlp(txte)
kr = []
for e in doc.ents:
if len(e.text.split())>1 and e.label_=='PERSON':
if e.text.lower() in allstr:
i = allstr.index(e.text.lower())//2525
kr.append(dfstr2.loc[i,'Kratica stranke'])
else:
ee = re.sub(r'[()]', '', e.text.lower()) #remove all parentheses
for i in range(1,3): #up to 3 differences
l = regex.findall('(?:' + ee + '){e<=' + str(i) + '}', allstr)
if len(l)>0:
i = allstr.index(l[0])//2525
kr.append(dfstr2.loc[i,'Kratica stranke'])
break;
return kr
def get_str_from_brackets(txt,dfstr2):
#stranke napisane v oklepajih
besede = re.findall('\((.*?)\)',txt)
imena = list(dfstr2['Ime stranke'])
kratice = list(dfstr2['Kratica stranke'])
krat = []
for k in besede:
if k in imena:
i = dfstr2.index[dfstr2['Ime stranke'] == k].tolist()[0]
krat.append(dfstr2.loc[i,'Kratica stranke'])
elif k in kratice:
krat.append(k)
return krat
def get_str_from_txt(txt,dfstr2):
#kratice strank kjerkoli (split->ena beseda)
#https://stackoverflow.com/a/43934982
kratice=[]
translator = str.maketrans(string.punctuation, ' '*len(string.punctuation)) #remove punctuations
splt = txt.translate(translator).split()
for k in dfstr2['Kratica stranke']:
if k in splt:
kratice.extend([k]*splt.count(k))
return kratice
def get_str_from_txt_name(txt,dfstr2):
#pojavitev celotnega imena stranke v besedilu
kratice=[]
for i,k in dfstr2[['Kratica stranke','Ime stranke']].iterrows():
ime = k['Ime stranke']
kra = k['Kratica stranke']
if ime in txt:
kratice.extend([kra]*txt.count(ime))
return kratice
''' graph sentiment per political party '''
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
dfnew2 = pd.read_csv("24ur_data.csv")
dg = dfnew2.dropna()
g = dg[['Max stranka','Sentiment']]
#plot everything
g.plot(kind='scatter',x='Max stranka',y='Sentiment',color='red', ylim=[-1,1])
plt.show()
#plot only articles where mostly 1 party was mentioned
ix=[]
for i,k in dg.iterrows():
r=k['Stevilo pojavitev']
if len(r)<1:
continue;
l=list(map(int, r.split(",")))
l=np.array(l)
lp1=np.where(l>np.mean(l)+1)[0]
lp3=np.where(l>np.mean(l)+3)[0]
if len(lp1)==1 and len(lp3)==1:
ix.append(i)
print(i,lp1, k['Stranke'].split(',')[lp1[0]])
for i,z in dg.iterrows():
if(len(z['Stranke'].split(','))==1 and len(z['Stranke'])>0):
ix.append(i)
print(i,z['Stranke'])
ix.sort()
t=dfnew2.iloc[ix]
g=t[['Max stranka','Sentiment']]