Datasets:

Modalities:
Image
Text
Formats:
parquet
ArXiv:
Tags:
code
Libraries:
Datasets
Dask
License:

Can you elaborate on the implementation of TreeBLEU?

#3
by Liangxiaoyun - opened

Hello, can you elaborate on the implementation of TreeBLEU,It would be better if the code can be open sourced.

xcodemind org

Sorry, we plan to elaborate on the implementation of TreeBLEU in the new publication. We provide the code directly for your reference.
Here we use the calculated tree_rouge_1 as the TreeBLEU score (CodeBLEU also takes the recall score as metric exactly.).
All the code is detailed below:

class HTMLMulNode:
    def __init__(self, name):
        self.childs = []
        self.name = name
        self.parent = None
        self.depth = 0

    def add_child(self, ch):
        self.childs.append(ch)
        ch.parent = self
        ch.depth = self.depth + 1      

def html2tree(html: str, drop_leaves=True):
    soup = BeautifulSoup(html, "html.parser")
    nodes = []

    def dfs(html_element: Tag, parent: HTMLMulNode):
        name = html_element.name if html_element.name else str(html_element.strip())
        new_node = HTMLMulNode(name)
        nodes.append(new_node)
        if parent is None:
            new_node.depth = 0
        else:
            parent.add_child(new_node)
        if html_element.name and html_element.contents:
            for child in html_element.contents:
                if child and str(child).strip() and html_element is not child:
                    if not (drop_leaves and child.name is None):
                        dfs(child, new_node)
        return new_node

    if soup.html:
        dfs(soup.html, None)         
    return nodes

def dom_sim(ref:str, cand:str):
    """
    subtree matching [1],
    match_score = count(cand matched in ref)/count(subtrees number of ref)
    
    [1] Ren, Shuo et al. “CodeBLEU: a Method for Automatic Evaluation of Code Synthesis.” ArXiv abs/2009.10297 (2020): n. pag.
    """

    ref_tree_nodes = html2tree(ref)
    cand_tree_nodes = html2tree(cand)
    
    if len(ref_tree_nodes) == 0 or len(ref_tree_nodes) == 0:
        return 0,0
    
    def collect_all_subtrees(nodes, height=1):
        subtrees = []
        for node in nodes:
            if len(node.childs) == 0:
                continue
            names = [node.name.strip().lower()]
            for child in node.childs:
                names.append(child.name.strip().lower())            
            
            subtrees.append('_'.join(names))
        return subtrees
    
    ref_subtree_seqs = collect_all_subtrees(ref_tree_nodes)
    cand_subtree_seqs = collect_all_subtrees(cand_tree_nodes)
    
    match_count = 0
    for seq in set(cand_subtree_seqs):
        if seq in set(ref_subtree_seqs):
            match_count += 1
    
    tree_rouge_1 = match_count/len(set(ref_subtree_seqs))
    
    match_count = 0
    for seq in cand_subtree_seqs:
        if seq in set(ref_subtree_seqs):
            match_count += 1
        
    tree_bleu = match_count/len(cand_subtree_seqs)
    
    return tree_bleu, tree_rouge_1
starmage520 changed discussion status to closed
Your need to confirm your account before you can post a new comment.

Sign up or log in to comment