def _find_seeks_bookmarks(fileobj, queries, debug=False):
    """Use bookmarks to find sections of the file that *may* contain
    the data we're looking for. End locations are required to ensure
    prompt termination, since we have no idea if the file contains the
    information we're looking for."""
    timer = Timer()
    locs = Counter()
    endlocs = {}
    if debug:
        print "  Searching bookmarks..."
    for query in sorted(queries):
        start, end = fileobj.find_bookmark(query.encode('utf-8'),
                                           give_range=True)
        #print "Got",start,end,"for",query.encode('utf-8')
        if not end:
            endlocs[start] = None   # None = EOF
        elif start not in endlocs or endlocs[start] < end:
            endlocs[start] = end
        #print '  ',endlocs[start]
        locs.update((start,))

    # Because of the inexact nature of bookmarks, we need to normalize
    # the locations to a non-overlapping set of ranges, some of which
    # may be infinite.
    start = 0
    end = 0
    nresults = 0
    for nextstart, nextnresults in sorted(locs.items()):
        nextend = endlocs[nextstart] if nextstart in endlocs else None
        assert(nextnresults > 0)
        #print "Got",startloc,endloc
        if end is None or nextstart <= end:
            # Previous range ends after start of this range (or the
            # previous range is infinite). Extend the previous range
            # to include this one.
            if nextend is None or (end is not None and nextend > end):
                end = nextend
            nresults += nextnresults
        else:
            # The ranges do not overlap. Return the previous range and
            # then save the current range.
            if nresults > 0:
                #assert(end is None or end > start)
                yield (start, end, nresults)
            start = nextstart
            end = nextend
            nresults = nextnresults
    if nresults > 0:
        #assert(end is None or end > start)
        yield (start, end, nresults)
    if debug:
        print '  Completed in', timer, 'seconds.'


# Parser objects
dataset/ETHPy150Open nandhp/python-imdb/imdb/parsers.py/_find_seeks_bookmarks/original