it.unimi.di.mg4j.query
Class IntervalSelector

java.lang.Object
  extended by it.unimi.di.mg4j.query.IntervalSelector
All Implemented Interfaces:
FlyweightPrototype<IntervalSelector>

public class IntervalSelector
extends Object
implements FlyweightPrototype<IntervalSelector>

A strategy for selecting reasonable intervals to be shown to the user.

MG4J returns for each query and each document a list of minimal intervals satisfying the query. Due to overlaps and long intervals, this list is not always the best way to show the result of a query to the user. Instances of this class select intervals using two parameters (maximum number of intervals and maximum interval length) and the following algorithm: intervals enqueued in a queue ordered by length; then, they are extracted from the queue and added greedily to the result set as long as they do not overlap any other interval already in the result set, they are not longer than the maximum length, and the result set contains less intervals than the maximum allowed.

If all intervals are longer than the maximum allowed length, then from the shorter interval we extract two new intervals long as half of the maximum allowed length and sharing the left and right extreme, respectively, with the original interval.

Warning: implementations of this class are not required to be thread-safe, but they provide flyweight copies (actually, just copies, as no internal state is shared, but we implement the interface for consistency with the rest of the components used by a QueryEngine). The copy() method is strengthened so to return an object implementing this interface.


Constructor Summary
IntervalSelector()
          Creates a new selector that selects all intervals.
IntervalSelector(int maxIntervals, int intervalMaxLength)
          Creates a new selector.
 
Method Summary
 IntervalSelector copy()
           
 Reference2ObjectMap<Index,SelectedInterval[]> select(DocumentIterator documentIterator, Reference2ObjectMap<Index,SelectedInterval[]> index2Interval)
          Selects intervals from a document iterator.
 SelectedInterval[] select(IntervalIterator intervalIterator)
          Selects intervals from an interval iterator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IntervalSelector

public IntervalSelector()
Creates a new selector that selects all intervals.


IntervalSelector

public IntervalSelector(int maxIntervals,
                        int intervalMaxLength)
Creates a new selector.

Parameters:
maxIntervals - the maximum number of intervals returned by the selector.
intervalMaxLength - the maximum length of an interval returned by the selector.
Method Detail

copy

public IntervalSelector copy()
Specified by:
copy in interface FlyweightPrototype<IntervalSelector>

select

public SelectedInterval[] select(IntervalIterator intervalIterator)
                          throws IOException
Selects intervals from an interval iterator.

Parameters:
intervalIterator - an iterator returning intervals.
Returns:
an array containing the selected intervals; the special empty arrays SelectedInterval.TRUE_ARRAY and SelectedInterval.FALSE_ARRAY are returned for IntervalIterators.TRUE and IntervalIterators.FALSE, respectively.
Throws:
IOException

select

public Reference2ObjectMap<Index,SelectedInterval[]> select(DocumentIterator documentIterator,
                                                            Reference2ObjectMap<Index,SelectedInterval[]> index2Interval)
                                                     throws IOException
Selects intervals from a document iterator.

Intervals will be gathered using the interval iterators returned by the document iterator for the current document.

Parameters:
documentIterator - a document iterator positioned over a document, with callable DocumentIterator.intervalIterator(Index) methods for all indices.
index2Interval - a map that will be cleared and fill with associations from indices to arrays of selected intervals; the special empty arrays SelectedInterval.TRUE_ARRAY and SelectedInterval.FALSE_ARRAY are returned for IntervalIterators.TRUE and IntervalIterators.FALSE, respectively.
Returns:
index2Interval.
Throws:
IOException