コード例 #1
0
    def get_overlaps(self, utterance):
        """
        Query and build overlaps with regards to the subject and object of the heard statement
        Parameters
        ----------
        utterance

        Returns
        -------
            Overlaps containing shared information with the heard statement
        """
        # Role as subject
        query = read_query('thoughts/object_overlap') % (
            utterance.triple.predicate_name, utterance.triple.object_name,
            utterance.triple.subject_name)
        response = self._submit_query(query)

        if response[0]['types']['value'] != '':
            object_overlap = [self._fill_overlap_(elem) for elem in response]
        else:
            object_overlap = []

        # Role as object
        query = read_query('thoughts/subject_overlap') % (
            utterance.triple.predicate_name, utterance.triple.subject_name,
            utterance.triple.object_name)
        response = self._submit_query(query)

        if response[0]['types']['value'] != '':
            subject_overlap = [self._fill_overlap_(elem) for elem in response]
        else:
            subject_overlap = []

        return Overlaps(subject_overlap, object_overlap)
コード例 #2
0
ファイル: long_term_memory.py プロジェクト: jenbishop/pepper
    def get_overlaps(self, capsule):
        # Role as subject
        query = read_query('object_overlap') % (capsule['predicate']['type'],
                                                capsule['object']['label'],
                                                capsule['subject']['label'])
        response = self._submit_query(query)

        if response:
            object_overlap = [{
                'subject': elem['slabel']['value'],
                'author': elem['authorlabel']['value'],
                'date': elem['date']['value'].split('/')[-1]
            } for elem in response]
        else:
            object_overlap = []

        # Role as object
        query = read_query('subject_overlap') % (capsule['predicate']['type'],
                                                 capsule['subject']['label'],
                                                 capsule['object']['label'])
        response = self._submit_query(query)

        if response:
            subject_overlap = [{
                'object': elem['olabel']['value'],
                'author': elem['authorlabel']['value'],
                'date': elem['date']['value'].split('/')[-1]
            } for elem in response]
        else:
            subject_overlap = []

        return {'subject': subject_overlap, 'object': object_overlap}
コード例 #3
0
ファイル: long_term_memory.py プロジェクト: jenbishop/pepper
    def get_gaps_from_entity(self, entity):
        # Role as subject
        query = read_query('subject_gaps') % (entity['label'], entity['label'])
        response = self._submit_query(query)

        if response:
            subject_gaps = [{
                'predicate': elem['p']['value'].split('/')[-1],
                'range': elem['type2']['value'].split('/')[-1]
            } for elem in response if elem['p']['value'].split('/')[-1] not in
                            self._NOT_TO_ASK_PREDICATES]
        else:
            subject_gaps = []

        # Role as object
        query = read_query('object_gaps') % (entity['label'], entity['label'])
        response = self._submit_query(query)

        if response:
            object_gaps = [{
                'predicate': elem['p']['value'].split('/')[-1],
                'domain': elem['type2']['value'].split('/')[-1]
            } for elem in response if elem['p']['value'].split('/')[-1] not in
                           self._NOT_TO_ASK_PREDICATES]
        else:
            object_gaps = []

        return {'subject': subject_gaps, 'object': object_gaps}
コード例 #4
0
    def get_negation_conflicts(self, utterance):
        """
        Query and build negation conflicts, meaning conflicts because predicates are directly negated
        Parameters
        ----------
        utterance

        Returns
        -------
        conflicts: List[NegationConflict]
            List of Conflicts containing the predicate which creates the conflict, and their provenance
        """
        query = read_query('thoughts/negation_conflicts') % (
            utterance.triple.predicate_name, utterance.triple.subject_name,
            utterance.triple.object_name)

        response = self._submit_query(query)
        if response[0] != {}:
            conflicts = [
                self._fill_negation_conflict_(elem) for elem in response
            ]
        else:
            conflicts = []

        return conflicts
