本文整理汇总了Python中util.ElementHelper.get_children方法的典型用法代码示例。如果您正苦于以下问题:Python ElementHelper.get_children方法的具体用法?Python ElementHelper.get_children怎么用?Python ElementHelper.get_children使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类util.ElementHelper
的用法示例。
在下文中一共展示了ElementHelper.get_children方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: similar_check
# 需要导入模块: from util import ElementHelper [as 别名]
# 或者: from util.ElementHelper import get_children [as 别名]
def similar_check(cls, nodeA, nodeB):
if nodeA.tag != nodeB.tag:
return False
#compare distinct nodes
dnodesA = ElementHelper.get_children(nodeA)
dnodesB = ElementHelper.get_children(nodeB)
#dA is node_levels_mapping, rA is level_nodes_mapping
dA, dB, rA, rB = {}, {}, {}, {}
for node in dnodesA:
#ignore <a> tag as distinct tag
if node.tag == 'a': continue
dA.setdefault(node.tag, []).append(int(node.get(px)))
rA.setdefault(int(node.get(px)), []).append(node.tag)
for node in dnodesB:
if node.tag == 'a': continue
dB.setdefault(node.tag, []).append(int(node.get(px)))
rB.setdefault(int(node.get(px)), []).append(node.tag)
if abs(len(dA)-len(dB))>1 or abs(len(rA)-len(rB))>1:
return False
#check distinct tag is same?
for tag in dA:
if tag not in ('em', 'b', 'br','i', 'font') and tag not in dB:
return False
sumA = sum([len(StringHelper.unique(rA[A])) for A in rA])
sumB = sum([len(StringHelper.unique(rB[B])) for B in rB])
if abs(sumA-sumB)>1:
return False
return True
示例2: is_cluster_all_links
# 需要导入模块: from util import ElementHelper [as 别名]
# 或者: from util.ElementHelper import get_children [as 别名]
def is_cluster_all_links(cluster):
""" #p判断是否是链接节点的集合。1.如果该集合中所有的文本节点都是链接节点,则属于链接噪声<a> text </a>或<li><a>text</a></li>的形式
if all tags which contain links are <a> tag, then return True
For example:
<a> link </a>
OR
<li> <a> link </a> </li>
"""
all_nodes_contain_text = []
for node in cluster:
children = ElementHelper.get_children(node)
nodes_contain_text = [node for node in children if not ElementHelper.is_element_text_none(node)
and node.tag not in ('em','strong','span','i','b')]
all_nodes_contain_text.extend(nodes_contain_text)
link_nodes = [node for node in all_nodes_contain_text if node.tag=='a' or node.getparent().tag=='a']
other_nodes = [node for node in all_nodes_contain_text if node.tag!='a' and node.getparent().tag != 'a']
link_nodes_text_number = cluster_text_number(link_nodes)
other_nodes_text_number = cluster_text_number(other_nodes)
if len(other_nodes)==0 or other_nodes_text_number==0:
return True
if 1.0 *link_nodes_text_number/other_nodes_text_number>2.0:
return True
return False
示例3: is_node_or_children_record
# 需要导入模块: from util import ElementHelper [as 别名]
# 或者: from util.ElementHelper import get_children [as 别名]
def is_node_or_children_record(cls, element):
children = ElementHelper.get_children(element)
marks = [child.get(kg_record_mark) for child in children]
unique_marks = StringHelper.unique(marks)
if len(unique_marks)==2:
return True
return False
示例4: clean_body
# 需要导入模块: from util import ElementHelper [as 别名]
# 或者: from util.ElementHelper import get_children [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
示例5: get_article_wish
# 需要导入模块: from util import ElementHelper [as 别名]
# 或者: from util.ElementHelper import get_children [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
示例6: is_cluster_contain_user_comments
# 需要导入模块: from util import ElementHelper [as 别名]
# 或者: from util.ElementHelper import get_children [as 别名]
def is_cluster_contain_user_comments(cluster):
""" identify whether element or its children contain comment content, only consider <a> tag
1.each node in cluster, at least has 3 children
2.there is at least one <a> tag has same text
"""
# can not identify
if len(cluster) < 2: return False
text_number_mapping = {}
#at least have three children contain text
for node in cluster:
children = ElementHelper.get_children(node)
link_nodes_contain_text = [n for n in children if is_link_node_with_text(n)]
non_link_nodes_contain_text = [n for n in children if is_none_link_node_with_text(n)]
if len(link_nodes_contain_text)<3: return False
if len(non_link_nodes_contain_text)<2: return False
for n in link_nodes_contain_text:
text = ElementHelper.element_text_content(n)
if text in text_number_mapping:
text_number_mapping[text] += 1
else:
text_number_mapping[text] = 1
#去除标点符号,出数字,空的文本
tmp = copy.deepcopy(text_number_mapping)
for text in tmp:
if len(text)==0 or StringHelper.is_digits(text) :
del text_number_mapping[text]
text_number = text_number_mapping.values()
# FOR TEST
# for text, number in node_text_mapping.items():
# print text,':', number
text_number_counter = collections.Counter(text_number).most_common()
for number, counter in text_number_counter:
if number > 1 and number==len(cluster) and counter>=2: #ToDo 2016/03/08 old:counter>=2 --> new:counter>=1
print 'find comment!'
return True
return False