forked from KPF-newstrust/ntrust-worker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_basic.py
More file actions
334 lines (259 loc) · 10.7 KB
/
Copy pathhandler_basic.py
File metadata and controls
334 lines (259 loc) · 10.7 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import datetime
import timeit
from konlpy.tag import Hannanum
from konlpy.tag import Kkma
from konlpy.tag import Komoran
from konlpy.tag import Twitter
from bson.objectid import ObjectId
import helper
from ntrust import tagger
from ntrust import textcount
from ntrust import sentences
from ntrust import jnscore
from ntrust import anonpred
import ntrust.content
import ntrust.byline
COLLNAME_NEWS_SRC = "news_src"
COLLNAME_NEWS = "news"
COLLNAME_NEWS_ENTITY = "news_entity"
hannanum = Hannanum()
kkma = Kkma()
komoran = Komoran()
twitter = Twitter()
def get_metric_update_dicts(params):
import helper.entity_coll
title = params["title"]
content = params["content"]
media_id = params["mediaId"] if "mediaId" in params else None
image_count = params["imageCount"] if "imageCount" in params else 0
main_sets = dict()
main_unsets = dict()
# title
#main_sets["title"] = title
main_sets["title_length"] = textcount.length(title)
main_sets[jnscore.DOCKEY_TITLE_NUM_EXCLAMATION] = textcount.number_of_exclamation_marks(title)
main_sets[jnscore.DOCKEY_TITLE_NUM_QUESTION] = textcount.number_of_question_marks(title)
main_sets["title_numPuncs"] = main_sets["title_numExclamation"] + main_sets["title_numQuestion"]
main_sets["title_numSingleQuote"] = textcount.number_of_singlequote_marks(title)
main_sets["title_numDoubleQuote"] = textcount.number_of_doublequote_marks(title)
main_sets["title_hasShock"] = textcount.is_title_shock_ohmy(title)
main_sets["title_hasExclusive"] = textcount.is_title_exclusive_news(title)
main_sets["title_hasBreaking"] = textcount.is_title_breaking_news(title)
main_sets["title_hasPlan"] = textcount.is_title_plan_news(title)
# image
main_sets[jnscore.DOCKEY_IMAGE_COUNT] = image_count
# content
san = ntrust.content.Sanitizer(media_id)
san.process(content)
sancon = san.get_contents()
main_sets["content"] = sancon
content_length = textcount.length(sancon)
main_sets[jnscore.DOCKEY_CONTENT_LENGTH] = content_length
# 본문에서 바이라인 추출
bylines = san.get_bylines()
if bylines:
main_sets[jnscore.DOCKEY_BYLINES] = bylines
else:
main_unsets[jnscore.DOCKEY_BYLINES] = 1
# 인용문 추출
quotes = sentences.extract_quoted(sancon)
qarr = list()
total_quotes_len = 0
if quotes:
for qs in quotes:
if textcount.number_of_white_spaces(qs) >= 2:
qlen = textcount.length(qs)
total_quotes_len += qlen
qarr.append({"sentence":qs, "length":qlen})
main_sets[jnscore.DOCKEY_QUOTES] = qarr # 빈 qarr 도 저장하라고 해서..
main_sets[jnscore.DOCKEY_CONTENT_QUOTE_PERCENT] = (total_quotes_len / content_length) if content_length > 0 else 0
# 형태소 분석 및 인용문 앞부분 추출에 사용
lines = sancon.split("\n")
# 문장 갯수
num_sentence = len(lines)
main_sets["content_numSentence"] = num_sentence
# 문장 당 평균 길이
avgSentenceLength = 0
if num_sentence > 0:
sumSentenceLength = 0
for line in lines:
sumSentenceLength += len(line)
avgSentenceLength = float(sumSentenceLength) / num_sentence
main_sets[jnscore.DOCKEY_CONTENT_AVG_SENTENCE_LENGTH] = avgSentenceLength
# 인용문 앞부분 추출
if quotes:
for line in lines:
for qdic in main_sets[jnscore.DOCKEY_QUOTES]:
if "frontText" not in qdic and line.find(qdic["sentence"]) >= 0:
qdic["frontText"] = sentences.get_text_front_quote(line, qdic["sentence"])
# 형태소 분석
news_entity = dict()
num_adverbs = 0
num_adjectives = 0
num_conjunctions = 0
# Mecab
mecab_tags = []
mecab_start_time = timeit.default_timer()
for line in lines:
tagResults = tagger.merge(line, deep=False)
for tres in tagResults:
# print(tres)
if tres[0] == "":
continue
if '-' not in tres[2]:
pos = ','.join(tres[1])
mecab_tags.append({"word": tres[0], "pos": pos})
mecab_end_time = timeit.default_timer()
news_entity["mecab_time"] = mecab_end_time - mecab_start_time
news_entity["mecab_postag"] = mecab_tags
# print("Mecab: %f seconds: %s" % (update_set["mecab_time"], update_set["mecab_postag"]))
ec_mecab = helper.entity_coll.EntityCollector()
for item in mecab_tags:
for pos in item["pos"].split(","):
if pos.startswith('N'):
ec_mecab.feed(item["word"])
if pos.startswith("MAG"):
num_adverbs += 1
elif pos.startswith("MAJ"):
num_conjunctions += 1
elif pos.startswith("VA"):
num_adjectives += 1
ec_mecab.get_result("mecab_", news_entity)
# Hannanum
hannn_tags = []
hannn_start_time = timeit.default_timer()
for line in lines:
hpos = hannanum.pos(line)
for pos in hpos:
hannn_tags.append({"word": pos[0], "pos": pos[1]})
hannn_end_time = timeit.default_timer()
news_entity["hannanum_time"] = hannn_end_time - hannn_start_time
news_entity["hannanum_postag"] = hannn_tags
# print("Hannanum: %f seconds: %s" % (update_set["hannanum_time"], update_set["hannanum_postag"]))
ec_hannanum = helper.entity_coll.EntityCollector()
for item in hannn_tags:
if item["pos"].startswith('N'):
ec_hannanum.feed(item["word"])
ec_hannanum.get_result("hannanum_", news_entity)
# Kkma
kkma_tags = []
kkma_start_time = timeit.default_timer()
for line in lines:
kpos = kkma.pos(line)
for pos in kpos:
kkma_tags.append({"word": pos[0], "pos": pos[1]})
kkma_end_time = timeit.default_timer()
news_entity["kkma_time"] = kkma_end_time - kkma_start_time
news_entity["kkma_postag"] = kkma_tags
# print("Kkma: %f seconds: %s" % (update_set["kkma_time"], update_set["kkma_postag"]))
ec_kkma = helper.entity_coll.EntityCollector()
for item in kkma_tags:
if item["pos"].startswith('N'):
ec_kkma.feed(item["word"])
ec_kkma.get_result("kkma_", news_entity)
# Twitter
twit_tags = []
twit_start_time = timeit.default_timer()
for line in lines:
tpos = twitter.pos(line)
for pos in tpos:
twit_tags.append({"word": pos[0], "pos": pos[1]})
twit_end_time = timeit.default_timer()
news_entity["twitter_time"] = twit_end_time - twit_start_time
news_entity["twitter_postag"] = twit_tags
# print("Twitter: %f seconds: %s" % (update_set["twitter_time"], update_set["twitter_postag"]))
ec_twit = helper.entity_coll.EntityCollector()
for item in twit_tags:
if item["pos"].startswith('N'):
ec_twit.feed(item["word"])
ec_twit.get_result("twitter_", news_entity)
# 문장 당 평균 부사 수
main_sets["content_numAdverb"] = num_adverbs
main_sets["content_numAdjective"] = num_adjectives
main_sets["content_numConjunction"] = num_conjunctions
main_sets["content_avgAdverbsPerSentence"] = (num_adverbs / num_sentence) if num_sentence > 0 else 0
# 본문에 수치 인용 개수
num_numbers = 0
postags = news_entity["mecab_postag"]
for item in postags:
if item["pos"].startswith('SN'):
num_numbers += 1
main_sets[jnscore.DOCKEY_CONTENT_NUM_NUMBER] = num_numbers
main_sets["content_anonPredicates"] = anonpred.find_anonymous_predicates(sancon)
# TODO
#main_sets["content_numForeignWord"] = 0
main_sets["informant_real"] = []
main_sets["quotes_ratioRealAnon"] = 0
# 제목에 부사 수
title_adverbs = []
titlePos = kkma.pos(title)
for pos in titlePos:
if pos[1] == "MAG":
title_adverbs.append(pos[0])
main_sets["title_adverbs"] = title_adverbs
return ({"$set":main_sets, "$unset":main_unsets}, news_entity)
def process_basic_measurements(ver, param):
news_id = param["newsId"]
coll_src = helper.mongo.get_collection(COLLNAME_NEWS_SRC)
coll_dst = helper.mongo.get_collection(COLLNAME_NEWS)
coll_ett = helper.mongo.get_collection(COLLNAME_NEWS_ENTITY)
print("Begin %s" % (news_id))
src = coll_src.find_one({"newsitem_id":news_id})
if src is None:
helper.eventlog.error("Source news item not found: %s" % news_id)
return
chgs_news, chgs_entity = get_metric_update_dicts({
"mediaId": src['media_id'],
"title": src["title"],
"content": src["news_content"],
"imageCount":src["news_img_cnt"]
})
main_sets = chgs_news["$set"]
main_sets["runSpec"] = 3
main_sets["runDate"] = datetime.datetime.utcnow()
writer_byline = src["writer_byline"]
writer_email = src["writer_email"]
writer_post = src["writer_post"]
if len(writer_byline) > 1:
wr = dict();
if len(writer_post) > 1:
wr["post"] = writer_post
if len(writer_email) > 1:
wr["email"] = writer_email
if ntrust.byline.find_likely_email(writer_byline):
# 바이라인에 이메일이 포함되어 있는 경우가 있다.
wbyline = ntrust.byline.BylineExtractor(writer_byline).get_result()
if "name" in wbyline:
wr["name"] = wbyline["name"]
if "email" in wbyline:
wr["email"] = wbyline["email"]
else:
wr["name"] = writer_byline
main_sets["byline_writer"] = wr
# 경향신문은 바이라인이 본문에 없고 byline_writer 에 값이 있는 경우가 다수 발견,
# main_sets["bylines"] 가 비었을 경우 byline_writer 를 넣도록 한다.
if "bylines" not in main_sets and len(wr) > 0:
main_sets["bylines"] = [wr]
chgs_news["$unset"].pop("bylines",None)
else:
chgs_news["$unset"]["byline_writer"] = 1
if len(chgs_news["$unset"]) == 0:
chgs_news.pop("$unset")
coll_dst.update_one({"newsId": news_id}, chgs_news, upsert=False)
chgs_entity["newsId"] = news_id;
coll_ett.replace_one({"newsId": news_id}, chgs_entity)
# 2017.12.12 기사평가로직 추가
doc = coll_dst.find_one({"newsId":news_id})
category = doc["categoryFinal"]
mediaType = "방송" if doc["mediaName"] in jnscore.BROADCASTS else "신문"
jsco = jnscore.evaluate(category, mediaType, doc)
coll_dst.update_one({"newsId": news_id}, {"$set":{
"journal":jsco.journal,
"journal_totalSum": jsco.journalSum,
"vanilla": jsco.vanilla,
"vanilla_totalSum": jsco.vanillaSum,
"score": jsco.scores
}}, upsert=False)
print(" Done %s: %s" % (news_id, src["title"]))
if __name__ == "__main__":
print("Do not execute this script standalone.")