def __init__(self):
     self.__af = ArgFramework()
     self.__twitter = None
     self.__rte = None
     self.__lstm_results_csv = os.getcwd() + "\\logs\\LSTM_PREDICTION_RESULTS.csv"
     self.__sentences = []
     self.__result = None
 def __init__(self, *args, **kwargs):
     super(TestArgFramework, self).__init__(*args, **kwargs)
     self.af = ArgFramework()
     self.af.add_argument("test1")
     self.af.add_argument("test2")
     self.af.add_argument("test3")
     self.af.add_argument("test4")
     self.af.add_relation("test1", "test2", SUPPORT)
     self.af.add_relation("test2", "test3", ATTACK)
     self.af.add_relation("test3", "test4", ATTACK)
     self.af.pre_setup()
Exemple #3
0
 def test_save_and_load_graph(self):
     self.graph.add_argument("test1")
     self.graph.add_argument("test2")
     self.graph.add_argument("test3")
     self.graph.add_argument("test4")
     self.graph.add_relation("test1", "test2", SUPPORT)
     self.graph.add_relation("test2", "test3", ATTACK)
     self.graph.add_relation("test3", "test4", ATTACK)
     self.graph.save(custom_path=os.getcwd() + '\\graph_data\\01_Test.gexf')
     self.assertEqual(
         os.path.isfile(os.getcwd() + '\\graph_data\\01_Test.gexf'), True)
     self.graph = None
     self.graph = ArgFramework()
     self.graph.load(custom_path=os.getcwd() + '\\graph_data\\01_Test.gexf')
     self.assertEqual(3, len(self.graph.get_af().edges()))
     self.assertEqual(4, len(self.graph.get_af().nodes()))
