예제 #1
0
 def __getattr__(self, key):
     if self.corpus_context is None:
         raise(GraphModelError('This object is not bound to a corpus context.'))
     if self._id is None:
         raise(GraphModelError('This object has not been loaded with an id yet.'))
     if key == self._annotation._type:
         return self._annotation
     if key in self._node.properties:
         return self._node.properties[key]
     if key == 'label':
         return None
     raise(AttributeError)
예제 #2
0
 def __getattr__(self, key):
     if self.corpus_context is None:
         raise (GraphModelError(
             'This object is not bound to a corpus context.'))
     if key in self._node.keys():
         return self._node[key]
     return None
예제 #3
0
 def __getattr__(self, key):
     if self.corpus_context is None:
         raise (GraphModelError(
             'This object is not bound to a corpus context.'))
     if self._id is None:
         raise (GraphModelError(
             'This object has not been loaded with an id yet.'))
     if key == self._annotation._type:
         return self._annotation
     if key in self._node.keys():
         return self._node[key]
     if key == 'label':
         return None
     if not self._corpus_context.hierarchy.has_subannotation_property(
             self._type, key):
         raise AttributeError
     return None
예제 #4
0
    def delete_subannotation(self, subannotation):
        """
        Deletes a subannotation from the graph
        
        Parameters
        ----------
        subannotation : :class: `~polyglotdb.graph.SubAnnotation`
            the subannotation to be deleted
         """
        for i, sa in enumerate(self._subannotations[subannotation._type]):
            if sa.id == subannotation.id:
                break
        else:
            raise(GraphModelError('Can\'t delete a subannotation that doesn\'t belong to this annotation.'))
        subannotation = self._subannotations[subannotation._type].pop(i)

        statement = '''MATCH (n:{type} {{id: {{id}}}}) DETACH DELETE n'''.format(type = subannotation._type)

        self.corpus_context.execute_cypher(statement, id = subannotation.id)
예제 #5
0
 def __getattr__(self, key):
     if self.corpus_context is None:
         raise(GraphModelError('This object is not bound to a corpus context.'))
     if key in self._node.properties:
         return self._node.properties[key]
     raise(AttributeError)
