Beispiel #1
0
 def __or__(self, x):
     return self.__class__(
         weightedUnion(self._dict, x._dict)[1],
         union(self._words, x._words),
         self._index,
         )
     return self.__class__(result, self._words+x._words, self._index)
Beispiel #2
0
    def test_None_is_smallest(self):
        t = self._makeOne()
        for i in range(999): # Make sure we multiple buckets
            t[i] = i*i
        t[None] = -1
        for i in range(-99,0): # Make sure we multiple buckets
            t[i] = i*i
        self.assertEqual(list(t), [None] + list(range(-99, 999)))
        self.assertEqual(list(t.values()),
                         [-1] + [i*i for i in range(-99, 999)])
        self.assertEqual(t[2], 4)
        self.assertEqual(t[-2], 4)
        self.assertEqual(t[None], -1)
        t[None] = -2
        self.assertEqual(t[None], -2)
        t2 = t.__class__(t)
        del t[None]
        self.assertEqual(list(t), list(range(-99, 999)))

        if 'Py' in self.__class__.__name__:
            return
        from BTrees.OOBTree import difference, union, intersection
        self.assertEqual(list(difference(t2, t).items()), [(None, -2)])
        self.assertEqual(list(union(t, t2)), list(t2))
        self.assertEqual(list(intersection(t, t2)), list(t))
Beispiel #3
0
    def near(self, x):
        result = IIBucket()
        dict = self._dict
        xdict = x._dict
        xhas = xdict.has_key
        positions = self._index.positions
        for id, score in dict.items():
            if not xhas(id): continue
            p=(map(lambda i: (i,0), positions(id,self._words))+
               map(lambda i: (i,1), positions(id,x._words)))
            p.sort()
            d = lp = 9999
            li = None
            lsrc = None
            for i,src in p:
                if i is not li and src is not lsrc and li is not None:
                    d = min(d,i-li)
                li = i
                lsrc = src
            if d==lp: score = min(score,xdict[id]) # synonyms
            else: score = (score+xdict[id])/d
            result[id] = score

        return self.__class__(
            result, union(self._words, x._words), self._index)
Beispiel #4
0
    def test_None_is_smallest(self):
        t = self._makeOne()
        for i in range(999):  # Make sure we multiple buckets
            t[i] = i * i
        t[None] = -1
        for i in range(-99, 0):  # Make sure we multiple buckets
            t[i] = i * i
        self.assertEqual(list(t), [None] + list(range(-99, 999)))
        self.assertEqual(list(t.values()),
                         [-1] + [i * i for i in range(-99, 999)])
        self.assertEqual(t[2], 4)
        self.assertEqual(t[-2], 4)
        self.assertEqual(t[None], -1)
        t[None] = -2
        self.assertEqual(t[None], -2)
        t2 = t.__class__(t)
        del t[None]
        self.assertEqual(list(t), list(range(-99, 999)))

        if 'Py' in self.__class__.__name__:
            return
        from BTrees.OOBTree import difference, union, intersection
        self.assertEqual(list(difference(t2, t).items()), [(None, -2)])
        self.assertEqual(list(union(t, t2)), list(t2))
        self.assertEqual(list(intersection(t, t2)), list(t))
Beispiel #5
0
    def near(self, x):
        result = IIBucket()
        dict = self._dict
        xdict = x._dict
        xhas = xdict.has_key
        positions = self._index.positions
        for id, score in dict.items():
            if not xhas(id): continue
            p = (map(lambda i: (i, 0), positions(id, self._words)) +
                 map(lambda i: (i, 1), positions(id, x._words)))
            p.sort()
            d = lp = 9999
            li = None
            lsrc = None
            for i, src in p:
                if i is not li and src is not lsrc and li is not None:
                    d = min(d, i - li)
                li = i
                lsrc = src
            if d == lp: score = min(score, xdict[id])  # synonyms
            else: score = (score + xdict[id]) / d
            result[id] = score

        return self.__class__(result, union(self._words, x._words),
                              self._index)
