public <T extends Comparable<T>>List<T> sortDocsUsingDocQuery(List<T> recentItems,List<T> sortItems,QueryTransform<T> docTransform,Set<T> exclusions){
  boolean useRank=false;
  boolean bestScore=false;
  boolean useThreshold=false;
  double threshold=0.999;
  List<T> result=new ArrayList<>();
  Map<Vector,T> sortVectors=new HashMap<>();
  Map<Vector,Double> scores=new HashMap<>();
  boolean comparisonsMade=false;
  boolean foundItemsToSort=false;
  List<T> alreadySeen=new ArrayList<>();
  List<T> notFound=new ArrayList<>();
  for (  T item : sortItems) {
    if (!recentItems.contains(item) && !exclusions.contains(item)) {
      Vector v=docVecReader.getVector(docTransform.toSV(item));
      if (v != null && !v.isZeroVector()) {
        foundItemsToSort=true;
        sortVectors.put(v,item);
        scores.put(v,0D);
      }
 else {
        notFound.add(item);
        logger.warn("Can't find vector for sort item " + item);
      }
    }
 else {
      if (logger.isDebugEnabled())       logger.debug("Not sorting already seen article " + item);
      alreadySeen.add(item);
    }
  }
  if (!foundItemsToSort) {
    logger.debug("No sort items so returning empty list");
    return new ArrayList<>();
  }
  for (  T recent : recentItems) {
    if (logger.isDebugEnabled())     logger.debug("Recent item " + recent);
    String recentDoc=docTransform.toSV(recent);
    Vector vectorRecent=docVecReader.getVector(recentDoc);
    if (vectorRecent != null && !vectorRecent.isZeroVector()) {
      comparisonsMade=true;
      if (useRank) {
        Map<Vector,Double> scoresLocal=new HashMap<>();
        for (        Map.Entry<Vector,T> e : sortVectors.entrySet())         scoresLocal.put(e.getKey(),vectorRecent.measureOverlap(e.getKey()));
        List<Vector> orderedLocal=CollectionTools.sortMapAndLimitToList(scoresLocal,scoresLocal.size());
        double count=1;
        for (        Vector vOrdered : orderedLocal) {
          scores.put(vOrdered,scores.get(vOrdered) + count);
          count++;
        }
      }
 else {
        for (        Map.Entry<Vector,T> e : sortVectors.entrySet()) {
          double overlap=vectorRecent.measureOverlap(e.getKey());
          double current=scores.get(e.getKey());
          if (!Double.isNaN(overlap)) {
            if (logger.isDebugEnabled())             logger.debug("Overlap with " + e.getValue() + " is "+ overlap);
            if (bestScore) {
              if (overlap > current)               scores.put(e.getKey(),overlap);
            }
 else {
              if (useThreshold) {
                if (current < threshold && overlap > current)                 scores.put(e.getKey(),overlap);
 else                 if (current > threshold && overlap > threshold)                 scores.put(e.getKey(),overlap + current);
              }
 else               scores.put(e.getKey(),overlap + current);
            }
          }
        }
      }
    }
 else     logger.warn("Can't get vector for recent item " + recent);
  }
  if (comparisonsMade) {
    List<Vector> ordered;
    if (useRank)     ordered=CollectionTools.sortMapAndLimitToList(scores,scores.size(),false);
 else     ordered=CollectionTools.sortMapAndLimitToList(scores,scores.size());
    for (    Vector vOrdered : ordered) {
      if (logger.isDebugEnabled())       logger.debug("Item " + sortVectors.get(vOrdered) + " has score "+ scores.get(vOrdered));
      result.add(sortVectors.get(vOrdered));
    }
    for (    T seenItem : alreadySeen) {
      if (logger.isDebugEnabled())       logger.debug("Adding already seen item " + seenItem + " to end of list");
      result.add(seenItem);
    }
    for (    T notFoundItem : notFound) {
      if (logger.isDebugEnabled())       logger.debug("Adding not found item " + notFoundItem + " to end of list");
      result.add(notFoundItem);
    }
    return result;
  }
 else {
    logger.debug("No comparisons made so returning empty list");
    return new ArrayList<>();
  }
}