예제 #6
0
    def __getattr__(self, key):
        if self.corpus_context is None:
            raise(GraphModelError('This object is not bound to a corpus context.'))
        if self._id is None:
            raise(GraphModelError('This object has not been loaded with an id yet.'))
        if key == 'previous':
            if self._previous == 'empty':
                return None
            if self._previous is None:
                res = list(self.corpus_context.execute_cypher(
                    '''MATCH (previous_type)<-[:is_a]-(previous_token)-[:precedes]->(token {id: {id}})
                        RETURN previous_token, previous_type''', id = self._id))
                if len(res) == 0:
                    self._previous = 'empty'
                    return None
                self._previous = LinguisticAnnotation(self.corpus_context)
                self._previous.node = res[0]['previous_token']
                self._previous.type_node = res[0]['previous_type']
            return self._previous
        if key == 'following':
            if self._following == 'empty':
                return None
            if self._following is None:
                res = list(self.corpus_context.execute_cypher(
                    '''MATCH (following_type)<-[:is_a]-(following_token)<-[:precedes]-(token {id: {id}})
                            RETURN following_token, following_type''', id = self._id))
                if len(res) == 0:
                    self._following = 'empty'
                    return None
                self._following = LinguisticAnnotation(self.corpus_context)
                self._following.node = res[0]['following_token']
                self._following.type_node = res[0]['following_type']
            return self._following
        if key == 'speaker':
            if self._speaker == 'empty':
                return None
            if self._speaker is None:
                res = list(self.corpus_context.execute_cypher(
                    '''MATCH (speaker:Speaker)<-[:spoken_by]-(token {id: {id}})
                        RETURN speaker''', id = self._id))
                if len(res) == 0:
                    self._speaker = 'empty'
                    return None
                self._speaker = Speaker(self.corpus_context)
                self._speaker.node = res[0]['speaker']
            return self._speaker
        if key == 'discourse':
            if self._discourse == 'empty':
                return None
            if self._discourse is None:
                res = list(self.corpus_context.execute_cypher(
                    '''MATCH (discourse:Discourse)<-[:spoken_in]-(token {id: {id}})
                        RETURN discourse''', id = self._id))
                if len(res) == 0:
                    self._discourse = 'empty'
                    return None
                self._discourse = Discourse(self.corpus_context)
                self._discourse.node = res[0]['discourse']
            return self._discourse
        if key in self.corpus_context.hierarchy.get_lower_types(self._type):
            if key not in self._subs:
                res = self.corpus_context.execute_cypher(
                    '''MATCH (lower_type)<-[:is_a]-(lower_token:{a_type})-[:contained_by*1..]->(token {{id: {{id}}}})
                        RETURN lower_token, lower_type ORDER BY lower_token.begin'''.format(a_type = key), id = self._id)
                self._subs[key] = []
                for r in res:
                    a = LinguisticAnnotation(self.corpus_context)
                    a.node = r['lower_token']
                    a.type_node = r['lower_type']
                    self._subs[key].append(a)
            return self._subs[key]
        if key in self.corpus_context.hierarchy.get_higher_types(self._type):
            if key not in self._supers:
                res = list(self.corpus_context.execute_cypher(
                    '''MATCH (higher_type)<-[:is_a]-(higher_token:{a_type})<-[:contained_by*1..]-(token {{id: {{id}}}})
                        RETURN higher_token, higher_type'''.format(a_type = key), id = self._id))
                if len(res) == 0:
                    return None
                a =  LinguisticAnnotation(self.corpus_context)
                a.node = res[0]['higher_token']
                a.type_node = res[0]['higher_type']
                self._supers[key] = a
            return self._supers[key]
        try:
            if key in self.corpus_context.hierarchy.subannotations[self._type]:
                if self._preloaded and key not in self._subannotations:
                    return []
                elif key not in self._subannotations:
                    res = self.corpus_context.execute_cypher(
                        '''MATCH (sub:{a_type})-[:annotates]->(token {{id: {{id}}}})
                            RETURN sub'''.format(a_type = key), id = self._id)

                    self._subannotations[key] = []
                    for r in res:
                        a = SubAnnotation(self.corpus_context)
                        a._annotation = self
                        a.node = r['sub']
                        self._subannotations[key].append(a)
                return self._subannotations[key]
        except KeyError:
            pass
        if key in self._node.properties:
            return self._node.properties[key]
        if key in self._type_node.properties:
            return self._type_node.properties[key]
