Skip to content

EnrichmentWithOLS

Enrichment class for enriching OLS terms with textual descriptions

EnrichmentWithOLS

Bases: Enrichments

Enrichment class using OLS terms

Source code in aiagents4pharma/talk2knowledgegraphs/utils/enrichments/ols_terms.py
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
71
72
73
74
75
76
class EnrichmentWithOLS(Enrichments):
    """
    Enrichment class using OLS terms
    """
    def enrich_documents(self, texts: List[str]) -> List[str]:
        """
        Enrich a list of input OLS terms

        Args:
            texts: The list of OLS terms to be enriched.

        Returns:
            The list of enriched descriptions
        """

        ols_ids = texts

        logger.log(logging.INFO,
                   "Load Hydra configuration for OLS enrichments.")
        with hydra.initialize(version_base=None, config_path="../../configs"):
            cfg = hydra.compose(config_name='config',
                                overrides=['utils/enrichments/ols_terms=default'])
            cfg = cfg.utils.enrichments.ols_terms

        descriptions = []
        for ols_id in ols_ids:
            params = {
                'short_form': ols_id
            }
            r = requests.get(cfg.base_url,
                             headers={ "Accept" : "application/json"},
                             params=params,
                             timeout=cfg.timeout)
            response_body = json.loads(r.text)
            # if the response body is empty
            if '_embedded' not in response_body:
                descriptions.append(None)
                continue
            # Add the description to the list
            description = response_body['_embedded']['terms'][0]['description']
            # Add synonyms to the description
            description += response_body['_embedded']['terms'][0]['synonyms']
            # Add the label to the description
            # Label is not provided as list, so we need to convert it to a list
            description += [response_body['_embedded']['terms'][0]['label']]
            descriptions.append('\n'.join(description))
        return descriptions

    def enrich_documents_with_rag(self, texts, docs):
        """
        Enrich a list of input OLS terms

        Args:
            texts: The list of OLS to be enriched.

        Returns:
            The list of enriched descriptions
        """
        return self.enrich_documents(texts)

enrich_documents(texts)

Enrich a list of input OLS terms

Parameters:

Name Type Description Default
texts List[str]

The list of OLS terms to be enriched.

required

Returns:

Type Description
List[str]

The list of enriched descriptions

Source code in aiagents4pharma/talk2knowledgegraphs/utils/enrichments/ols_terms.py
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
def enrich_documents(self, texts: List[str]) -> List[str]:
    """
    Enrich a list of input OLS terms

    Args:
        texts: The list of OLS terms to be enriched.

    Returns:
        The list of enriched descriptions
    """

    ols_ids = texts

    logger.log(logging.INFO,
               "Load Hydra configuration for OLS enrichments.")
    with hydra.initialize(version_base=None, config_path="../../configs"):
        cfg = hydra.compose(config_name='config',
                            overrides=['utils/enrichments/ols_terms=default'])
        cfg = cfg.utils.enrichments.ols_terms

    descriptions = []
    for ols_id in ols_ids:
        params = {
            'short_form': ols_id
        }
        r = requests.get(cfg.base_url,
                         headers={ "Accept" : "application/json"},
                         params=params,
                         timeout=cfg.timeout)
        response_body = json.loads(r.text)
        # if the response body is empty
        if '_embedded' not in response_body:
            descriptions.append(None)
            continue
        # Add the description to the list
        description = response_body['_embedded']['terms'][0]['description']
        # Add synonyms to the description
        description += response_body['_embedded']['terms'][0]['synonyms']
        # Add the label to the description
        # Label is not provided as list, so we need to convert it to a list
        description += [response_body['_embedded']['terms'][0]['label']]
        descriptions.append('\n'.join(description))
    return descriptions

enrich_documents_with_rag(texts, docs)

Enrich a list of input OLS terms

Parameters:

Name Type Description Default
texts

The list of OLS to be enriched.

required

Returns:

Type Description

The list of enriched descriptions

Source code in aiagents4pharma/talk2knowledgegraphs/utils/enrichments/ols_terms.py
66
67
68
69
70
71
72
73
74
75
76
def enrich_documents_with_rag(self, texts, docs):
    """
    Enrich a list of input OLS terms

    Args:
        texts: The list of OLS to be enriched.

    Returns:
        The list of enriched descriptions
    """
    return self.enrich_documents(texts)