brendenlake / MLC

Meta-Learning for Compositionality (MLC) for modeling human behavior
137 stars 18 forks source link

equiv_to_miniscan function missing from datasets module #1

Open norrrbit opened 9 months ago

ayushchakravarthy commented 5 months ago
def equiv_to_miniscan(G_source_str):
    # Is the grammar G_source_Str equivalent to the MiniScan grammar?
    #
    # Input
    #  G_source_str : grammar as string (with \n separators). candidate grammar to compare
    # Output
    #  true/false : is it equivalent to MiniSCAN grammar?
    assert(isinstance(G_source_str, str) and '\n' in G_source_str)
    G_source_rules = G_source_str.split('\n') # list of rules
    my_rules = [r.strip() for r in G_source_rules[4:7]]
    my_rules_possible_thrice = [change_rule_name(r,'thrice') for r in my_rules]
    my_rules_possible_surround = [change_rule_name(r,'surround') for r in my_rules]
    my_rules_possible_after = [change_rule_name(r,'after') for r in my_rules]
    match = 'u1 thrice -> [u1] [u1] [u1]' in my_rules_possible_thrice
    match = match and 'u1 surround u2 -> [u1] [u2] [u1]' in my_rules_possible_surround
    match = match and 'x1 after x2 -> [x2] [x1]' in my_rules_possible_after
    return match

def change_rule_name(rule_str,new_name):
    # Input
    #  rule_str : (str) rule in string format
    #  new_name : (str) rename the function to this string
    s = rule_str.split()
    s[1] = new_name
    return ' '.join(s)   

Here are the functions!