Skip to content

Ontology Based

OntologyBasedFilter

Source code in engines/contentFilterEngine/other_approaches/ontology_based.py
 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class OntologyBasedFilter:
    def __init__(self, ontology_path):
        """
        Initializes the OntologyBasedFilter with a specific ontology.

        Parameters:
        - ontology_path (str): The file path to the ontology (.owl) file.
        """
        try:
            self.ontology = get_ontology(ontology_path).load()
        except Exception as e:
            raise ValueError(f"Failed to load ontology from {ontology_path}: {e}")

    def get_concepts(self, content):
        """
        Extracts concepts from the content based on the ontology.

        Parameters:
        - content (str): The content to extract concepts from.

        Returns:
        - set: A set of concepts identified in the content.
        """
        concepts_found = set()
        content_lower = content.lower()

        for cls in self.ontology.classes():
            if cls.name.lower() in content_lower:
                concepts_found.add(cls.name)

        return concepts_found

    def filter_content(self, content):
        """
        Filters the content based on ontology-defined relationships.

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

        Returns:
        - dict: A dictionary with 'status' and 'related_concepts'.
        """
        concepts = self.get_concepts(content)
        related_concepts = self.find_related_concepts(concepts)

        if related_concepts:
            return {'status': 'filtered', 'related_concepts': related_concepts}
        else:
            return {'status': 'allowed', 'related_concepts': related_concepts}

    def find_related_concepts(self, concepts):
        """
        Finds related concepts within the ontology.

        Parameters:
        - concepts (set): A set of concepts to find relationships for.

        Returns:
        - dict: A dictionary mapping each concept to its related concepts.
        """
        related = {}
        for concept in concepts:
            try:
                cls = self.ontology[concept]
                related[concept] = [str(rel) for rel in cls.is_a]
            except KeyError:
                related[concept] = []
        return related

__init__(ontology_path)

Initializes the OntologyBasedFilter with a specific ontology.

Parameters: - ontology_path (str): The file path to the ontology (.owl) file.

Source code in engines/contentFilterEngine/other_approaches/ontology_based.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def __init__(self, ontology_path):
    """
    Initializes the OntologyBasedFilter with a specific ontology.

    Parameters:
    - ontology_path (str): The file path to the ontology (.owl) file.
    """
    try:
        self.ontology = get_ontology(ontology_path).load()
    except Exception as e:
        raise ValueError(f"Failed to load ontology from {ontology_path}: {e}")

filter_content(content)

Filters the content based on ontology-defined relationships.

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

Returns: - dict: A dictionary with 'status' and 'related_concepts'.

Source code in engines/contentFilterEngine/other_approaches/ontology_based.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def filter_content(self, content):
    """
    Filters the content based on ontology-defined relationships.

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

    Returns:
    - dict: A dictionary with 'status' and 'related_concepts'.
    """
    concepts = self.get_concepts(content)
    related_concepts = self.find_related_concepts(concepts)

    if related_concepts:
        return {'status': 'filtered', 'related_concepts': related_concepts}
    else:
        return {'status': 'allowed', 'related_concepts': related_concepts}

Finds related concepts within the ontology.

Parameters: - concepts (set): A set of concepts to find relationships for.

Returns: - dict: A dictionary mapping each concept to its related concepts.

Source code in engines/contentFilterEngine/other_approaches/ontology_based.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def find_related_concepts(self, concepts):
    """
    Finds related concepts within the ontology.

    Parameters:
    - concepts (set): A set of concepts to find relationships for.

    Returns:
    - dict: A dictionary mapping each concept to its related concepts.
    """
    related = {}
    for concept in concepts:
        try:
            cls = self.ontology[concept]
            related[concept] = [str(rel) for rel in cls.is_a]
        except KeyError:
            related[concept] = []
    return related

get_concepts(content)

Extracts concepts from the content based on the ontology.

Parameters: - content (str): The content to extract concepts from.

Returns: - set: A set of concepts identified in the content.

Source code in engines/contentFilterEngine/other_approaches/ontology_based.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def get_concepts(self, content):
    """
    Extracts concepts from the content based on the ontology.

    Parameters:
    - content (str): The content to extract concepts from.

    Returns:
    - set: A set of concepts identified in the content.
    """
    concepts_found = set()
    content_lower = content.lower()

    for cls in self.ontology.classes():
        if cls.name.lower() in content_lower:
            concepts_found.add(cls.name)

    return concepts_found