Пример #1
0
    def decode(self, number_of_synsets, words_file_path):
        with open(words_file_path, encoding='utf8') as words_file:
            words = [line[:-1] for line in words_file.readlines()]
        word_to_decoded_graph = dict()
        for word in words:
            print("Decoding: " + word)
            decoded_graph = SGE.build_word2vec_graph(word, number_of_synsets,
                                                     self.wnUtilities)

            word_to_decoded_graph[word] = decoded_graph
        return word_to_decoded_graph
Пример #2
0
def main(number_of_synsets, vector_file_path, words_path):
    print("Decoding illustration for: {}, {}, {}".format(number_of_synsets,
                                                         vector_file_path,
                                                         words_path))
    w2vUtilities = Word2VecUtilities()
    w2vUtilities.load_vectors_from(vector_file_path)
    wnUtilities = WordnetUtilities(w2vUtilities)

    decoding = Decoding(wnUtilities)
    word_to_decoded_graph_dict = decoding.decode(number_of_synsets, words_path)

    for word, decoded_graph in word_to_decoded_graph_dict.items():
        baseline_graph = SGE.build_baseline_graph(word)
        print("baseline for {}:\n{}".format(word, baseline_graph.display()))

        gold_graph = SGE.build_gold_graph(word, wnUtilities)
        print("gold_graph for {}:\n{}".format(word, gold_graph.display()))

        print("decoded for {}:\n{}".format(word, decoded_graph.display()))

    print(word_to_decoded_graph_dict)
Пример #3
0
    def evaluate(self, number_of_synsets, word_to_decoded_graph_dict):
        word_to_comparison_dict = dict()
        category_results = dict()

        for word, decoded_graph in word_to_decoded_graph_dict.items():
            exps_dir_path = "../exps/{}-{}/".format(word, number_of_synsets)
            results_dir_path = "../results/{}-{}/".format(word,
                                                          number_of_synsets)
            self._create_dir_if_doesnt_exist(exps_dir_path)
            self._create_dir_if_doesnt_exist(results_dir_path)

            baseline_graph = SGE.build_baseline_graph(word)
            baseline_graph.dump_to_file(exps_dir_path + "baseline.txt")

            gold_graph = SGE.build_gold_graph(word, self.wnUtilities)
            baseline_graph.dump_to_file(exps_dir_path + "gold.txt")

            decoded_graph.dump_to_file(exps_dir_path + "decoded.txt")

            comparison = SynsetGraphComarison(baseline_graph, decoded_graph,
                                              gold_graph)
            comparison.compare_using_all_methods()
            comparison.dump_to_file(results_dir_path + "comparison.txt")
            word_to_comparison_dict[word] = comparison

            for category, result in comparison.results.items():
                if category not in category_results:
                    category_results[category] = 0
                category_results[category] += result

        with open("../results/final-{}.txt".format(number_of_synsets),
                  'w', encoding='utf8') as category_results_file:
            for category, result in category_results.items():
                category_results[category] = (result /
                                              len(word_to_decoded_graph_dict))
                category_results_file.write("{}: {}\n".
                                            format(category,
                                                   category_results[category]))

        return word_to_comparison_dict, category_results