checkers.profanity

 1from src.checkers import Checker
 2import src.utils.db
 3
 4
 5class ProfanityChecker(Checker):
 6    def __init__(self):
 7        self.words = {}
 8        self.conn = src.utils.db.DbConnector()
 9        self.conn.connect()
10        self.conn.create_tables()
11
12    def add_words(self, server, word):
13        """
14        Add all the words inserted into the hate words list
15        :return:
16        """
17        try:
18            cursor = self.conn.connector.cursor()
19            sql_query = "SELECT word FROM discorddb.pwords WHERE server_name = %s AND word = %s"
20            cursor.execute(sql_query, (server, word))
21            result = cursor.fetchone()
22            if not result:
23                sql_query = "INSERT INTO discorddb.pwords (server_name, word) VALUES (%s,%s)"
24                val = (server, word)
25                cursor.execute(sql_query, val)
26                self.conn.connector.commit()
27            else:
28                print("The word is already reported.")
29
30            if server not in self.words:
31                self.words[server] = [word]
32            else:
33                if word not in self.words[server]:
34                    self.words[server].append(word)
35            print(self.words)
36        except Exception as error:
37            print("Failed to insert record from MySQL table: {}".format(error))
38        finally:
39            cursor.close()
40            return True
41
42    def check_word(self, server, word: str):
43        """
44        Checks and returns True if the word is in the hate words list. Else returns False
45        :param word:
46        :return:
47        """
48        if server in self.words:
49            return bool(word in self.words[server])
50        return False
51
52    def check_message(self, server, message: str):
53        """
54        Function to check if a message has profane words
55        :param message: Message String
56        :return: Boolean
57        """
58        for word in message.split():
59            if self.check_word(server, word):
60                return True
61        return False
class ProfanityChecker(src.checkers.Checker):
 6class ProfanityChecker(Checker):
 7    def __init__(self):
 8        self.words = {}
 9        self.conn = src.utils.db.DbConnector()
10        self.conn.connect()
11        self.conn.create_tables()
12
13    def add_words(self, server, word):
14        """
15        Add all the words inserted into the hate words list
16        :return:
17        """
18        try:
19            cursor = self.conn.connector.cursor()
20            sql_query = "SELECT word FROM discorddb.pwords WHERE server_name = %s AND word = %s"
21            cursor.execute(sql_query, (server, word))
22            result = cursor.fetchone()
23            if not result:
24                sql_query = "INSERT INTO discorddb.pwords (server_name, word) VALUES (%s,%s)"
25                val = (server, word)
26                cursor.execute(sql_query, val)
27                self.conn.connector.commit()
28            else:
29                print("The word is already reported.")
30
31            if server not in self.words:
32                self.words[server] = [word]
33            else:
34                if word not in self.words[server]:
35                    self.words[server].append(word)
36            print(self.words)
37        except Exception as error:
38            print("Failed to insert record from MySQL table: {}".format(error))
39        finally:
40            cursor.close()
41            return True
42
43    def check_word(self, server, word: str):
44        """
45        Checks and returns True if the word is in the hate words list. Else returns False
46        :param word:
47        :return:
48        """
49        if server in self.words:
50            return bool(word in self.words[server])
51        return False
52
53    def check_message(self, server, message: str):
54        """
55        Function to check if a message has profane words
56        :param message: Message String
57        :return: Boolean
58        """
59        for word in message.split():
60            if self.check_word(server, word):
61                return True
62        return False

An Abstract Class for all Checker Classes. It is inherited by all the Checker objects

ProfanityChecker()
 7    def __init__(self):
 8        self.words = {}
 9        self.conn = src.utils.db.DbConnector()
10        self.conn.connect()
11        self.conn.create_tables()
def add_words(self, server, word):
13    def add_words(self, server, word):
14        """
15        Add all the words inserted into the hate words list
16        :return:
17        """
18        try:
19            cursor = self.conn.connector.cursor()
20            sql_query = "SELECT word FROM discorddb.pwords WHERE server_name = %s AND word = %s"
21            cursor.execute(sql_query, (server, word))
22            result = cursor.fetchone()
23            if not result:
24                sql_query = "INSERT INTO discorddb.pwords (server_name, word) VALUES (%s,%s)"
25                val = (server, word)
26                cursor.execute(sql_query, val)
27                self.conn.connector.commit()
28            else:
29                print("The word is already reported.")
30
31            if server not in self.words:
32                self.words[server] = [word]
33            else:
34                if word not in self.words[server]:
35                    self.words[server].append(word)
36            print(self.words)
37        except Exception as error:
38            print("Failed to insert record from MySQL table: {}".format(error))
39        finally:
40            cursor.close()
41            return True

Add all the words inserted into the hate words list

Returns
def check_word(self, server, word: str):
43    def check_word(self, server, word: str):
44        """
45        Checks and returns True if the word is in the hate words list. Else returns False
46        :param word:
47        :return:
48        """
49        if server in self.words:
50            return bool(word in self.words[server])
51        return False

Checks and returns True if the word is in the hate words list. Else returns False

Parameters
  • word:
Returns
def check_message(self, server, message: str):
53    def check_message(self, server, message: str):
54        """
55        Function to check if a message has profane words
56        :param message: Message String
57        :return: Boolean
58        """
59        for word in message.split():
60            if self.check_word(server, word):
61                return True
62        return False

Function to check if a message has profane words

Parameters
  • message: Message String
Returns

Boolean