예제 #7
0
    def __getattr__(self, key):
        if self.corpus_context is None:
            raise (GraphModelError(
                'This object is not bound to a corpus context.'))
        if self._id is None:
            raise (GraphModelError(
                'This object has not been loaded with an id yet.'))
        if key == self._type:
            return self
        if key == 'current':
            return self
        if key == 'label' and self._type == 'utterance':
            return '{} ({} to {})'.format(self.discourse.name, self.begin,
                                          self.end)
        if key == 'previous':
            if self._previous == 'empty':
                return None
            if self._previous is None:
                print(
                    'Warning: fetching previous annotation from the database, '
                    'preload this annotation for faster access.')
                res = list(
                    self.corpus_context.execute_cypher(
                        '''MATCH (previous_type)<-[:is_a]-(previous_token)-[:precedes]->(token {id: {id}})
                        RETURN previous_token, previous_type''',
                        id=self._id))
                if len(res) == 0:
                    self._previous = 'empty'
                    return None
                self._previous = LinguisticAnnotation(self.corpus_context)
                self._previous.node = res[0]['previous_token']
                self._previous.type_node = res[0]['previous_type']
            return self._previous
        if key == 'following':
            if self._following == 'empty':
                return None
            if self._following is None:
                print(
                    'Warning: fetching following annotation from the database, '
                    'preload this annotation for faster access.')
                res = list(
                    self.corpus_context.execute_cypher(
                        '''MATCH (following_type)<-[:is_a]-(following_token)<-[:precedes]-(token {id: {id}})
                            RETURN following_token, following_type''',
                        id=self._id))
                if len(res) == 0:
                    self._following = 'empty'
                    return None
                self._following = LinguisticAnnotation(self.corpus_context)
                self._following.node = res[0]['following_token']
                self._following.type_node = res[0]['following_type']
            return self._following
        if key.startswith('previous'):
            p, key = key.split('_', 1)
            p = self.previous
            if p is None:
                return None
            return getattr(p, key)
        if key.startswith('following'):
            p, key = key.split('_', 1)
            f = self.following
            if f is None:
                return None
            return getattr(f, key)
        if key == 'speaker':
            if self._speaker == 'empty':
                return None
            if self._speaker is None:
                print(
                    'Warning: fetching speaker information from the database, '
                    'preload speakers for faster access.')
                res = list(
                    self.corpus_context.execute_cypher(
                        '''MATCH (speaker:Speaker)<-[:spoken_by]-(token {id: {id}})
                        RETURN speaker''',
                        id=self._id))
                if len(res) == 0:
                    self._speaker = 'empty'
                    return None
                self._speaker = Speaker(self.corpus_context)
                self._speaker.node = res[0]['speaker']
            return self._speaker
        if key == 'discourse':
            if self._discourse == 'empty':
                return None
            if self._discourse is None:
                print(
                    'Warning: fetching discourse information from the database, '
                    'preload discourses for faster access.')
                res = list(
                    self.corpus_context.execute_cypher(
                        '''MATCH (discourse:Discourse)<-[:spoken_in]-(token {id: {id}})
                        RETURN discourse''',
                        id=self._id))
                if len(res) == 0:
                    self._discourse = 'empty'
                    return None
                self._discourse = Discourse(self.corpus_context)
                self._discourse.node = res[0]['discourse']
            return self._discourse
        if key in self.corpus_context.hierarchy.get_lower_types(self._type):
            if key not in self._subs:
                print('Warning: fetching {0} information from the database, '
                      'preload {0} annotations for faster access.'.format(key))
                res = self.corpus_context.execute_cypher(
                    '''MATCH (lower_type)<-[:is_a]-(lower_token:{a_type})-[:contained_by*1..]->(token {{id: {{id}}}})
                        RETURN lower_token, lower_type ORDER BY lower_token.begin'''
                    .format(a_type=key),
                    id=self._id)
                self._subs[key] = []
                for r in res:
                    a = LinguisticAnnotation(self.corpus_context)
                    a.node = r['lower_token']
                    a.type_node = r['lower_type']
                    self._subs[key].append(a)
            return self._subs[key]
        if key in self.corpus_context.hierarchy.get_higher_types(self._type):
            if key not in self._supers:
                print('Warning: fetching {0} information from the database, '
                      'preload {0} annotations for faster access.'.format(key))
                res = list(
                    self.corpus_context.execute_cypher(
                        '''MATCH (higher_type)<-[:is_a]-(higher_token:{a_type})<-[:contained_by*1..]-(token {{id: {{id}}}})
                        RETURN higher_token, higher_type'''.format(a_type=key),
                        id=self._id))
                if len(res) == 0:
                    return None
                a = LinguisticAnnotation(self.corpus_context)
                a.node = res[0]['higher_token']
                a.type_node = res[0]['higher_type']
                self._supers[key] = a
            return self._supers[key]
        try:
            if key in self.corpus_context.hierarchy.subannotations[self._type]:
                if self._preloaded and key not in self._subannotations:
                    return []
                elif key not in self._subannotations:
                    print(
                        'Warning: fetching {0} information from the database, '
                        'preload {0} annotations for faster access.'.format(
                            key))
                    res = self.corpus_context.execute_cypher(
                        '''MATCH (sub:{a_type})-[:annotates]->(token {{id: {{id}}}})
                            RETURN sub'''.format(a_type=key),
                        id=self._id)

                    self._subannotations[key] = []
                    for r in res:
                        a = SubAnnotation(self.corpus_context)
                        a._annotation = self
                        a.node = r['sub']
                        self._subannotations[key].append(a)
                return self._subannotations[key]
        except KeyError:
            pass
        if key == 'duration':
            return self.end - self.begin
        if key in self._node.keys():
            return self._node[key]
        if key in self._type_node.keys():
            return self._type_node[key]