<commit_before>"""
Command that will allow for a user to inject triples into a database.
"""
from rhobot.components.commands.base_command import BaseCommand
from rdflib.namespace import RDF
import logging
import json

logger = logging.getLogger(__name__)

class InjectTriples(BaseCommand):

    def initialize_command(self):
        super(InjectTriples, self).initialize_command()

        logger.info('Initialize Command')
        self._initialize_command(identifier='inject_triples', name='Inject Triples',
                                 additional_dependencies={'rho_bot_storage_client'})

    def command_start(self, request, initial_session):
        """
        Provide the configuration details back to the requester and end the command.
        :param request:
        :param initial_session:
        :return:
        """
        form = self.xmpp['xep_0004'].make_form()
        form.add_field(var='json_data', ftype='text-multi', label='RDF Content (JSON)')

        initial_session['payload'] = form
        initial_session['next'] = self.store_results
        initial_session['has_next'] = False

        return initial_session

    def store_results(self, request, session):

        injection_dictionary = json.loads(request['values']['json_data'])

        logger.info('Going to inject data: %s' % injection_dictionary)

        # This needs to go through and create all the nodes with the URI's and literals, then go through
        # and add all of the bnodes after all of the nodes have been inserted.
        bnode_dictionary = self.create_nodes(injection_dictionary)
        self.create_bnode_relationships(injection_dictionary, bnode_dictionary)

        logger.info('Finished')

        session['notes'] = (('info', 'Triples imported to database',),)
        session['has_next'] = False
        session['next'] = None

        return session

    def create_nodes(self, data):
        """
        Create all of the nodes with the literals and resources.
        :param data:
        :return:
        """
        bnode_mappings = dict()

        for uri, node_definition in data.iteritems():
            result = self.process_node_definition(uri, node_definition)
            bnode_mappings[uri] = result

        for uri, node_definition in data.iteritems():
<commit_msg>Fix implementation of inject triples<commit_after>"""
Command that will allow for a user to inject triples into a database.
"""
from rhobot.components.commands.base_command import BaseCommand
from rdflib.namespace import RDF
import logging
import json

logger = logging.getLogger(__name__)

class InjectTriples(BaseCommand):

    def initialize_command(self):
        super(InjectTriples, self).initialize_command()

        logger.info('Initialize Command')
        self._initialize_command(identifier='inject_triples', name='Inject Triples',
                                 additional_dependencies={'rho_bot_storage_client'})

    def command_start(self, request, initial_session):
        """
        Provide the configuration details back to the requester and end the command.
        :param request:
        :param initial_session:
        :return:
        """
        form = self.xmpp['xep_0004'].make_form()
        form.add_field(var='json_data', ftype='text-multi', label='RDF Content (JSON)')

        initial_session['payload'] = form
        initial_session['next'] = self.store_results
        initial_session['has_next'] = False

        return initial_session

    def store_results(self, request, session):

        injection_dictionary = json.loads(request['values']['json_data'])

        logger.info('Going to inject data: %s' % injection_dictionary)

        # This needs to go through and create all the nodes with the URI's and literals, then go through
        # and add all of the bnodes after all of the nodes have been inserted.
        self.create_nodes(injection_dictionary)

        logger.info('Finished')

        session['notes'] = (('info', 'Triples imported to database',),)
        session['has_next'] = False
        session['next'] = None

        return session

    def create_nodes(self, data):
        """
        Create all of the nodes with the literals and resources.
        :param data:
        :return:
        """
        bnode_mappings = dict()

        for uri, node_definition in data.iteritems():
            result = self.process_node_definition(uri, node_definition)
            bnode_mappings[uri] = result

        for uri, node_definition in data.iteritems():

{'fraction_non_alphanumeric': 0.09463652482269504, 'fraction_numerical': 0.0017730496453900709, 'mean_word_length': 2.931184668989547, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4220376', 'n_tokens_mistral': 1219, 'n_tokens_neox': 1171, 'n_words': 341}