Example #1
0
def read_tree(line):
    parents = map(int, line.split())
    trees = dict()
    root = None
    for i in xrange(1, len(parents) + 1):
        # if not trees[i-1] and parents[i-1]!=-1:
        if i - 1 not in trees.keys() and parents[i - 1] != -1:
            idx = i
            prev = None
            while True:
                parent = parents[idx - 1]
                if parent == -1:
                    break
                tree = Tree()
                if prev is not None:
                    tree.add_child(prev)
                trees[idx - 1] = tree
                tree.idx = idx - 1
                # if trees[parent-1] is not None:
                if parent - 1 in trees.keys():
                    trees[parent - 1].add_child(tree)
                    break
                elif parent == 0:
                    root = tree
                    break
                else:
                    prev = tree
                    idx = parent
    return root
Example #2
0
def solveLinear (poly): # solving via -b/m
  m = Tree(poly.coeffs[1])
  b = Tree(poly.coeffs[0])
  answer = Tree("=", OP, None, None)
  answer.left = Tree(Polynomial(poly.var))
  temp = Tree("-", OP, 0, b)
  answer.right = Tree("/", OP, temp, m)
  if INTERMEDIATE: print str(answer), "\t(Solving linear equation)"
  answer.right = evalTree(answer.right)
  return answer
Example #3
0
def create_tree(array, tree=Tree(), nodes=[]):
    if len(array) == 0:
        return tree

    if tree.root is None:
        tree = Tree()
        root = TreeNode(array[0])
        tree.root = root
        return create_tree(array[1:], tree, [root])

    created = _bulk_create_children(parents=nodes, array=array[:len(nodes)*2])

    return create_tree(array[len(created):], tree, created)
Example #4
0
def replace_constituent(tree, constituent, new_constituent):
  """
  Replaces one constituent in this tree with another.
  """
  # We injected span, so the standard __eq__ check doesn't look for it
  if tree == constituent and (not isinstance(tree, Tree) or tree.span ==
      constituent.span):
    return new_constituent
  if not isinstance(tree, Tree):
    return tree
  n_tree = Tree(tree.node, [replace_constituent(subtree, constituent,
    new_constituent) for subtree in tree])
  n_tree.span = tree.span
  return n_tree
Example #5
0
def replace_constituent(tree, constituent, new_constituent):
    """
  Replaces one constituent in this tree with another.
  """
    # We injected span, so the standard __eq__ check doesn't look for it
    if tree == constituent and (not isinstance(tree, Tree)
                                or tree.span == constituent.span):
        return new_constituent
    if not isinstance(tree, Tree):
        return tree
    n_tree = Tree(tree.node, [
        replace_constituent(subtree, constituent, new_constituent)
        for subtree in tree
    ])
    n_tree.span = tree.span
    return n_tree
Example #6
0
def sample_tree(max_level, train=True):
    tree = Tree()
    if train:
        pattern = random.sample(training_patterns,
                                1)[0]  # sample a pattern for training data
    else:
        pattern = random.sample(test_patterns,
                                1)[0]  # sample a pattern for test data
    tree = expand_tree(tree, 0, None, [], 0, max_level, pattern)
    allign_tree(tree, 0)
    return tree
