Beispiel #1
0
 def testContainsDel(self):
     ''' tests the contains and del functions '''
     d = MemoryCache()
     d.fetchObjectId("10", self.add, *(), **{'a': 3, 'b': 4})
     assert "10" in d
     del d["10"]
     assert "10" not in d
Beispiel #2
0
 def testContainsDel(self):
     ''' tests the contains and del functions '''
     d = MemoryCache()
     d.fetchObjectId("10", self.add, *(), **{'a':3, 'b':4})
     assert "10" in d
     del d["10"]
     assert "10" not in d
Beispiel #3
0
class RelationTypes(object):

    SHORT_POS = { 
        'IN': 'p', 'TO': 'p', 'RB': 'p', 'RP': 'p',
        'VB': 'v', 'MD': 'v', 
        'NN': 'v'                   # map nouns to verbs as they are the default results
    }

    AUXILIARY_VERBS = ('be', 'have', 'may', 'should', 'can', 'do', 'might', 'must' 'ought', 'shall', 'would')
    
    def __init__(self):
        self.w = WordNetLemmatizer()
        self.cache = MemoryCache()
        # self.cache.fetchObjectId(text,  self.w.lemmatize, text.split(), pos='v' ):

    @staticmethod
    @MemoryCached
    def getPos(text):
        """ returns the pos tags for the given text """
        posMap = []
        for term, pos in pos_tag( text.split() ):
            mapping = [ spos for lpos, spos in RelationTypes.SHORT_POS.items() if pos.startswith(lpos) ]
            pos = mapping[0] if mapping else ''
            posMap.append( pos )

        return posMap


    def partitionRelation(self, text, removeAuxiliaryVerbs=True) :
        """ partitions a relation into the following comparision format:
              {'v': ( v1, ...), 'p': ('by', ...), '': ('also', ...) }
            @param[in] text the text describing the link
            @returns   the partitioned text 
        """
        result = defaultdict( list )
        for term, pos in zip( text.split(), self.getPos(text)):
            result[pos].append(  
                 self.cache.fetchObjectId(term, self.w.lemmatize, term, pos='v' ) if pos=='v' else term )

        if 'v' in result and removeAuxiliaryVerbs:
            result['v'] = self.removeAuxiliaryVerbs( result['v'] )
 
        return result

    @staticmethod
    def removeAuxiliaryVerbs( verbList ):
        """ removes auxiliary verbs from the given list, provided that
            at least one verb remains in the list.

            @param[in] verbList a list of verbs to analyze
            @returns   the cleaned verblist 
        """
        return [ v for v in verbList if v not in RelationTypes.AUXILIARY_VERBS ] or [ verbList[0] ]
Beispiel #4
0
 def __init__(self):
     self.w = WordNetLemmatizer()
     self.cache = MemoryCache()
Beispiel #5
0
class RelationTypes(object):

    SHORT_POS = {
        'IN': 'p',
        'TO': 'p',
        'RB': 'p',
        'RP': 'p',
        'VB': 'v',
        'MD': 'v',
        'NN': 'v'  # map nouns to verbs as they are the default results
    }

    AUXILIARY_VERBS = ('be', 'have', 'may', 'should', 'can', 'do', 'might',
                       'must'
                       'ought', 'shall', 'would')

    def __init__(self):
        self.w = WordNetLemmatizer()
        self.cache = MemoryCache()
        # self.cache.fetchObjectId(text,  self.w.lemmatize, text.split(), pos='v' ):

    @staticmethod
    @MemoryCached
    def getPos(text):
        """ returns the pos tags for the given text """
        posMap = []
        for term, pos in pos_tag(text.split()):
            mapping = [
                spos for lpos, spos in RelationTypes.SHORT_POS.items()
                if pos.startswith(lpos)
            ]
            pos = mapping[0] if mapping else ''
            posMap.append(pos)

        return posMap

    def partitionRelation(self, text, removeAuxiliaryVerbs=True):
        """ partitions a relation into the following comparision format:
              {'v': ( v1, ...), 'p': ('by', ...), '': ('also', ...) }
            @param[in] text the text describing the link
            @returns   the partitioned text 
        """
        result = defaultdict(list)
        for term, pos in zip(text.split(), self.getPos(text)):
            result[pos].append(
                self.cache.fetchObjectId(term, self.w.lemmatize, term, pos='v'
                                         ) if pos == 'v' else term)

        if 'v' in result and removeAuxiliaryVerbs:
            result['v'] = self.removeAuxiliaryVerbs(result['v'])

        return result

    @staticmethod
    def removeAuxiliaryVerbs(verbList):
        """ removes auxiliary verbs from the given list, provided that
            at least one verb remains in the list.

            @param[in] verbList a list of verbs to analyze
            @returns   the cleaned verblist 
        """
        return [v for v in verbList if v not in RelationTypes.AUXILIARY_VERBS
                ] or [verbList[0]]
Beispiel #6
0
 def __init__(self):
     self.w = WordNetLemmatizer()
     self.cache = MemoryCache()