# Path: indra/ontology/standardize.py
# def standardize_agent_name(agent, standardize_refs=True, ontology=None,
#                            ns_order=None):
#     """Standardize the name of an Agent based on grounding information.
# 
#     The priority of which namespace is used as the bases for the
#     standard name depends on
# 
#     Parameters
#     ----------
#     agent : indra.statements.Agent
#         An INDRA Agent whose name attribute should be standardized based
#         on grounding information.
#     standardize_refs : Optional[bool]
#         If True, this function assumes that the Agent's db_refs need to
#         be standardized, e.g., HGNC mapped to UP.
#         Default: True
#     ontology : Optional[indra.ontology.IndraOntology]
#         An IndraOntology object, if not provided, the default BioOntology
#         is used.
#     ns_order : Optional[list]
#         A list of namespaces which are in order of priority with higher
#         priority namespaces appearing earlier in the list.
# 
#     Returns
#     -------
#     bool
#         True if a new name was set, False otherwise.
#     """
#     # If the Agent is None, we return immediately
#     if agent is None:
#         return False
#     # If we want to standardize the Agent's db_refs, we call this now
#     if standardize_refs:
#         agent.db_refs = standardize_db_refs(agent.db_refs, ontology=ontology)
#     # We next try to get a standard name based on the Agent's grounding
#     standard_name = get_standard_name(agent.db_refs, ontology=ontology,
#                                       ns_order=ns_order)
#     # If we got a proper standard name, we apply it
#     if standard_name:
#         agent.name = standard_name
#         return True
#     return False
# 
# Path: indra/preassembler/grounding_mapper/gilda.py
# def ground_agent(agent, txt, context=None, mode='web'):
#     """Set the grounding of a given agent, by re-grounding with Gilda.
# 
#     This function changes the agent in place without returning a value.
# 
#     Parameters
#     ----------
#     agent : indra.statements.Agent
#         The Agent whose db_refs shuld be changed.
#     txt : str
#         The text by which the Agent should be grounded.
#     context : Optional[str]
#         Any additional text context to help disambiguate the sense
#         associated with txt.
#     mode : Optional[str]
#         If 'web', the web service given in the GILDA_URL config setting or
#         environmental variable is used. Otherwise, the gilda package is
#         attempted to be imported and used. Default: web
#     """
#     gr, results = get_grounding(txt, context, mode)
#     if gr:
#         db_refs = {'TEXT': txt}
#         db_refs.update(gr)
#         agent.db_refs = db_refs
#         standardize_agent_name(agent, standardize_refs=True)
#     return results


import logging
from indra.ontology.standardize \
    import standardize_agent_name
