Example #1
0
    def inference(self, test_set, real_test, golden_set):
        correct = 0
        total = 0
        result_dict = {}
        for sentence in test_set:
            graph_with_all_weights = self.feature_maker.create_weighted_graph_for_sentence(
                sentence, self.weights, test_set)
            maximum_spanning_tree = mst.mst(0, graph_with_all_weights)
            if not real_test:
                golden_standard = golden_set[sentence]
                correct += self.number_of_correct(maximum_spanning_tree,
                                                  golden_standard)
                total += (len(test_set[sentence]) - 1)
                accuracy = float(float(correct) / total)

            else:
                result_dict[sentence] = maximum_spanning_tree

        if not real_test:
            print("The final accuracy is ", accuracy, " achieved with ",
                  self.global_iterations, " iterations")
            return accuracy
        else:
            return result_dict
Example #2
0
 def run(self):
     self.init_weights()
     begin = time.time()
     problem = 0
     for iteration in range(0, self.global_iterations):
         for sentence in self.feature_maker.train_data:
             graph_with_all_weights = self.feature_maker.create_weighted_graph_for_sentence(
                 sentence, self.weights, self.feature_maker.train_data)
             maximum_spanning_tree = mst.mst(0, graph_with_all_weights)
             """if 0 not in maximum_spanning_tree:
                 maximum_spanning_tree = self.connect_tree(maximum_spanning_tree)"""
             golden_standard = self.feature_maker.golden_standard[sentence]
             if not self.compare_trees(maximum_spanning_tree,
                                       golden_standard):
                 self.add(self.feature_maker.
                          sentence_feature_dictionary[sentence])
                 self.subtract(
                     self.feature_maker.create_feature_vector_from_tree(
                         sentence, maximum_spanning_tree))
     print("perceptron run took :",
           time.time() - begin, " with ", self.global_iterations,
           " iterations")
     print("problem with ", problem)
     return self.weights