Example #7
0
def evalTree(tree): # evaluates and simplifies a tree (as far as it can go without hitting a variable) that you give it
  if tree.isNumber():
    return tree
  elif tree.isOperator(): # +, -, *, /
    left = evalTree(tree.left) # eval and set up copies of the tree branches
    right = evalTree(tree.right)
    if left.isOperator() or right.isOperator() or (left.isPoly() and right.isPoly() and right.value.var != left.value.var): # if left side is op, or right side is op, pass up. If both sides are vars, and they don't match, pass up.
      tree.left = left
      tree.right = right
      return tree    
    num1 = left.value
    num2 = right.value
    if tree.value == "+": answer = num1 + num2   # evaluate ops
    elif tree.value == "-": answer = num1 - num2
    elif tree.value == "*": answer = num1 * num2
    elif tree.value == "/": answer = num1 / num2
    elif tree.value == "^" or tree.value == "**": answer = num1 ** num2
    if INTERMEDIATE: print "\t"+str(num1)+str(tree.value)+str(num2)+"="+str(answer) # ...[+, -, *, /,etc.]...=...
    result = Tree(answer, NUMBER, None, None)
    
    if result.value.__class__ == Polynomial:
      result.type = NAME      
    return result
  elif tree.isFunction():
    num = evalTree(tree.left) # make sure it is a number
    if not num.isNumber(): # if not (such as var) pass it up
      return tree
    evalString = tree.value+"("+str(num.value)+")" # setting up the function...
    answer = eval(evalString) # ...and evaling it
    if INTERMEDIATE: print "\t"+evalString+"="+str(answer) # f(x)=...
    result = Tree(answer, NUMBER, None, None)
    return result
    
  elif tree.isPoly():
    return tree
  else:
    raise SyntaxError
Example #8
0
def sample_tree(max_layout_level,
                add_layout_prob,
                obj_count,
                zero_shot=False,
                train=True):
    tree = Tree()
    tree, obj_count = expand_tree(tree,
                                  0,
                                  None, [],
                                  0,
                                  max_layout_level,
                                  add_layout_prob,
                                  train,
                                  obj_count,
                                  zero_shot=zero_shot)
    allign_tree(tree, 0)
    return tree
Example #9
0
def solveQuad(poly): # Solve via quadratic formula
  a = Tree(poly.coeffs[2])
  b = Tree(poly.coeffs[1])
  c = Tree(poly.coeffs[0])
  discriminant = Tree(
    "sqrt",
    FUNCTION, 
    Tree("-", OP,
       Tree( "**", OP, b, 2),
      Tree("*", OP, 
        4, 
        Tree( "*", OP, a, c)),
	 ),
      None, 
  )
  answerLeft = Tree(Polynomial(poly.var))
  answer1 = Tree( "=", OP , answerLeft, None)  #may need Copy here
  answer1.right = Tree("/", OP,
    Tree( "+",  OP,
      	Tree( "-", OP, 0, b ), 
      discriminant,
		),
    Tree("*", OP, 2, a )
 )
  
  if INTERMEDIATE: print str(answer1), "\t(Solving quadratic equation; root 1)"
  answer1.right = evalTree(answer1.right)

  answer2 = Tree("=", OP, answerLeft, None)
  answer2.left = Tree(Polynomial(poly.var))
  answer2.right = Tree("/", OP,
    Tree( "-",  OP,
      	Tree( "-", OP, 0, b ), 
      discriminant,
		),
    Tree("*", OP, 2, a))
  
  if INTERMEDIATE: print str(answer2), "\t(Solving quadratic equation; root 2)"
  
  answer2.right = evalTree(answer2.right)
  return [answer1, answer2]
Example #10
0
    def extract_rules_corpus(self, nl_path, amr_path, alignment_path,
                             destination_prefix, composition_depth):
        """
    Extract all rules from the corpus specified by the *_path arguments.
    """

        syn_f = open(nl_path)
        sem_f = open(amr_path)
        align_f = open(alignment_path)

        n_examples = count_lines(amr_path)
        announce_interval = n_examples / 10

        # load input data into examples list
        examples = []
        for example_i in range(n_examples):
            syn_s = syn_f.readline().strip()
            sem_s = sem_f.readline().strip()
            align_s = align_f.readline().strip()

            amr = Dag.from_string(sem_s)
            tree = Tree(syn_s)
            label_spans(tree)
            align = get_alignments(align_s, amr)

            examples.append((amr, tree, align))

        # extract rules from data
        rules = []
        for example in examples:
            example_rules = extract_rules(example[0], example[1], example[2],
                                          composition_depth)
            rules += example_rules

        # assign ML weights by counting
        grammar = collect_counts(rules)
        Rule.write_to_file(grammar, destination_prefix)