コード例 #5
0
    def get_object_cardinality_conflicts(self, utterance):
        """
        Query and build cardinality conflicts, meaning conflicts because predicates should be one to one but have
        multiple object values
        Parameters
        ----------
        utterance

        Returns
        -------
        conflicts: List[CardinalityConflicts]
            List of Conflicts containing the object which creates the conflict, and their provenance
        """
        if str(utterance.triple.predicate_name
               ) not in self._ONE_TO_ONE_PREDICATES:
            return []

        query = read_query('thoughts/object_cardinality_conflicts') % (
            utterance.triple.predicate_name, utterance.triple.subject_name,
            utterance.triple.object_name)

        response = self._submit_query(query)
        if response[0] != {}:
            conflicts = [
                self._fill_cardinality_conflict_(elem) for elem in response
            ]
        else:
            conflicts = []

        return conflicts
コード例 #6
0
ファイル: type_reasoner.py プロジェクト: leolani/pepper
    def _exact_match_wikidata(item):
        """
        Query wikidata for information on this item to get it's semantic type and description.
        :param item:
        :return:
        """
        url = 'https://query.wikidata.org/sparql'

        # Gather combinations
        combinations = [item.lower()]

        for comb in combinations:
            # Try exact matching query
            query = read_query('typing/wikidata_type_and_description') % comb
            try:
                r = requests.get(url, params={'format': 'json', 'query': query}, timeout=3)
                data = r.json() if r.status_code != 500 else None
            except:
                data = None

            # break if we have a hit
            if data:
                break

        if data is not None:
            class_type = data[u'results'][u'bindings'][0][u'itemtypeLabel'][u'value'] \
                if 'itemtypeLabel' in data[u'results'][u'bindings'][0].keys() else None
            description = data[u'results'][u'bindings'][0][u'itemDescription'][u'value'] \
                if 'itemDescription' in data[u'results'][u'bindings'][0].keys() else None

            return class_type, description

        else:
            return None, None
コード例 #7
0
ファイル: long_term_memory.py プロジェクト: jenbishop/pepper
    def __init__(self, address=config.BRAIN_URL_LOCAL):
        """
        Interact with Triple store

        Parameters
        ----------
        address: str
            IP address and port of the Triple store
        """

        self.address = address
        self.namespaces = {}
        self.ontology_paths = {}
        self.format = 'trig'
        self.dataset = Dataset()
        self.query_prefixes = read_query('prefixes')

        self._define_namespaces()
        self._get_ontology_path()
        self._bind_namespaces()

        self.my_uri = None

        self._log = logger.getChild(self.__class__.__name__)
        self._log.debug("Booted")

        self._brain_log = config.BRAIN_LOG_ROOT.format(
            datetime.now().strftime('%Y-%m-%d-%H-%M'))

        # Launch first query
        self.count_statements()
コード例 #8
0
 def count_statements(self):
     """
     Count statements or 'facts' in the brain
     :return:
     """
     query = read_query('count_statements')
     response = self._submit_query(query)
     return response[0]['count']['value']
コード例 #9
0
 def count_friends(self):
     """
     Count number of people I have talked to
     :return:
     """
     query = read_query('count_friends')
     response = self._submit_query(query)
     return response[0]['count']['value']
コード例 #10
0
 def get_my_friends(self):
     """
     Get names of people I have talked to
     :return:
     """
     query = read_query('my_friends')
     response = self._submit_query(query)
     return [elem['name']['value'].split('/')[-1] for elem in response]
コード例 #11
0
 def get_best_friends(self):
     """
     Get names of the 5 people I have talked to the most
     :return:
     """
     query = read_query('best_friends')
     response = self._submit_query(query)
     return [elem['name']['value'] for elem in response]
コード例 #12
0
ファイル: basic_brain.py プロジェクト: leolani/pepper
    def get_predicates(self):
        """
        Get predicates in social ontology
        :return:
        """
        query = read_query('structure exploration/predicates')
        response = self._submit_query(query)

        return [elem['p']['value'].split('/')[-1] for elem in response]
コード例 #13
0
ファイル: basic_brain.py プロジェクト: leolani/pepper
    def get_classes(self):
        """
        Get classes or types in social ontology
        :return:
        """
        query = read_query('structure exploration/classes')
        response = self._submit_query(query)

        return [elem['c']['value'].split('/')[-1] for elem in response]
コード例 #14
0
ファイル: basic_brain.py プロジェクト: leolani/pepper
 def get_type_of_instance(self, label):
     """
     Get types of a certain instance identified by its label
     :param label: label of instance
     :return:
     """
     query = read_query('content exploration/type_of_instance') % label
     response = self._submit_query(query)
     return [elem['type']['value'] for elem in response] if response else []
