<commit_before>from __future__ import absolute_import, print_function, unicode_literals
from builtins import dict, str
import re
import json
import logging
import objectpath
from indra.statements import Influence, Concept, Evidence


logger = logging.getLogger('eidos')

class EidosJsonLdProcessor(object):
    """This processor extracts INDRA Statements from Eidos JSON-LD output.

    Parameters
    ----------
    json_dict : dict
        A JSON dictionary containing the Eidos extractions in JSON-LD format.

    Attributes
    ----------
    tree : objectpath.Tree
        The objectpath Tree object representing the extractions.
    statements : list[indra.statements.Statement]
        A list of INDRA Statements that were extracted by the processor.
    """
    def __init__(self, json_dict):
        self.tree = objectpath.Tree(json_dict)
        self.statements = []

    def get_events(self):
        events = \
            self.tree.execute("$.extractions[(@.@type is 'DirectedRelation')]")
        if not events:
            return

        entities = \
            self.tree.execute("$.extractions[(@.@type is 'Entity')]")
        entity_ids = \
            self.tree.execute("$.extractions[(@.@type is 'Entity')].@id")
        entity_dict = {id:entity for id, entity in zip(entity_ids, entities)}

        # The first state corresponds to increase/decrease
        def get_polarity(x):
            # x is either subj or obj
            if 'states' in x.keys():
                if x['states'][0]['type'] == 'DEC':
                    return -1
                elif x['states'][0]['type'] == 'INC':
                    return 1
                else:
                    return None
            else:
                return None

        def get_adjectives(x):
            # x is either subj or obj
            if 'states' in x.keys():
                if 'modifiers' in x['states'][0].keys():
                    return [mod['text'] for mod in
                            x['states'][0]['modifiers']]
                else:
                    return []
            else:
                return []

        def _get_eidos_groundings(entity):
            """Return Eidos groundings are a list of tuples with scores."""
            grounding_tag = entity.get('groundings')
            # If no grounding at all, just return None
            if not grounding_tag:
                return None
            # The grounding dict can still be empty
            grounding_dict = grounding_tag[0]
            if not grounding_dict or 'values' not in grounding_dict:
                return None
            grounding_values = grounding_dict.get('values', [])
            # Otherwise get all the groundings that have non-zero score
            grounding_tuples = []
            for g in grounding_values:
                if g['value'] > 0:
                    if g['ontologyConcept'].startswith('/'):
                        concept = g['ontologyConcept'][1:]
                    else:<commit_msg>Simplify getting adjectives and polarity<commit_after>from __future__ import absolute_import, print_function, unicode_literals
from builtins import dict, str
import re
import json
import logging
import objectpath
from indra.statements import Influence, Concept, Evidence


logger = logging.getLogger('eidos')

class EidosJsonLdProcessor(object):
    """This processor extracts INDRA Statements from Eidos JSON-LD output.

    Parameters
    ----------
    json_dict : dict
        A JSON dictionary containing the Eidos extractions in JSON-LD format.

    Attributes
    ----------
    tree : objectpath.Tree
        The objectpath Tree object representing the extractions.
    statements : list[indra.statements.Statement]
        A list of INDRA Statements that were extracted by the processor.
    """
    def __init__(self, json_dict):
        self.tree = objectpath.Tree(json_dict)
        self.statements = []

    def get_events(self):
        events = \
            self.tree.execute("$.extractions[(@.@type is 'DirectedRelation')]")
        if not events:
            return

        entities = \
            self.tree.execute("$.extractions[(@.@type is 'Entity')]")
        entity_ids = \
            self.tree.execute("$.extractions[(@.@type is 'Entity')].@id")
        entity_dict = {id:entity for id, entity in zip(entity_ids, entities)}

        # The first state corresponds to increase/decrease
        def get_polarity(x):
            # x is either subj or obj
            if 'states' in x:
                if x['states'][0]['type'] == 'DEC':
                    return -1
                elif x['states'][0]['type'] == 'INC':
                    return 1
                else:
                    return None
            else:
                return None

        def get_adjectives(x):
            # x is either subj or obj
            if 'states' in x:
                if 'modifiers' in x['states'][0]:
                    return [mod['text'] for mod in
                            x['states'][0]['modifiers']]
                else:
                    return []
            else:
                return []

        def _get_eidos_groundings(entity):
            """Return Eidos groundings are a list of tuples with scores."""
            grounding_tag = entity.get('groundings')
            # If no grounding at all, just return None
            if not grounding_tag:
                return None
            # The grounding dict can still be empty
            grounding_dict = grounding_tag[0]
            if not grounding_dict or 'values' not in grounding_dict:
                return None
            grounding_values = grounding_dict.get('values', [])
            # Otherwise get all the groundings that have non-zero score
            grounding_tuples = []
            for g in grounding_values:
                if g['value'] > 0:
                    if g['ontologyConcept'].startswith('/'):
                        concept = g['ontologyConcept'][1:]
                    else:
{'fraction_non_alphanumeric': 0.09789859906604403, 'fraction_numerical': 0.003002001334222815, 'mean_word_length': 1.720961887477314, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '224116', 'n_tokens_mistral': 1590, 'n_tokens_neox': 1518, 'n_words': 475}