Example #11
0
def expand_tree(tree, level, parent, memorylist, child_idx, max_level,
                metadata_pattern):
    if parent is None or parent.function == 'layout':
        if level + 1 >= max_level:
            valid = [1]
        else:
            valid = [0, 1]

        # sample module, the module can be either layout or describe here
        module_id = random.randint(0, len(valid) - 1)
        tree.function = module_list[valid[module_id]]

        # sample content
        dict_index = metadata_pattern[pattern_map[tree.function]]
        module_dict = module_dicts[dict_index]

        word_id = random.randint(0, len(module_dict[tree.function]) - 1)
        tree.word = module_dict[tree.function][word_id]

        if tree.function == 'layout':
            tree.function_obj = Layout(tree.word)
            print('add layout')
        else:
            tree.function_obj = Describe(tree.word)
            print('add describe')

        # num children
        if level + 1 > max_level:
            tree.num_children = 0
        else:
            tree.num_children = children_dict[tree.function]
            if parent is not None:  # then the parent must be a layout node
                if child_idx == 0:
                    parent.function_obj.left_child = tree.function_obj
                else:
                    parent.function_obj.right_child = tree.function_obj

        for i in range(tree.num_children):
            tree.children.append(Tree())
            tree.children[i] = expand_tree(tree.children[i], level + 1, tree,
                                           [], i, max_level, metadata_pattern)

    # must contain only one child node, which is a combine node
    elif parent.function == 'describe' or parent.function == 'combine':
        print('add combine')
        valid = [2]
        # no need to sample module for now
        module_id = 0
        tree.function = module_list[valid[module_id]]

        # sample content
        # sample which attributes
        if len(set(attribute_list) - set(memorylist)) <= 1:
            full_attribute = True
        else:
            full_attribute = False

        attribute = random.sample(set(attribute_list) - set(memorylist), 1)[0]
        memorylist += [attribute]

        dict_idx = metadata_pattern[pattern_map[attribute]]
        module_dict = module_dicts[dict_idx]
        word_id = random.randint(
            0,
            len(module_dict[tree.function][attribute]) - 1)
        tree.word = module_dict[tree.function][attribute][word_id]

        if isinstance(parent.function_obj, Describe):
            carrier = parent.function_obj
        else:
            carrier = parent.function_obj.get_carrier()

        tree.function_obj = Combine(attribute, tree.word)
        tree.function_obj.set_carrier(carrier)
        carrier.set_attribute(attribute, tree.function_obj)

        if not full_attribute:
            tree.num_children = children_dict[tree.function]

            for i in range(tree.num_children):
                tree.children.append(Tree())
                tree.children[i] = expand_tree(tree.children[i], level + 1,
                                               tree, memorylist, i, max_level,
                                               metadata_pattern)

    else:
        raise ValueError('Wrong function.')
    return tree
Example #12
0
import matplotlib.pyplot as plt
import numpy as np
import lib.node 
from sklearn.datasets import load_iris
from lib.tree import Tree

def execute(ax=None, **kwargs):
    ax = ax or plt.gca()
    line, = ax.plot (np.arange(0.0, 5.0, 0.02))
    return line

def plot():
    pass


if __name__ == "__main__":
    
    iris = load_iris()
	tree = Tree(iris.target, iris.data)
	nodes = tree.fit()
	print(tree.classify(iris.target[123],nodes))

    fig, [plot1, plot2] = plt.subplots(nrows=2)
    execute(plot1)
    execute(plot2)
    plt.show()
Example #13
0
    return tree_temp


def layout_mod(val):
    # st()
    trees, pos = val
    tree_temp = copy.deepcopy(layout)
    tree_temp.word = pos
    tree_temp.children = trees
    return tree_temp