public < T extends Comparable < T > > List < T > sortDocsUsingDocQuery ( List < T > recentItems , List < T > sortItems , QueryTransform < T > docTransform , Set < T > exclusions ) { boolean useRank = false ; boolean bestScore = false ; boolean useThreshold = false ; double threshold = 0.999 ; List < T > result = new ArrayList < > ( ) ; Map < Vector , T > sortVectors = new HashMap < > ( ) ; Map < Vector , Double > scores = new HashMap < > ( ) ; boolean comparisonsMade = false ; boolean foundItemsToSort = false ; List < T > alreadySeen = new ArrayList < > ( ) ; List < T > notFound = new ArrayList < > ( ) ; for ( T item : sortItems ) { if ( ! recentItems . contains ( item ) && ! exclusions . contains ( item ) ) { Vector v = docVecReader . getVector ( docTransform . toSV ( item ) ) ; if ( v != null && ! v . isZeroVector ( ) ) { foundItemsToSort = true ; sortVectors . put ( v , item ) ; scores . put ( v , 0D ) ; } else { notFound . add ( item ) ; logger . warn ( "Can't find vector for sort item " + item ) ; } } else { if ( logger . isDebugEnabled ( ) ) logger . debug ( "Not sorting already seen article " + item ) ; alreadySeen . add ( item ) ; } } if ( ! foundItemsToSort ) { logger . debug ( "No sort items so returning empty list" ) ; return new ArrayList < > ( ) ; } for ( T recent : recentItems ) { if ( logger . isDebugEnabled ( ) ) logger . debug ( "Recent item " + recent ) ; String recentDoc = docTransform . toSV ( recent ) ; Vector vectorRecent = docVecReader . getVector ( recentDoc ) ; if ( vectorRecent != null && ! vectorRecent . isZeroVector ( ) ) { comparisonsMade = true ; if ( useRank ) { Map < Vector , Double > scoresLocal = new HashMap < > ( ) ; for ( Map . Entry < Vector , T > e : sortVectors . entrySet ( ) ) scoresLocal . put ( e . getKey ( ) , vectorRecent . measureOverlap ( e . getKey ( ) ) ) ; List < Vector > orderedLocal = CollectionTools . sortMapAndLimitToList ( scoresLocal , scoresLocal . size ( ) ) ; double count = 1 ; for ( Vector vOrdered : orderedLocal ) { scores . put ( vOrdered , scores . get ( vOrdered ) + count ) ; count ++ ; } } else { for ( Map . Entry < Vector , T > e : sortVectors . entrySet ( ) ) { double overlap = vectorRecent . measureOverlap ( e . getKey ( ) ) ; double current = scores . get ( e . getKey ( ) ) ; if ( ! Double . isNaN ( overlap ) ) { if ( logger . isDebugEnabled ( ) ) logger . debug ( "Overlap with " + e . getValue ( ) + " is " + overlap ) ; if ( bestScore ) { if ( overlap > current ) scores . put ( e . getKey ( ) , overlap ) ; } else { if ( useThreshold ) { if ( current < threshold && overlap > current ) scores . put ( e . getKey ( ) , overlap ) ; else if ( current > threshold && overlap > threshold ) scores . put ( e . getKey ( ) , overlap + current ) ; } else scores . put ( e . getKey ( ) , overlap + current ) ; } } } } } else logger . warn ( "Can't get vector for recent item " + recent ) ; } if ( comparisonsMade ) { List < Vector > ordered ; if ( useRank ) ordered = CollectionTools . sortMapAndLimitToList ( scores , scores . size ( ) , false ) ; else ordered = CollectionTools . sortMapAndLimitToList ( scores , scores . size ( ) ) ; for ( Vector vOrdered : ordered ) { if ( logger . isDebugEnabled ( ) ) logger . debug ( "Item " + sortVectors . get ( vOrdered ) + " has score " + scores . get ( vOrdered ) ) ; result . add ( sortVectors . get ( vOrdered ) ) ; } for ( T seenItem : alreadySeen ) { if ( logger . isDebugEnabled ( ) ) logger . debug ( "Adding already seen item " + seenItem + " to end of list" ) ; result . add ( seenItem ) ; } for ( T notFoundItem : notFound ) { if ( logger . isDebugEnabled ( ) ) logger . debug ( "Adding not found item " + notFoundItem + " to end of list" ) ; result . add ( notFoundItem ) ; } return result ; } else { logger . debug ( "No comparisons made so returning empty list" ) ; return new ArrayList < > ( ) ; } }
sort a set of items based on similarity with a list of items