コード例 #15
0
 def when_last_chat_with(self, actor_label):
     """
     Get time value for the last time I chatted with this person
     :param actor_label: name of person
     :return:
     """
     query = read_query('when_last_chat_with') % (actor_label)
     response = self._submit_query(query)
     return response[0]['time']['value'].split('/')[-1]
コード例 #16
0
    def get_classes(self):
        """
        Get classes in social ontology
        :return:
        """
        query = read_query('classes')
        response = self._submit_query(query)

        return [elem['o']['value'].split('/')[-1] for elem in response]
コード例 #17
0
 def get_instance_of_type(self, instance_type):
     """
     Get isntances of a certain class type
     :param instance_type: name of class in ontology
     :return:
     """
     query = read_query('instance_of_type') % (instance_type)
     response = self._submit_query(query)
     return [elem['name']['value'] for elem in response]
コード例 #18
0
    def get_last_chat_id(self):
        """
        Get the id for the last interaction recorded
        :return: id
        """
        query = read_query('last_chat_id')
        response = self._submit_query(query)

        return int(response[0]['chatid']['value']) if response else 0
コード例 #19
0
 def get_triples_with_predicate(self, predicate):
     """
     Get triples that contain this predicate
     :param predicate:
     :return:
     """
     query = read_query('triples_with_predicate') % predicate
     response = self._submit_query(query)
     return [(elem['sname']['value'], elem['oname']['value'])
             for elem in response]
コード例 #20
0
ファイル: basic_brain.py プロジェクト: leolani/pepper
 def get_instance_of_type(self, instance_type):
     """
     Get instances of a certain class type
     :param instance_type: name of class in ontology
     :return:
     """
     query = read_query(
         'content exploration/instance_of_type') % instance_type
     response = self._submit_query(query)
     return [elem['name']['value'] for elem in response] if response else []
コード例 #21
0
ファイル: long_term_memory.py プロジェクト: jenbishop/pepper
    def check_statement_existence(self, instance_url):
        query = read_query('instance_existence') % (instance_url)
        response = self._submit_query(query)

        if response[0] != {}:
            response = [{
                'date': elem['date']['value'].split('/')[-1],
                'authorlabel': elem['authorlabel']['value']
            } for elem in response]

        return response
コード例 #22
0
    def get_entity_gaps(self, entity, exclude=None):
        """
        Query and build gaps with regards to the range and domain of the given entity and its predicates
        Parameters
        ----------
        entity: dict
            Information regarding the entity

        Returns
        -------
            Gaps object containing gaps related to range and domain information that could be learned
        """
        # Role as subject
        query = read_query('thoughts/subject_gaps') % (
            entity.label, entity.label if exclude is None else exclude.label)
        response = self._submit_query(query)

        if response:
            subject_gaps = [
                self._fill_entity_gap_(elem) for elem in response if elem['p']
                ['value'].split('/')[-1] not in self._NOT_TO_ASK_PREDICATES
            ]

        else:
            subject_gaps = []

        # Role as object
        query = read_query('thoughts/object_gaps') % (
            entity.label, entity.label if exclude is None else exclude.label)
        response = self._submit_query(query)

        if response:
            object_gaps = [
                self._fill_entity_gap_(elem) for elem in response if elem['p']
                ['value'].split('/')[-1] not in self._NOT_TO_ASK_PREDICATES
            ]

        else:
            object_gaps = []

        return Gaps(subject_gaps, object_gaps)
コード例 #23
0
    def get_labels_and_classes(self):
        """
        Get classes in social ontology
        :return:
        """
        query = read_query('labels_and_classes')
        response = self._submit_query(query)

        temp = dict()
        for r in response:
            temp[r['l']['value']] = r['o']['value'].split('/')[-1]

        return temp
コード例 #24
0
    def _check_instance_novelty_(self, instance_url):
        """
        Query if an instance (entity) has been heard about before
        Parameters
        ----------
        instance_url: str
            URI of instance

        Returns
        -------
        conflicts: List[StatementNovelty]
            List of provenance for the instance
        """
        query = read_query('thoughts/entity_novelty') % instance_url
        response = self._submit_query(query, ask=True)

        return response