from .gilda import ground_agent
    from adeft import available_shortforms as available_adeft_models
    from adeft.disambiguate import load_disambiguator
            from indra_db.util.content_scripts import TextContentSessionHandler
                from indra.literature.adeft_tools import universal_extract_text
            from indra.literature import pubmed_client
            otherwise.  Reasons for a False response can be the lack of
            evidence as well as failure to obtain text for grounding
            disambiguation.
        """
        success = False
        # If the Statement doesn't have evidence for some reason, then there is
        # no text to disambiguate by
        # NOTE: we might want to try disambiguating by other agents in the
        # Statement
        if not stmt.evidence:
            return False
        # Initialize annotations if needed so Adeft predicted
        # probabilities can be added to Agent annotations
        annots = stmt.evidence[0].annotations
        if 'agents' in annots:
            if 'adeft' not in annots['agents']:
                annots['agents']['adeft'] = \
                    {'adeft': [None for _ in stmt.agent_list()]}
        else:
            annots['agents'] = {'adeft': [None for _ in stmt.agent_list()]}
        grounding_text = self._get_text_for_grounding(stmt, agent_txt)

        def apply_grounding(agent, agent_txt, ns_and_id):
            db_ns, db_id = ns_and_id.split(':', maxsplit=1)
            if db_ns == 'CHEBI' and not db_id.startswith('CHEBI:'):
                db_id = 'CHEBI:%s' % db_id
            agent.db_refs = {'TEXT': agent_txt, db_ns: db_id}
            agent.name = standard_name
            logger.debug('Disambiguated %s to: %s, %s:%s' %
                         (agent_txt, standard_name, db_ns, db_id))

# Path: indra/ontology/standardize.py
# def standardize_agent_name(agent, standardize_refs=True, ontology=None,
#                            ns_order=None):
#     """Standardize the name of an Agent based on grounding information.
# 
#     The priority of which namespace is used as the bases for the
#     standard name depends on
# 
#     Parameters
#     ----------
#     agent : indra.statements.Agent
#         An INDRA Agent whose name attribute should be standardized based
#         on grounding information.
#     standardize_refs : Optional[bool]
#         If True, this function assumes that the Agent's db_refs need to
#         be standardized, e.g., HGNC mapped to UP.
#         Default: True
#     ontology : Optional[indra.ontology.IndraOntology]
#         An IndraOntology object, if not provided, the default BioOntology
#         is used.
#     ns_order : Optional[list]
#         A list of namespaces which are in order of priority with higher
#         priority namespaces appearing earlier in the list.
# 
#     Returns
#     -------
#     bool
#         True if a new name was set, False otherwise.
#     """
#     # If the Agent is None, we return immediately
#     if agent is None:
#         return False
#     # If we want to standardize the Agent's db_refs, we call this now
#     if standardize_refs:
#         agent.db_refs = standardize_db_refs(agent.db_refs, ontology=ontology)
#     # We next try to get a standard name based on the Agent's grounding
#     standard_name = get_standard_name(agent.db_refs, ontology=ontology,
#                                       ns_order=ns_order)
#     # If we got a proper standard name, we apply it
#     if standard_name:
#         agent.name = standard_name
#         return True
#     return False
# 
# Path: indra/preassembler/grounding_mapper/gilda.py
# def ground_agent(agent, txt, context=None, mode='web'):
#     """Set the grounding of a given agent, by re-grounding with Gilda.
# 
#     This function changes the agent in place without returning a value.
# 
#     Parameters
#     ----------
#     agent : indra.statements.Agent
#         The Agent whose db_refs shuld be changed.
#     txt : str
#         The text by which the Agent should be grounded.
#     context : Optional[str]
#         Any additional text context to help disambiguate the sense
#         associated with txt.
#     mode : Optional[str]
#         If 'web', the web service given in the GILDA_URL config setting or
#         environmental variable is used. Otherwise, the gilda package is
#         attempted to be imported and used. Default: web
#     """
#     gr, results = get_grounding(txt, context, mode)
#     if gr:
#         db_refs = {'TEXT': txt}
#         db_refs.update(gr)
#         agent.db_refs = db_refs
#         standardize_agent_name(agent, standardize_refs=True)
#     return results


# Path: indra/preassembler/grounding_mapper/disambiguate.py
import logging
from indra.ontology.standardize \
    import standardize_agent_name
from .gilda import ground_agent
    from adeft import available_shortforms as available_adeft_models
    from adeft.disambiguate import load_disambiguator
            from indra_db.util.content_scripts import TextContentSessionHandler
                from indra.literature.adeft_tools import universal_extract_text
            from indra.literature import pubmed_client

            otherwise.  Reasons for a False response can be the lack of
            evidence as well as failure to obtain text for grounding
            disambiguation.
        """
        success = False
        # If the Statement doesn't have evidence for some reason, then there is
        # no text to disambiguate by
        # NOTE: we might want to try disambiguating by other agents in the
        # Statement
        if not stmt.evidence:
            return False
        # Initialize annotations if needed so Adeft predicted
        # probabilities can be added to Agent annotations
        annots = stmt.evidence[0].annotations
        if 'agents' in annots:
            if 'adeft' not in annots['agents']:
                annots['agents']['adeft'] = \
                    {'adeft': [None for _ in stmt.agent_list()]}
        else:
            annots['agents'] = {'adeft': [None for _ in stmt.agent_list()]}
        grounding_text = self._get_text_for_grounding(stmt, agent_txt)

        def apply_grounding(agent, agent_txt, ns_and_id):
            db_ns, db_id = ns_and_id.split(':', maxsplit=1)
            if db_ns == 'CHEBI' and not db_id.startswith('CHEBI:'):
                db_id = 'CHEBI:%s' % db_id
            agent.db_refs = {'TEXT': agent_txt, db_ns: db_id}
            agent.name = standard_name
            logger.debug('Disambiguated %s to: %s, %s:%s' %
                         (agent_txt, standard_name, db_ns, db_id))

            standardize_agent_name(agent, standardize_refs=True)