Exemple #4
0
class GraphConstructionTest(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super(GraphConstructionTest, self).__init__(*args, **kwargs)
        self.graph = ArgFramework()

    def test_add_node(self):
        self.graph.add_argument("test1")
        self.graph.add_argument("test2")
        self.assertEqual(2, len(self.graph.get_af().nodes()))

    def test_add_edge(self):
        self.graph.add_argument("test1")
        self.graph.add_argument("test2")
        self.graph.add_argument("test3")
        self.graph.add_argument("test4")
        self.graph.add_relation("test1", "test2", ATTACK)
        self.graph.add_relation("test2", "test3", ATTACK)
        self.graph.add_relation("test3", "test4", ATTACK)
        self.assertEqual(3, len(self.graph.get_af().edges()))

    def test_different_edges(self):
        self.graph.add_argument("test1")
        self.graph.add_argument("test2")
        self.graph.add_argument("test3")
        self.graph.add_argument("test4")
        self.graph.add_relation("test1", "test2", SUPPORT)
        self.graph.add_relation("test2", "test3", ATTACK)
        self.graph.add_relation("test3", "test4", ATTACK)
        self.assertEqual(self.graph.get_af()[1][2]['color'], 'green')
        self.assertEqual(self.graph.get_af()[2][3]['color'], 'red')
        self.assertEqual(self.graph.get_af()[3][4]['color'], 'red')

    def test_save_and_load_graph(self):
        self.graph.add_argument("test1")
        self.graph.add_argument("test2")
        self.graph.add_argument("test3")
        self.graph.add_argument("test4")
        self.graph.add_relation("test1", "test2", SUPPORT)
        self.graph.add_relation("test2", "test3", ATTACK)
        self.graph.add_relation("test3", "test4", ATTACK)
        self.graph.save(custom_path=os.getcwd() + '\\graph_data\\01_Test.gexf')
        self.assertEqual(
            os.path.isfile(os.getcwd() + '\\graph_data\\01_Test.gexf'), True)
        self.graph = None
        self.graph = ArgFramework()
        self.graph.load(custom_path=os.getcwd() + '\\graph_data\\01_Test.gexf')
        self.assertEqual(3, len(self.graph.get_af().edges()))
        self.assertEqual(4, len(self.graph.get_af().nodes()))

    def test_save_png_format(self):
        self.graph.add_argument("test1")
        self.graph.add_argument("test2")
        self.graph.add_argument("test3")
        self.graph.add_argument("test4")
        self.graph.add_argument("test5")
        self.graph.add_relation("test1", "test2", SUPPORT)
        self.graph.add_relation("test2", "test3", ATTACK)
        self.graph.add_relation("test3", "test4", ATTACK)
        self.graph.add_relation("test5", "test4", ATTACK)
        self.graph.add_relation("test5", "test1", SUPPORT)
        self.graph.draw()
        self.assertEqual(os.path.isfile(IMAGE_PATH), True)
Exemple #5
0
 def __init__(self, *args, **kwargs):
     super(GraphConstructionTest, self).__init__(*args, **kwargs)
     self.graph = ArgFramework()
class TestArgFramework(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super(TestArgFramework, self).__init__(*args, **kwargs)
        self.af = ArgFramework()
        self.af.add_argument("test1")
        self.af.add_argument("test2")
        self.af.add_argument("test3")
        self.af.add_argument("test4")
        self.af.add_relation("test1", "test2", SUPPORT)
        self.af.add_relation("test2", "test3", ATTACK)
        self.af.add_relation("test3", "test4", ATTACK)
        self.af.pre_setup()

    def test_conflict_free(self):
        actual_cf = self.af.conflict_free_arguments()
        expected_cf = [tuple([1]), tuple([2]), tuple([3]), tuple([4]), tuple([1, 2]), tuple([1, 3]), tuple([1, 4]),
                       tuple([2, 4]), tuple([1, 2, 4])]
        self.assertEqual(expected_cf, actual_cf)

    def test_admissible(self):
        self.af.conflict_free_arguments()
        actual_adm = self.af.admissible_arguments()
        expected_adm = [tuple([1]), tuple([2]), tuple([1, 2]), tuple([2, 4]), tuple([1, 2, 4])]
        self.assertEqual(expected_adm, actual_adm)

    def test_complete_extension(self):
        self.af.conflict_free_arguments()
        self.af.admissible_arguments()
        actual_ce = self.af.complete_extension()
        expected_ce = [tuple([1, 2, 4])]
        self.assertEqual(expected_ce, actual_ce)

    def test_grounded_extension(self):
        self.af.conflict_free_arguments()
        self.af.admissible_arguments()
        self.af.complete_extension()
        actual_ge = self.af.grounded_extension()
        expected_ge = set([1, 2, 4])
        self.assertEqual(actual_ge, expected_ge)
class Main:
    def __init__(self):
        self.__af = ArgFramework()
        self.__twitter = None
        self.__rte = None
        self.__lstm_results_csv = os.getcwd() + "\\logs\\LSTM_PREDICTION_RESULTS.csv"
        self.__sentences = []
        self.__result = None

    def setup_twitter_data(self, tweet_count, q):
        print(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') + ": Setting up Twitter...")
        self.__twitter = TwitterMining()
        print(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') + ": Mining data...")
        self.__twitter.mine_tweets(query=q[0], since=str(datetime.datetime.now()).split(" ")[0], count=tweet_count)

    def train_lstm_model(self, model_name):
        print(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') + ": Textual Entailment "
                                                                                           "Training setup...")
        self.__rte = TextualEntailment(is_training=True)
        self.__rte.train_valid_test_log(save_as=model_name)

    def load_lstm_model(self, model_name):
        print(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') + ": Textual Entailment "
                                                                                           "Setup...")
        self.__rte = TextualEntailment(is_training=False)
        model_path = os.getcwd() + '\\models\\' + model_name
        print(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') + ": Loading model...")
        self.__rte.load_model(model_path)

    def load_twitter_data(self):
        print(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') + ": Reading sentences...")
        with open(self.__twitter.tweets_csv_path, mode='r', encoding="UTF-8") as file:
            for line in file:
                sent = line.split("|")[-1].rstrip()
                self.__sentences.append(sent)

    def run_model_on_twitter(self):
        relation_count = 0
        print(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') + ": Doing predictions...")
        with open(self.__lstm_results_csv, mode="w", encoding="UTF-8", newline='') as file:
            file = csv.writer(file, delimiter='|', quotechar='"', quoting=csv.QUOTE_MINIMAL)
            for i in range(len(self.__sentences) - 1, 0, -1):
                for j in range(i - 1, -1, -1):
                    res = self.__rte.run_predict(self.__sentences[j], self.__sentences[i])
                    if res == "E" or res == "C":
                        relation_count = relation_count + 1
                        self.__af.add_argument(self.__sentences[j])
                        self.__af.add_argument(self.__sentences[i])
                        self.__af.add_relation(self.__sentences[j], self.__sentences[i],
                                               ATTACK if res in ["C"] else SUPPORT)
                        file.writerow([j, res, i, self.__sentences[j], self.__sentences[i]])
        self.__af.pre_setup()
        print(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') + ": Created " +
              str(len(self.__af.get_af().nodes())) + " Argument Nodes...")
        print(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') + ": Calculated " +
              str(relation_count) + " Argument Relations...")

    def print_subsets(self):
        print(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') + ": Analysing Conflict Free "
                                                                                           "Arguments...")
        self.__af.conflict_free_arguments()
        print(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') + ": Analysing Admissible "
                                                                                           "Arguments...")
        self.__af.admissible_arguments()
        print(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') + ": Computing Complete "
                                                                                           "Extensions...")
        self.__af.complete_extension()
        print(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') + ": Finding Grounded "
                                                                                           "Extension...")
        self.__result = self.__af.grounded_extension()
        print(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') + ": Grounded Extension is: " +
              str(self.__result))

    def save_argument_framework(self):
        print(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') + ": Saving...")
        self.__af.save()

    def draw_argument_framework(self):
        print(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') + ": Drawing...")
        self.__af.draw()

    def result_tweets(self):
        print(datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') + ": Printing Grounded "
                                                                                           "Extension Tweets:")
        with open(self.__twitter.tweets_csv_path, mode="r", encoding="UTF-8") as file:
            for line in file:
                data = line.split("|")
                if int(data[0]) in self.__result:
                    print(data[0] + " | " + data[1] + " | " + data[5], end='\n')