本文整理汇总了Python中util.ElementHelper.get_element_preorder_num方法的典型用法代码示例。如果您正苦于以下问题:Python ElementHelper.get_element_preorder_num方法的具体用法?Python ElementHelper.get_element_preorder_num怎么用?Python ElementHelper.get_element_preorder_num使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类util.ElementHelper
的用法示例。
在下文中一共展示了ElementHelper.get_element_preorder_num方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_title_util
# 需要导入模块: from util import ElementHelper [as 别名]
# 或者: from util.ElementHelper import get_element_preorder_num [as 别名]
def get_title_util(body, title_text):
if len(title_text) < 2:
return None
#1.计算节点文本与title_text的longest commen lenght
candidate_nodes = []
for tag in TITLE_TAG:
nodes = ElementHelper.get_element_by_tag(body, tag)
if nodes is None or len(nodes)<1: continue
nodes = [node for node in nodes if is_possible_title_tag(node)]
candidate_nodes.extend(nodes)
mapping = {}
for node in candidate_nodes:
node_text = ElementHelper.element_text_content(node)
# if len(node_text)==0 or len(node_text)>len(title_text): continue #
if len(node_text)==0: continue
llength = longest_common_length(node_text, title_text)
if llength >= 1:
mapping[node] = llength
if len(mapping)==0: return None
#2.选择长度最大的作为title节点,如果存在多个最大的,选择最靠前的作为
#title节点
sorted_nodes = [node for node, _ in sorted(mapping.items(), key=lambda x:x[1], reverse=True)]
max_len = mapping[sorted_nodes[0]]
candidates = [node for node in sorted_nodes if mapping[node]==max_len]
if len(candidate_nodes)==1:
return sorted_nodes[0]
else:
candidates.sort(cmp=lambda x,y: ElementHelper.get_element_preorder_num(x)- ElementHelper.get_element_preorder_num(y), reverse=False)
return candidates[0]
示例2: clean_body
# 需要导入模块: from util import ElementHelper [as 别名]
# 或者: from util.ElementHelper import get_element_preorder_num [as 别名]
def clean_body(clusters, doctree, title_node=None, debug = False):
#filter user comments and all link records
clusters = filter_cluster(clusters)
if len(clusters) == 0:
title = ElementHelper.element_text_content(title_node)
return title, title
#choose cluster which has most texts
maxCluster = get_biggest_cluster(clusters)
nodes = clusters[maxCluster]
# m = {}
# m[maxCluster] = nodes
# filter_cluster(m)
#get all children of max cluster record
allnodes = []
for node in nodes:
children = ElementHelper.get_children(node)
allnodes.extend(children)
#sort max cluster nodes by its preorder index
allnodes.sort(lambda p,q:ElementHelper.get_element_preorder_num(p)-ElementHelper.get_element_preorder_num(q))
# debug=True
if debug:
print 'maxCluster: %s' % maxCluster
for n in allnodes:
print ElementHelper.get_xpath_by_element(n, doctree), n.get(py)
s = ElementHelper.get_element_preorder_num(allnodes[0])
t = ElementHelper.get_element_preorder_num(allnodes[-1])
#correct start position by title node
title_text = ''
#====================================================================
#在实验时借助title纠正正文文本的起始位置可以提高recall,但是实际过程中不需要
if title_node is not None:
index = ElementHelper.get_element_preorder_num(title_node)
if index < s:
s = index #ToDo:add 2016/03/09
title_text = ElementHelper.element_text_content(title_node)
#====================================================================
body = ElementHelper.get_body(doctree)
# remove nodes which not belong to main text
set_text_mark(body, s, t)
remove_nontext_element(body)
return body, title_text
示例3: get_article_wish
# 需要导入模块: from util import ElementHelper [as 别名]
# 或者: from util.ElementHelper import get_element_preorder_num [as 别名]
def get_article_wish(clusters, doctree, title_node=None, debug = False):
#filter
clusters = filter_cluster(clusters)
if len(clusters) == 0:
return ElementHelper.element_text_content(title_node)
maxCluster = get_biggest_cluster(clusters)
nodes = clusters[maxCluster]
m = {}
m[maxCluster] = nodes
filter_cluster(m)
#============================================
if debug:
# for test
print 'maxCluster: %s' %maxCluster
for n in nodes:
print ElementHelper.get_xpath_by_element(n, doctree)
allnodes = []
for node in nodes:
children = ElementHelper.get_children(node)
allnodes.extend(children)
allnodes.sort(lambda p,q:ElementHelper.get_element_preorder_num(p)-ElementHelper.get_element_preorder_num(q))
s = ElementHelper.get_element_preorder_num(allnodes[0])
t = ElementHelper.get_element_preorder_num(allnodes[-1])
title_text = ''
if title_node is not None:
index = ElementHelper.get_element_preorder_num(title_node)
if index < s:
s = index
title_text = ElementHelper.element_text_content(title_node)
body = ElementHelper.get_body(doctree)
set_text_mark(body, s, t)
remove_nontext_element(body)
# ElementHelper.print_element(body)
return ElementHelper.element_text_content_list(body), title_text
示例4: set_text_mark
# 需要导入模块: from util import ElementHelper [as 别名]
# 或者: from util.ElementHelper import get_element_preorder_num [as 别名]
def set_text_mark(element, s, t):
for child in element:
set_text_mark(child, s, t)
pos = ElementHelper.get_element_preorder_num(element)
if pos >=s and pos<=t:
element.set(kg_text_mark, '1')
parent = element.getparent()
while parent.tag != 'html':
parent.set(kg_text_mark, '2')
parent = parent.getparent()