layout_tree = True
if layout_tree:
    # val = []
    tree = Tree()
    for sent in paper:
        sent_temp = sent
        val = len(sent)
        assert (val + 1) % 3 == 0
        numObj = (val + 1) // 3
        # st()
        temp = []
        pos_l = []
        for obj in range(numObj):
            temp.append(combine_mod(sent[obj * 3:obj * 3 + 2]))
            if (obj * 3 + 2) < val:
                pos_l.append(sent[obj * 3 + 2])
        print([i.word for i in temp])
        print(pos_l)
        iterval = reversed(range(len(pos_l)))
Example #14
0
def sample_tree_flexible(percent_inside_samples,
                         include_inside_config,
                         max_layout_level,
                         add_layout_prob,
                         obj_count,
                         zero_shot=False,
                         train=True,
                         arguments=None,
                         back_front_only_flag=False):
    tree = Tree()

    if not include_inside_config:
        expand_func = expand_tree
    else:
        rand = random.random()
        if rand < percent_inside_samples:
            arguments = {'fix_num_objs': 2}
            expand_func = expand_tree_with_inside
            max_layout_level = 1
        else:
            expand_func = expand_tree

    if arguments is None:
        tree, obj_count = expand_func(
            tree,
            0,
            None, [],
            0,
            max_layout_level,
            add_layout_prob,
            train,
            obj_count,
            zero_shot=zero_shot,
            back_front_only_flag=back_front_only_flag)
    else:
        max_num_objs = arguments['max_num_objs']
        min_num_objs = arguments['min_num_objs']
        object_count_range = range(min_num_objs, max_num_objs + 1)

        tree, obj_count = expand_func(
            tree,
            0,
            None, [],
            0,
            max_layout_level,
            add_layout_prob,
            train,
            obj_count,
            zero_shot=zero_shot,
            back_front_only_flag=back_front_only_flag)
        num_objs = count_functions(tree, 'describe')
        while num_objs not in object_count_range:
            tree = Tree()
            tree, obj_count = expand_func(
                tree,
                0,
                None, [],
                0,
                max_layout_level,
                add_layout_prob,
                train,
                obj_count,
                zero_shot=zero_shot,
                back_front_only_flag=back_front_only_flag)
            num_objs = count_functions(tree, 'describe')

        # if 'max_num_objs' in arguments:
        #     max_num_objs = arguments['max_num_objs']
        #     tree, obj_count = expand_func(tree, 0, None, [], 0, max_layout_level, add_layout_prob, train, obj_count, zero_shot=zero_shot, back_front_only_flag=back_front_only_flag)
        #     num_objs = count_functions(tree, 'describe')
        #     while num_objs > max_num_objs:
        #         tree = Tree()
        #         tree, obj_count = expand_func(tree, 0, None, [], 0, max_layout_level, add_layout_prob, train, obj_count, zero_shot=zero_shot, back_front_only_flag=back_front_only_flag)
        #         num_objs = count_functions(tree, 'describe')
        #         print(num_objs)
        # elif 'fix_num_objs' in arguments:
        #     fix_num_objs = arguments['fix_num_objs']
        #     tree, obj_count = expand_func(tree, 0, None, [], 0, max_layout_level, add_layout_prob, train, obj_count, zero_shot=zero_shot, back_front_only_flag=back_front_only_flag)
        #     num_objs = count_functions(tree, 'describe')
        #     while num_objs != fix_num_objs:
        #         tree = Tree()
        #         tree, obj_count = expand_func(tree, 0, None, [], 0, max_layout_level, add_layout_prob, train, obj_count, zero_shot=zero_shot, back_front_only_flag=back_front_only_flag)
        #         num_objs = count_functions(tree, 'describe')
    allign_tree(tree, 0)

    return tree