Beispiel #6
0
 def _combine_union(self, values, object):
     if not values: return
     set = None
     for v in values:
         sv = self._standardizeValue(v, object)
         if not sv: continue
         set = union(set, sv)
     return set
 def addTrainingDocument(self,doc_id,tags):
     """
     """
     storage = getUtility(INounPhraseStorage)
     importantNouns = storage.getNounTerms(doc_id,self.noNounRanksToKeep)
     
     self.trainingDocs[doc_id] = (importantNouns,tags)
     self.allNouns = union(self.allNouns,OOSet(importantNouns))
 def _combine_union(self,values,object):
   if not values: return
   set= None
   for v in values:
     sv= self._standardizeValue(v,object)
     if not sv: continue
     set= union(set,sv)
   return set
Beispiel #9
0
 def testFunkyKeyIteration(self):
     # The internal set iteration protocol allows "iterating over" a
     # a single key as if it were a set.
     N = 100
     union, mkset = self.union, self.mkset
     slow = mkset()
     for i in range(N):
         slow = union(slow, mkset([i]))
     fast = self.multiunion(range(N))  # acts like N distinct singleton sets
     self.assertEqual(len(slow), N)
     self.assertEqual(len(fast), N)
     self.assertEqual(list(slow), list(fast))
     self.assertEqual(list(fast), range(N))
Beispiel #10
0
 def testFunkyKeyIteration(self):
     # The internal set iteration protocol allows "iterating over" a
     # a single key as if it were a set.
     N = 100
     union, mkset = self.union, self.mkset
     slow = mkset()
     for i in range(N):
         slow = union(slow, mkset([i]))
     fast = self.multiunion(range(N))  # acts like N distinct singleton sets
     self.assertEqual(len(slow), N)
     self.assertEqual(len(fast), N)
     self.assertEqual(list(slow), list(fast))
     self.assertEqual(list(fast), range(N))
 def train(self):
     """
     """
     presentNouns = dict()
     trainingData = []
     if not self.allNouns:
         storage = getUtility(INounPhraseStorage)
         for key in self.trainingDocs.keys():
             importantNouns = storage.getNounTerms(
                 key,
                 self.noNounRanksToKeep)
             self.allNouns = union(self.allNouns,OOSet(importantNouns))
     for item in self.allNouns:
         presentNouns.setdefault(item,0)
     
     for (nouns,tags) in self.trainingDocs.values():
         nounPresence = presentNouns.copy()
         for noun in nouns:
             nounPresence[noun] = 1
         for tag in tags:
             trainingData.append((nounPresence,tag,))
     if trainingData:
         self.classifier = NaiveBayesClassifier.train(trainingData)
Beispiel #12
0
 def __or__(self, x):
     return self.__class__(
         weightedUnion(self._dict, x._dict)[1],
         union(self._words, x._words),
         self._index,
     )
Beispiel #13
0
 def __and__(self, x):
     return self.__class__(
         weightedIntersection(self._dict, x._dict)[1],
         union(self._words, x._words),
         self._index,
     )
Beispiel #14
0
 def __and__(self, x):
     return self.__class__(
         weightedIntersection(self._dict, x._dict)[1],
         union(self._words, x._words),
         self._index,
         )
Beispiel #15
0
 def __or__(self, other):
     return QuerySet(union(OOTreeSet(self), OOTreeSet(other)))
Beispiel #16
0
 def union(self, *args):
     from BTrees.OOBTree import union
     return union(*args)
 def __or__(self, x):
     return self.__class__(
         weightedUnion(self._dict, x._dict),
         union(self._words, x._words),
         self._index,
         )
Beispiel #18
0
 def union(self, *args):
     from BTrees.LFBTree import union
     return union(*args)
Beispiel #19
0
 def __or__(self, x):
     return self.__class__(
         ii_union(self._dict, x._dict),
         union(self._words, x._words),
         self._index,
     )
Beispiel #20
0
 def __and__(self, x):
     return self.__class__(
         intersection(self._dict, x._dict),
         union(self._words, x._words),
         self._index,
     )
Beispiel #21
0
 def __and__(self, x):
     return self.__class__(
         intersection(self._dict, x._dict),
         union(self._words, x._words),
         self._index,
         )