Skip to content

Rule Based

RuleBasedFilter

Source code in engines/contentFilterEngine/other_approaches/rule_based.py
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class RuleBasedFilter:
    def __init__(self, rules=None):
        """
        Initializes the RuleBasedFilter with a set of rules.

        Parameters:
        - rules (list of dict): A list where each rule is a dictionary containing
                                 'keyword' and 'action' keys.
        """
        if rules is None:
            self.rules = []
        else:
            self.rules = rules

    def add_rule(self, keyword, action):
        """
        Adds a new rule to the filter.

        Parameters:
        - keyword (str): The keyword to look for in the content.
        - action (str): The action to take ('block', 'flag', etc.).
        """
        rule = {'keyword': keyword.lower(), 'action': action.lower()}
        self.rules.append(rule)

    def filter_content(self, content):
        """
        Filters the content based on the predefined rules.

        Parameters:
        - content (str): The content to be filtered.

        Returns:
        - dict: A dictionary with 'status' and 'actions' applied.
        """
        actions_applied = []
        content_lower = content.lower()

        for rule in self.rules:
            if rule['keyword'] in content_lower:
                actions_applied.append(rule['action'])

        if 'block' in actions_applied:
            return {'status': 'blocked', 'actions': actions_applied}
        elif 'flag' in actions_applied:
            return {'status': 'flagged', 'actions': actions_applied}
        else:
            return {'status': 'allowed', 'actions': actions_applied}

__init__(rules=None)

Initializes the RuleBasedFilter with a set of rules.

  • rules (list of dict): A list where each rule is a dictionary containing 'keyword' and 'action' keys.
Source code in engines/contentFilterEngine/other_approaches/rule_based.py
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def __init__(self, rules=None):
    """
    Initializes the RuleBasedFilter with a set of rules.

    Parameters:
    - rules (list of dict): A list where each rule is a dictionary containing
                             'keyword' and 'action' keys.
    """
    if rules is None:
        self.rules = []
    else:
        self.rules = rules

add_rule(keyword, action)

Adds a new rule to the filter.

Parameters: - keyword (str): The keyword to look for in the content. - action (str): The action to take ('block', 'flag', etc.).

Source code in engines/contentFilterEngine/other_approaches/rule_based.py
16
17
18
19
20
21
22
23
24
25
def add_rule(self, keyword, action):
    """
    Adds a new rule to the filter.

    Parameters:
    - keyword (str): The keyword to look for in the content.
    - action (str): The action to take ('block', 'flag', etc.).
    """
    rule = {'keyword': keyword.lower(), 'action': action.lower()}
    self.rules.append(rule)

filter_content(content)

Filters the content based on the predefined rules.

Parameters: - content (str): The content to be filtered.

Returns: - dict: A dictionary with 'status' and 'actions' applied.

Source code in engines/contentFilterEngine/other_approaches/rule_based.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def filter_content(self, content):
    """
    Filters the content based on the predefined rules.

    Parameters:
    - content (str): The content to be filtered.

    Returns:
    - dict: A dictionary with 'status' and 'actions' applied.
    """
    actions_applied = []
    content_lower = content.lower()

    for rule in self.rules:
        if rule['keyword'] in content_lower:
            actions_applied.append(rule['action'])

    if 'block' in actions_applied:
        return {'status': 'blocked', 'actions': actions_applied}
    elif 'flag' in actions_applied:
        return {'status': 'flagged', 'actions': actions_applied}
    else:
        return {'status': 'allowed', 'actions': actions_applied}