當前位置: 首頁>>代碼示例>>Python>>正文


Python ElementHelper.get_element_preorder_num方法代碼示例

本文整理匯總了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]
開發者ID:actlea,項目名稱:TopicalCrawler,代碼行數:37,代碼來源:api2.py

示例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
開發者ID:actlea,項目名稱:TopicalCrawler,代碼行數:51,代碼來源:api.py

示例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
開發者ID:actlea,項目名稱:TopicalCrawler,代碼行數:49,代碼來源:api3.py

示例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()
開發者ID:actlea,項目名稱:TopicalCrawler,代碼行數:13,代碼來源:api2.py


注:本文中的util.ElementHelper.get_element_preorder_num方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。