コード例 #25
0
    def get_last_turn_id(self, chat_id):
        """
        Get the id for the last turn in the given chat
        :param chat_id: id for chat of interest
        :return:  id
        """
        query = read_query('last_turn_id') % (chat_id)
        response = self._submit_query(query)

        last_turn = 0
        for turn in response:
            turn_uri = turn['s']['value']
            turn_id = turn_uri.split('/')[-1][10:]
            turn_id = int(turn_id)

            if turn_id > last_turn:
                last_turn = turn_id

        return last_turn
コード例 #26
0
ファイル: long_term_memory.py プロジェクト: jenbishop/pepper
    def create_chat_id(self, actor, date):
        """
        Determine chat id depending on my last conversation with this person
        :param actor:
        :param date:
        :return:
        """
        query = read_query('last_chat_with') % (actor)
        response = self._submit_query(query)

        if response and int(response[0]['day']['value']) == int(date.day) \
                and int(response[0]['month']['value']) == int(date.month) \
                and int(response[0]['year']['value']) == int(date.year):
            # Chatted with this person today so same chat id
            chat_id = int(response[0]['chatid']['value'])
        else:
            # Either have never chatted with this person, or I have but not today. Add one to latest chat
            chat_id = self.get_last_chat_id() + 1

        return chat_id
コード例 #27
0
    def get_conflicts_with_one_to_one_predicate(self, one_to_one_predicate):
        query = read_query('one_to_one_conflicts') % one_to_one_predicate

        response = self._submit_query(query)
        conflicts = []
        for item in response:
            conflict = {
                'subject': item['sname']['value'],
                'predicate': one_to_one_predicate,
                'objects': []
            }

            for x in item['pairs']['value'].split(';'):
                [val, auth] = x.split(',')
                option = {'value': val, 'author': auth}
                conflict['objects'].append(option)

            conflicts.append(conflict)

        return conflicts
コード例 #28
0
ファイル: long_term_memory.py プロジェクト: jenbishop/pepper
    def get_object_cardinality_conflicts_with_statement(self, capsule):
        # Case fold
        capsule = casefold_capsule(capsule, format='triple')

        if capsule['predicate']['type'] not in self._ONE_TO_ONE_PREDICATES:
            return [{}]

        query = read_query('object_cardinality_conflicts') % (
            capsule['predicate']['type'], capsule['subject']['label'],
            capsule['object']['label'])

        response = self._submit_query(query)

        if response[0] != {}:
            response = [{
                'date': elem['date']['value'].split('/')[-1],
                'authorlabel': elem['authorlabel']['value'],
                'oname': elem['oname']['value']
            } for elem in response]

        return response
コード例 #29
0
ファイル: long_term_memory.py プロジェクト: leolani/pepper
    def __init__(self, address=config.BRAIN_URL_LOCAL, clear_all=False):
        # type: (str, bool) -> LongTermMemory
        """
        Interact with Triple store

        Parameters
        ----------
        address: str
            IP address and port of the Triple store
        """

        super(LongTermMemory, self).__init__(address, clear_all)

        self.myself = None
        self.query_prefixes = read_query('prefixes')  # USED ONLY WHEN QUERYING
        self.thought_generator = ThoughtGenerator()
        self.location_reasoner = LocationReasoner()
        self.type_reasoner = TypeReasoner()

        self.set_location_label = self.location_reasoner.set_location_label
        self.reason_location = self.location_reasoner.reason_location
コード例 #30
0
ファイル: type_reasoner.py プロジェクト: leolani/pepper
    def _exact_match_dbpedia(self, item):
        """
        Query dbpedia for information on this item to get it's semantic type and description.
        :param item:
        :return:
        """
        # Gather combinations
        combinations = [item, item.capitalize(), item.lower(), item.title()]

        for comb in combinations:
            # Try exact matching query
            query = read_query('typing/dbpedia_type_and_description') % comb
            response = self._submit_query(query)

            # break if we have a hit
            if response:
                break

        class_type = response[0]['label_type']['value'] if response else None
        description = response[0]['description']['value'].split('.')[0] if response else None

        return class_type, description