forked from egcodes/aristotle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseTrendWords.py
208 lines (171 loc) · 5.83 KB
/
parseTrendWords.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
# -*- coding: utf-8 -*-
import sys, operator, time
import signal
from datetime import datetime, timedelta
from LogHandler import LogHandler
from ServerDatabaseHandler import ServerDatabaseHandler
class Main:
def __init__(self):
#=======================================================================
# Configuration
#=======================================================================
#Alinmamasi gereken kelimeler
self.blackWordListOne = []
self.blackWordListTwo = []
self.present = datetime.now().date()
self.yesterday = self.present - timedelta(days=1)
self.yearMonth = str(self.present.strftime('%Y%m'))
self.presentLastMonth = self.present - timedelta(days=30)
self.yearLastMonth = str(self.presentLastMonth.strftime('%Y%m'))
self.serverHandler = ServerDatabaseHandler()
print datetime.now()
self.serverHandler.executeQuery("""CREATE TABLE IF NOT EXISTS `trendWords` (
`id` int(11) NOT NULL,
`date` date DEFAULT NULL,
`category` varchar(50) DEFAULT NULL,
`text` varchar(255) CHARACTER SET utf32 COLLATE utf32_unicode_ci DEFAULT NULL,
`newscount` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;""")
try:
self.serverHandler.executeQuery("ALTER TABLE `trendWords` ADD PRIMARY KEY (`id`)")
except:
pass
lsCategories = ["gundem", "videohaber", "spor", "koseyazilari", "kulturvesanat", "teknoloji"]
lsDataTypes = ["guncel"]
for category in lsCategories:
for dataType in lsDataTypes:
self.category = category
self.dataType = dataType
print self.category, self.dataType
mostCommonWordsList = self.getMostCommonWordsFromMetaContent()
for i in mostCommonWordsList:
text = i[0]
text = text.replace("'", "''")
count = i[1]
if 0 == self.serverHandler.executeQuery("SELECT COUNT(*) FROM `trendWords` WHERE category='%s' and text='%s' and date=CURRENT_DATE()"%(self.category, text))[0][0]:
self.serverHandler.executeQuery("INSERT INTO `trendWords` VALUES(NULL, CURRENT_DATE(), '%s', '%s', %d)"%(self.category, text, count))
print "\t", self.category, text, count
print ""
print ""
def isUpper(self, text):
try:
wordDictU = {'İ':'I', 'Ö':'O', 'Ü':'U', 'Ç':'C', 'Ş':'S', 'Ğ':'G'}
wordDictL = {'ı':'i', 'ö':'o', 'ü':'u', 'ç':'c', 'ş':'s', 'ğ':'g'}
for data in wordDictU.iteritems():
if text[0:2].find(data[0]) != -1:
text = text.replace(data[0], data[1])
for data in wordDictL.iteritems():
if text[0:2].find(data[0]) != -1:
text = text.replace(data[0], data[1])
try:
int(text)
return True
except ValueError:
pass
if text[0].isupper():
return True
else:
return False
except:
self.logHandler.logger("isUpper", text)
return False
def getMostCommonWordsFromMetaContent(self):
metaContents = ""
linkForNews = []
if self.category == "gundem":
linkForNews.extend(list(self.serverHandler.executeQuery("SELECT title, description FROM `links_%s` WHERE tweetCount+facebookCount > 10 AND `date` = '%s' AND category = '%s'"% (self.yearMonth, self.present, self.category))))
else:
linkForNews.extend(list(self.serverHandler.executeQuery("SELECT title, description FROM `links_%s` WHERE `date` = '%s' AND category = '%s'"% (self.yearMonth, self.present, self.category))))
for items in linkForNews:
metaContents += ' ' + items[0] + ' ' + items[1] + ' '
metaWordList = metaContents.split()
wordsDict = {}
indexList = []
#Blackwordlist'de var ise sil
for index, word in enumerate(metaWordList):
try:
self.blackWordListOne.index(word)
metaWordList.remove(word)
except:
pass
for index, word in enumerate(metaWordList):
mostWord = []
try:
indexList.index(index)
continue
except:
pass
while True:
try:
metaWordList[index]
except:
break
if self.isUpper(metaWordList[index]):
mostWord.append(metaWordList[index])
index += 1
indexList.append(index)
else:
break
mostWord = ' '.join(mostWord)
#Icinde nokta virgul olanlari alma anlamsiz
if mostWord[2:-2].find('.') != -1:
continue
if mostWord[2:-2].find(',') != -1:
continue
if mostWord:
#' kaldiriliyor
sp = mostWord.split()
if len(sp) == 2:
if sp[0].find("'") != -1:
mostWord = ' '.join([sp[0][:sp[0].find("'")], sp[1]])
sp = mostWord.split()
if sp[1].find("'") != -1:
mostWord = ' '.join([sp[0], sp[1][:sp[1].find("'")]])
sp = mostWord.split()
if sp[0].find("’") != -1:
mostWord = ' '.join([sp[0][:sp[0].find("’")], sp[1]])
sp = mostWord.split()
if sp[1].find("’") != -1:
mostWord = ' '.join([sp[0], sp[1][:sp[1].find("’")]])
#en sondaki , varsa kaldiriliyor
if mostWord[-1] == ',':
mostWord = mostWord[:-1]
#Blackwordlist'de var ise sil
try:
flag = False
for blackWord in self.blackWordListTwo:
if mostWord.find(blackWord) != -1:
flag = True
if flag:
continue
except:
pass
try:
wordsDict[mostWord] += 1
except:
wordsDict[mostWord] = 1
#En yaygin kelimeler aliniyor
lastDict = {}
index = 0
while len(wordsDict):
index += 1
if index > 100:
break;
commonWord = max(wordsDict, key=wordsDict.get)
#En az 3 haber olsun
if wordsDict[commonWord] < 3:
continue
#Count 1'den fazla ise
if len(commonWord.split()) == 2:
lastDict[' '.join(commonWord.split()[0:3])] = wordsDict[commonWord]
index += 1
del wordsDict[commonWord]
return reversed(sorted(lastDict.iteritems(), key=operator.itemgetter(1)))
def closeProcess(self, arg1, signal):
sys.exit(1)
if __name__ == '__main__':
reload(sys)
sys.setdefaultencoding('utf-8')
main = Main()
# Ctrl+C sinyalini yakalayan fonksiyonu cagirir. Cikis islemleri yaptirilir
signal.signal(signal.SIGINT, main.closeProcess)