Пример #1
0
class MultiSearch:
    """A container of sub-lists which will be searched for via the search method."""
    def __init__(self):
        self._t = PrefixTree()

    def insert(self, key, val):
        """inserts a new sublist, and an object identifying it.  Inserting the same key multiple times
		   is allowed, as long as val is different.
		   @param key: a tuple sub-list
		   @param val: the object associated with the key
		"""
        try:
            self._t.get(key).append(val)
        except (KeyError):
            self._t.insert(key, [val])

    def search(self, li):
        """searches li for sublists contained in this object.
			@param li: a sequence to be searched
			@return: a dict of val -> [int, int, ...] where val is the object
			associated with a found sublist,, and [int, int,...] is a list of one or
			more start indexes into li, where the sublist associated with val was found"""
        d = {}
        for i in range(len(li) - 1):
            (hitNodes, rest) = self._t.getUpToLongestPrefix(li[i:])
            for hits in [node.value for node in hitNodes]:
                for hit in hits:
                    if hit in d:
                        #print relevantMistakes[curM]
                        d[hit].append(i)
                    else:
                        d[hit] = [
                            i,
                        ]
        return d
Пример #2
0
class MultiSearch:
	"""A container of sub-lists which will be searched for via the search method."""
	def __init__(self):
		self._t=PrefixTree()

	def insert(self,key,val):
		"""inserts a new sublist, and an object identifying it.  Inserting the same key multiple times
		   is allowed, as long as val is different.
		   @param key: a tuple sub-list
		   @param val: the object associated with the key
		"""
		try:
			self._t.get(key).append(val)
		except(KeyError):
			self._t.insert(key,[val])

	def search(self,li):
		"""searches li for sublists contained in this object.
			@param li: a sequence to be searched
			@return: a dict of val -> [int, int, ...] where val is the object
			associated with a found sublist,, and [int, int,...] is a list of one or
			more start indexes into li, where the sublist associated with val was found"""
		d={}
		for i in range(len(li)-1):
			(hitNodes, rest) = self._t.getUpToLongestPrefix(li[i:])
			for hits in [node.value for node in hitNodes]:
				for hit in hits:
					if hit in d:
						#print relevantMistakes[curM]
						d[hit].append(i)
					else:
						d[hit]=[i,]
		return d
Пример #3
0
 def __init__(self):
     self._t = PrefixTree()
Пример #4
0
	def __init__(self):
		self._t=PrefixTree()