-
Notifications
You must be signed in to change notification settings - Fork 1
/
extractor.py
416 lines (352 loc) · 14.8 KB
/
extractor.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import logging
from lxml import etree, html
from text import TextHandler
import util
import math
class ContentExtractor(object):
"""docstring for ContentExtractor"""
def __init__(self, config):
super(ContentExtractor, self).__init__()
self.xtopnodetags = etree.XPath("//*[self::p or self::td or self::pre]")
#self.xlinks = etree.XPath("./a|./*/a")
self.config = config
self.texthandler = config.texthandler()
#self.xparas = etree.XPath("./p|./*/p")
#self.config = config
def gettitle(self, doc):
#select title
titleelems = doc.cssselect('title')
title = None
if not titleelems or len(titleelems) == 0 :
# try to get meta title
title = self.getmetacontent(doc, "meta[name=title]")
else:
title = titleelems[0].text
if not title: return None
#split title if it contains delim
delim = ''
if title.find('|') >= 0 :
#split pipe
delim = r'|'
elif title.find('-') >= 0 :
#split dash
delim = r'-'
elif title.find('»') >= 0:
#split arrow
delim = r'»'
elif title.find(':') >= 0:
#split colon
delim = r':'
#split and take the longest token
if delim:
tokens = title.split(delim)
mainTitle = ''
for tok in tokens:
if len(tok) > len(mainTitle):
mainTitle = tok
title = mainTitle
#replace '»' in title , motley replacement
title = title.replace('»','').replace('»','').replace('�','')
return title
def getmetacontent(self, doc, metaname):
metaElems = doc.cssselect(metaname)
if metaElems and len(metaElems)>0:
content = metaElems[0].get('content')
if content : content = content.strip()
return content
else:
return None
def getmetadesc(self, doc):
return self.getmetacontent(doc,'meta[name=description]')
def getmetakeywords(self,doc):
return self.getmetacontent(doc, 'meta[name=keywords]')
def getcanonicallink(self,doc):
links = doc.cssselect('link[ref=canonical]')
if links and len(links)>0:
href = links[0].get('href')
if href : href = href.strip()
return href
#else canonical link = article.finalUrl
return None
def getdomain(self, url):
from urllib.request import Request
return Request(url).host
def extracttags(self, doc):
#A_REL_TAG_SELECTOR: String = "a[rel=tag], a[href*=/tag/]"
if len(doc.getchildren()) == 0 :
#return empty set
return set()
tagSet = set()
tags = doc.cssselect('a[rel=tag],a[href*=tag]')
if tags and len(tags) > 0:
for t in tags:
tagSet.add(t.text)
#tags = doc.cssselect('a[href*=tag]')
#if tags and len(tags)>0:
# for t in tags:
# tagSet.add(t.text)
return tagSet
def getbestnodes_bsdoncluster(self, doc):
nodeswithtext = []
parentnodes = []
nodes = self.getnodestocheck(doc)
startboost = 1.0
count = 0
i = 0 #iteration
for node in nodes:
#logging.debug("checking %s node id=%s class=%s "% (node.tag, node.get('id'), node.get('class')))
#logging.debug(util.getouterhtml(node))
#logging.debug("\n")
nodetext = util.getinnertext(node)
if nodetext!= None:
#wordstats = self.texthandler.getstopwordscount(nodetext)
linkdense = self.ishighlinkdensity(node)
if(self.getrelevancescore(nodetext) > self.getcutoffscore() and not linkdense ):
nodeswithtext.append(node)
logging.debug("To inspect %d nodes with text " % len(nodeswithtext))
negativescore = 0
bottomnode_for_negativescore = len(nodeswithtext) * 0.25
for node in nodeswithtext:
boostscore = 0
if self.isboostable(node) :
if count >= 0:
boostscore = ( 1.0/ startboost * 50)
startboost += 1
if len(nodeswithtext) > 15:
# for nodes that fall in bottom 25%
if (len(nodeswithtext) - i) <= bottomnode_for_negativescore:
booster = bottomnode_for_negativescore - (len(nodeswithtext) - i )
boostscore = -math.pow(booster, 2)
negscore = math.fabs(boostscore) + negativescore
if negscore > 40:
boostscore = 5
logging.debug("Location boost score %d on iteration %d id='%s' class='%s' tag='%s'" % (boostscore, i, node.getparent().get('id'), node.getparent().get('class'), node.getparent().tag ))
nodetext = util.getinnertext(node)
#logging.debug(nodetext)
#ws = self.texthandler.getstopwordscount(nodetext)
#upscore = ws.stopwordcount + boostscore
upscore = self.getrelevancescore(nodetext) + boostscore
logging.debug("total upscore = %f " % upscore )
parent = node.getparent()
grandpar = node.getparent().getparent()
self._score(parent, upscore)
self._score(grandpar, upscore/2)
self._nodecount(parent, 1)
self._nodecount(grandpar,1)
try:
parentnodes.index(parent)
except ValueError:
parentnodes.append(parent)
try:
parentnodes.index(grandpar)
except ValueError:
parentnodes.append(grandpar)
count += 1
i += 1
topnodescore = 0
topnode = None
for node in parentnodes:
logging.debug("Parent Node: score=%s nodeCount=%s id=%s class=%s tag=%s" % (self._score(node),self._nodecount(node),node.get('id'),node.get('class'), node.tag))
score = self._score(node)
if score > topnodescore:
topnode = node
topnodescore = score
if topnode is None:
topnode = node
return topnode
def _score(self, node, addscore=None):
"""get or add score to a node"""
score = node.get('score')
score = float(score) if score is not None else 0
if addscore is not None:
score += addscore
node.set('score', str(score))
return score
def _nodecount(self, node, addcount=None):
"""get or add childnode count to a parent node"""
count = node.get('nodecount')
count = int(count) if count is not None else 0
if addcount is not None:
count += addcount
node.set('nodecount', str(count))
return count
def getrelevancescore(self, textcontent):
return self.texthandler.gettextscore(textcontent)
def getcutoffscore(self):
"""no of stopwords for a node to be non-trivial"""
return self.texthandler.getcutoff()
def getnodestocheck(self, doc):
tocheck = self.xtopnodetags(doc)
return tocheck
def ishighlinkdensity(self, node):
"""check if a node contains lots of links"""
text = util.getinnertext(node, True)
if not text:
return False
words = self.texthandler.splittext(text)
linkbuffer = []
for link in node.iterdescendants('a'):
if link.text != None:
linkbuffer.append(link.text)
if len(linkbuffer) == 0:
return False
linktext = ' '.join(linkbuffer)
linkwords = self.texthandler.splittext(linktext)
linkdivisor = len(linkwords)/len(words)
score = linkdivisor * len(linkbuffer)
logging.debug("Link density score is %f for node %s"%(score, self._getshorttext(node)))
return score > 1
def _getshorttext(self,node):
return etree.tostring(node).decode('utf-8')[:50]
def isboostable (self, node ):
""" make sure that the node is a paragraph, and connected to other paragraph """
stepsaway = 0
minscoretoboost = self.texthandler.getminboostable()
maxstepsaway = 3
for sib in node.itersiblings(preceding=True):
if(sib.tag == 'p'):
if stepsaway >= maxstepsaway:
logging.debug("Next paragraph is too farway, not boost")
return False
paratext = util.getinnertext(sib)
if paratext != None:
#ws = self.texthandler.getstopwordscount(paratext)
if self.getrelevancescore(paratext) > minscoretoboost:
logging.debug("Boosting this node")
return True
stepsaway += 1
return False
def postextractionclean(self, topnode):
"""remove any divs that looks like non-content, link clusters"""
node = self.addsiblings(topnode)
for child in node.iterchildren():
if child.tag != 'p':
if self.ishighlinkdensity(child) or self.istablenopara(child) or not self.isthresholdmet(child):
#logging.info("Removing node tag %s with text : %s " % (child.tag, util.getouterhtml(child)))
node.remove(child)
return node
def getbaselinescoreforsiblings(self, topnode):
"""get base score against average scoring of paragraphs within topnodes. Siblings must have higher score than baseline"""
base = 100000
numparas = 0
scoreparas = 0
#nodestocheck = self.xparas(topnode)
for node in topnode.iterdescendants('p'):
nodetext = util.getinnertext(node)
#ws = self.texthandler.getstopwordscount(nodetext)
if nodetext:
relscore = self.getrelevancescore(nodetext)
linkdense = self.ishighlinkdensity(topnode)
if(relscore > self.getcutoffscore() and not linkdense):
numparas += 1
scoreparas += relscore#ws.stopwordcount
if numparas > 0:
base = scoreparas/ numparas
return base
def addsiblings(self, topnode):
""" add content of siblings that are likely to be meaning ful to the topnode"""
logging.debug("Start adding siblings")
baselinescore = self.getbaselinescoreforsiblings(topnode)
siblingcontent = []
for sib in topnode.itersiblings(preceding=True):
content = self.getsiblingcontent(sib, baselinescore)
if content:
siblingcontent.append(content)
for content in siblingcontent:
paras = html.fragments_fromstring(content)
for p in reversed(paras):
self._insertFirst(topnode, p)
return topnode
def _insertFirst(self, parnode, node):
childnodes = parnode.getchildren()
if childnodes and len(childnodes) > 0:
first = childnodes[0]
first.addprevious(node)
else:
parnode.append(node)
# preserve the relative order of text blks
if parnode.text:
node.tail = parnode.text
parnode.text = None
def getsiblingcontent(self, currentsibling, basescore):
if currentsibling.tag == 'p':
siblingtext = util.getinnertext(currentsibling, True)
if siblingtext is not None and len(util.getinnertext(currentsibling)) > 0:
return util.getouterhtml(currentsibling)
alltext = []
for para in currentsibling.iterdescendants('p'):#self.xparas(currentsibling):
text = util.getinnertext(para)
if text and len(text) > 0 :
ws = self.texthandler.getstopwordscount(text)
parascore = ws.stopwordcount
if basescore * 0.30 < parascore:
alltext.append("<p>" + text + "</p>")
if len(alltext) > 0:
return " ".join(alltext)
else:
return None
def istablenopara(self, node):
for subpara in node.iterdescendants('p'):
paratext = util.getinnertext(subpara,True)
if paratext is None or len(paratext) < 25:
parent = subpara.getparent()
if parent:
logging.debug("removing node %s" % subpara.tag)
parent.remove(subpara)
#subparas = self.xparas(node)
iterpar = node.iterdescendants('p')
try:
p = next(iterpar)
return False
except StopIteration:
logging.debug("YES")
if node.tag != 'td':
return True
def isthresholdmet(self, node):
topnode = node.getparent()
topnodescore = self._score(topnode)
curscore = self._score(node)
threshold = topnodescore * 0.08
logging.debug("curscore = %f , threshold = %f "%(curscore, threshold))
if curscore < threshold and node.tag != 'td':
return False
else:
return True
class StandardContentExtractor(ContentExtractor):
"""standard extension of ContentExtractor.
Add no new features at the moment"""
def __init__(self, config):
super(StandardContentExtractor,self).__init__(config)
class LengthbsdContentExtractor(ContentExtractor):
"""Extract article using text length as scoring instead of stopword count"""
def __init__(self):
super(LengthbsdContentExtractor,self).__init__()
def getcutoffscore(self):
return 25
def getrelevancescore(self,content):
return self.texthandler.getwordscount(content)
def isboostable(self, node):
""" make sure that the node is a paragraph, and connected to other paragraph """
stepsaway = 0
minscoretoboost = 50
maxstepsaway = 3
for sib in node.itersiblings(preceding=True):
if(sib.tag == 'p'):
if stepsaway >= maxstepsaway:
logging.debug("Next paragraph is too farway, not boost")
return False
paratext = util.getinnertext(sib)
if paratext != None:
#ws = self.texthandler.getstopwordscount(paratext)
if self.getrelevancescore(paratext) > minscoretoboost:
logging.debug("Boosting this node")
return True
stepsaway += 1
return False
class PublishDateExtractor(object):
"""docstring for PublishDateExtractor"""
def __init__(self):
super(PublishDateExtractor, self).__init__()
#pending not yet implemented
def extract(self, doc):
return None