Example #15
0
def expand_tree(tree,
                level,
                parent,
                memorylist,
                child_idx,
                max_layout_level,
                add_layout_prob,
                train,
                obj_count,
                zero_shot=False,
                metadata_pattern=None,
                back_front_only_flag=False):
    if parent is None or parent.function == 'layout':
        # sample module, the module can be either layout or describe here
        if level + 1 > max_layout_level:
            module_idx = 1
        else:
            rand = random.random()
            if rand >= 1 - add_layout_prob:
                module_idx = 0
            else:
                module_idx = 1
        tree.function = module_list[module_idx]
        if zero_shot and (level == 0 or tree.function == 'describe'):
            r = random.random()
            if train:
                metadata_pattern = _choose_pattern(zs_training_patterns,
                                                   zs_training_probs, r)
            else:
                metadata_pattern = _choose_pattern(zs_test_patterns,
                                                   zs_test_probs, r)
        # sample content
        if zero_shot:
            assert (metadata_pattern is not None)
            dict_index = metadata_pattern[pattern_map[tree.function]]
            module_dict = module_dicts_zeroshot[dict_index]
        elif back_front_only_flag:
            module_dict = module_dicts_back_front
        else:
            module_dict = module_dict_normal

        word_id = random.randint(0, len(module_dict[tree.function]) - 1)
        tree.word = module_dict[tree.function][word_id]

        if tree.function == 'layout':
            tree.function_obj = Layout(tree.word)
            # print('add layout')
        else:
            obj_count += 1
            tree.function_obj = Describe(tree.word, obj_count)
            # print('add describe')

        tree.num_children = children_dict[tree.function]
        if parent is not None:  # then the parent must be a layout node
            if child_idx == 0:
                parent.function_obj.left_child = tree.function_obj
            else:
                parent.function_obj.right_child = tree.function_obj

        for i in range(tree.num_children):
            tree.children.append(Tree())
            tree.children[i], obj_count = expand_tree(
                tree.children[i], level + 1, tree, [], i, max_layout_level,
                add_layout_prob, train, obj_count, zero_shot, metadata_pattern,
                back_front_only_flag)

    # must contain only one child node, which is a combine node
    elif parent.function == 'describe' or parent.function == 'combine':
        # print('add combine')
        valid = [2]
        # no need to sample module for now
        module_id = 0
        tree.function = module_list[valid[module_id]]

        # sample content
        # sample which attributes
        if len(set(attribute_list) - set(memorylist)) <= 1:
            full_attribute = True
        else:
            full_attribute = False

        attribute = random.sample(set(attribute_list) - set(memorylist), 1)[0]
        memorylist += [attribute]

        if zero_shot:
            assert (metadata_pattern is not None)
            dict_idx = metadata_pattern[pattern_map[attribute]]
            module_dict = module_dicts_zeroshot[dict_idx]
        else:
            module_dict = module_dict_normal

        word_id = random.randint(
            0,
            len(module_dict[tree.function][attribute]) - 1)
        tree.word = module_dict[tree.function][attribute][word_id]

        if isinstance(parent.function_obj, Describe):
            carrier = parent.function_obj
        else:
            carrier = parent.function_obj.get_carrier()

        tree.function_obj = Combine(attribute, tree.word)
        tree.function_obj.set_carrier(carrier)
        carrier.set_attribute(attribute, tree.function_obj)

        if not full_attribute:
            tree.num_children = children_dict[tree.function]

            for i in range(tree.num_children):
                tree.children.append(Tree())
                tree.children[i], obj_count = expand_tree(
                    tree.children[i], level + 1, tree, memorylist, i,
                    max_layout_level, add_layout_prob, train, obj_count,
                    zero_shot, metadata_pattern, back_front_only_flag)
    else:
        raise ValueError('Wrong function.')
    return tree, obj_count
Example #16
0
from lib.tree import Tree

tree = Tree(k=int(input('k: ')), k_leaf=int(input('k*: ')))

while True:
    command = input('>> ')
    if command == 'insert':
        tree.insert(int(input('Key: ')), input('Data: '))
    elif command == 'rangeinsert':
        for i in range(int(input('Start: ')), int(input('End: ')) + 1):
            tree.insert(i, str(i))
            print(str(i) + ': ' + str(tree))
    elif command == 'lookup':
        print(tree.lookup(int(input('Key: '))))
    elif command == 'print':
        print(tree)
    elif command == 'exit':
        break