com.google.common.collect.MinMaxPriorityQueue Java Examples

The following examples show how to use com.google.common.collect.MinMaxPriorityQueue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FastText.java    From fastText4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void predict(List<Integer> words, List<FastTextPrediction> predictions, int k, float threshold) {
  if (!words.isEmpty()) {
    Vector hidden = new Vector(args.getDimension());
    Vector output = new Vector(dict.nLabels());
    MinMaxPriorityQueue<Pair<Float, Integer>> modelPredictions = MinMaxPriorityQueue
      .orderedBy(new Model.HeapComparator<Integer>())
      .expectedSize(dict.nLabels())
      .create();
    int[] input = Ints.toArray(words);
    if (words.isEmpty()) {
      return;
    }
    model.predict(input, k, threshold, modelPredictions, hidden, output);
    while (!modelPredictions.isEmpty()) {
      Pair<Float, Integer> pred = modelPredictions.pollFirst();
      predictions.add(new FastTextPrediction(dict.getLabel(pred.last()), pred.first()));
    }
  }
}
 
Example #2
Source File: MaximizeWebsitesLinkSelector.java    From ache with Apache License 2.0 6 votes vote down vote up
@Override
public List<LinkRelevance> getSelectedLinks() {
    List<LinkRelevance> links = new ArrayList<>();
    while (links.size() < numberOfLinks && !topkLinksPerDomain.isEmpty()) {
        // adds the URL with max score of each domain
        MinMaxPriorityQueue<LinkRelevance> topk = newPriorityQueue(numberOfLinks);
        Iterator<Entry<String, MinMaxPriorityQueue<LinkRelevance>>> it = topkLinksPerDomain.entrySet().iterator();
        while (it.hasNext()) {
            MinMaxPriorityQueue<LinkRelevance> domain = it.next().getValue();
            topk.add(domain.poll());
            if (domain.isEmpty()) {
                it.remove();
            }
        }
        for(LinkRelevance link : topk) {
            links.add(link);
        }
    }
    this.topkLinksPerDomain = null; // clean-up reference
    return links;
}
 
Example #3
Source File: Model.java    From fastText4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void findKBest(int k,
                      float threshold,
                      MinMaxPriorityQueue<Pair<Float, Integer>> heap,
                      Vector hidden,
                      Vector output) {
  computeOutputSoftmax(hidden, output);
  for (int i = 0; i < osz; i++) {
    if (output.data[i] < threshold) continue;
    if (heap.size() == k && stdLog(output.data[i]) < heap.peekFirst().first()) {
      continue;
    }
    heap.add(new Pair<>(stdLog(output.data[i]), i));
  }
  while (heap.size() > k) {
    heap.pollLast();
  }
}
 
Example #4
Source File: MappedByteBufferSortedQueue.java    From phoenix with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public ResultEntry peek() {
    if (mergedQueue == null) {
        mergedQueue = MinMaxPriorityQueue.<ResultEntry> orderedBy(
                comparator).maximumSize(queues.size()).create();
        for (MappedByteBufferPriorityQueue queue : queues) {
            try {
                IndexedResultEntry next = queue.getNextResult();
                if (next != null) {
                    mergedQueue.add(next);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    if (!mergedQueue.isEmpty()) {
        IndexedResultEntry re = mergedQueue.peekFirst();
        if (re != null) {
            return re;
        }
    }
    return null;
}
 
Example #5
Source File: DimensionValueMetricPairTest.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
@Test
public void comparatorTest() throws Exception {

  MinMaxPriorityQueue<DimensionValueMetricPair> testQueue = MinMaxPriorityQueue.maximumSize(2).create();

  DimensionValueMetricPair d1 = new DimensionValueMetricPair("d1", 1);
  DimensionValueMetricPair d2 = new DimensionValueMetricPair("d2", 2);
  DimensionValueMetricPair d3 = new DimensionValueMetricPair(30, 3);
  DimensionValueMetricPair d4 = new DimensionValueMetricPair("d4", 4);

  testQueue.add(d1);
  testQueue.add(d2);
  testQueue.add(d3);
  testQueue.add(d4);

  for (DimensionValueMetricPair pair : testQueue) {
    Assert.assertEquals(pair.getMetricValue().intValue() > 2, true,
        "Incorrect comparator for DimensionValueMetricPair, queue must retain highest metric values");
  }

}
 
Example #6
Source File: CachedPostingListCounter.java    From vespa with Apache License 2.0 6 votes vote down vote up
public CachedPostingListCounter rebuildCache() {
    MinMaxPriorityQueue<Entry> mostExpensive = MinMaxPriorityQueue
            .maximumSize(32).expectedSize(32).create();
    synchronized (this) {
        for (ObjectLongPair<int[]> p : frequency.keyValuesView()) {
            mostExpensive.add(new Entry(p.getOne(), p.getTwo()));
        }
    }
    ObjectIntHashMap<int[]> postingListMapping = new ObjectIntHashMap<>();
    int[] bitVector = new int[nDocuments];
    int length = mostExpensive.size();
    for (int i = 0; i < length; i++) {
        Entry e = mostExpensive.removeFirst();
        int[] docIds = e.docIds;
        postingListMapping.put(docIds, i);
        for (int docId : docIds) {
            bitVector[docId] |= (1 << i);
        }
    }
    return new CachedPostingListCounter(postingListMapping, bitVector);
}
 
Example #7
Source File: PMetaDataImpl.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Used when the cache is growing past its max size to clone in a single pass.
 * Removes least recently used tables to get size of cache below its max size by
 * the overage amount.
 */
public PTableCache cloneMinusOverage(long overage) {
    assert(overage > 0);
    int nToRemove = Math.max(MIN_REMOVAL_SIZE, (int)Math.ceil((currentByteSize-maxByteSize) / ((double)currentByteSize / size())) + 1);
    MinMaxPriorityQueue<PTableRef> toRemove = BUILDER.expectedSize(nToRemove).create();
    PTableCache newCache = new PTableCache(this.size(), this.maxByteSize, this.timeKeeper);
    
    long toRemoveBytes = 0;
    // Add to new cache, but track references to remove when done
    // to bring cache at least overage amount below it's max size.
    for (PTableRef tableRef : tables.values()) {
        newCache.put(tableRef.table.getKey(), new PTableRef(tableRef));
        toRemove.add(tableRef);
        toRemoveBytes += tableRef.estSize;
        if (toRemoveBytes - toRemove.peekLast().estSize > overage) {
            PTableRef removedRef = toRemove.removeLast();
            toRemoveBytes -= removedRef.estSize;
        }
    }
    for (PTableRef toRemoveRef : toRemove) {
        newCache.remove(toRemoveRef.table.getKey());
    }
    return newCache;
}
 
Example #8
Source File: PMetaDataCache.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Used when the cache is growing past its max size to clone in a single pass.
 * Removes least recently used tables to get size of cache below its max size by
 * the overage amount.
 */
public PMetaDataCache cloneMinusOverage(long overage) {
    assert(overage > 0);
    int nToRemove = Math.max(MIN_REMOVAL_SIZE, (int)Math.ceil((currentByteSize-maxByteSize) / ((double)currentByteSize / size())) + 1);
    MinMaxPriorityQueue<PTableRef> toRemove = BUILDER.expectedSize(nToRemove).create();
    PMetaDataCache newCache = new PMetaDataCache(this.size(), this.maxByteSize, this.timeKeeper, this.tableRefFactory);
    
    long toRemoveBytes = 0;
    // Add to new cache, but track references to remove when done
    // to bring cache at least overage amount below it's max size.
    for (PTableRef tableRef : this.tables.values()) {
        newCache.put(tableRef.getTable().getKey(), tableRefFactory.makePTableRef(tableRef));
        toRemove.add(tableRef);
        toRemoveBytes += tableRef.getEstimatedSize();
        while (toRemoveBytes - toRemove.peekLast().getEstimatedSize() >= overage) {
            PTableRef removedRef = toRemove.removeLast();
            toRemoveBytes -= removedRef.getEstimatedSize();
        }
    }
    for (PTableRef toRemoveRef : toRemove) {
        newCache.remove(toRemoveRef.getTable().getKey());
    }
    return newCache;
}
 
Example #9
Source File: MaximizeWebsitesLinkSelector.java    From ache with Apache License 2.0 5 votes vote down vote up
@Override
public void evaluateLink(LinkRelevance link) {
    if (link.getRelevance() > minRelevance) {
        String domainName = link.getTopLevelDomainName();
        MinMaxPriorityQueue<LinkRelevance> domainQueue = topkLinksPerDomain.get(domainName);
        if (domainQueue == null) {
            domainQueue = newPriorityQueue(MAX_LINKS_PER_DOMAIN);
            topkLinksPerDomain.put(domainName, domainQueue);
        }
        domainQueue.add(link);
    }
}
 
Example #10
Source File: MinMaxPriorityQueueUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenMinMaxPriorityQueue_whenAddElementToFull_thenShouldEvictGreatestItem() {
    //given
    MinMaxPriorityQueue<CustomClass> queue = MinMaxPriorityQueue
            .orderedBy(Comparator.comparing(CustomClass::getValue))
            .maximumSize(10)
            .create();

    //when
    IntStream
            .iterate(10, i -> i - 1)
            .limit(10)
            .forEach(i -> queue.add(new CustomClass(i)));

    //then
    assertThat(queue.peekFirst().getValue()).isEqualTo(1);
    assertThat(queue.peekLast().getValue()).isEqualTo(10);


    //and
    queue.add(new CustomClass(-1));


    //then
    assertThat(queue.peekFirst().getValue()).isEqualTo(-1);
    assertThat(queue.peekLast().getValue()).isEqualTo(9);

    //and
    queue.add(new CustomClass(100));
    assertThat(queue.peekFirst().getValue()).isEqualTo(-1);
    assertThat(queue.peekLast().getValue()).isEqualTo(9);

}
 
Example #11
Source File: BilingualAlignmentService.java    From termsuite-core with Apache License 2.0 5 votes vote down vote up
public List<TranslationCandidate> alignDistributional(TermService sourceTerm, int nbCandidates,
		int minCandidateFrequency) {
	Queue<TranslationCandidate> alignedCandidateQueue = MinMaxPriorityQueue.maximumSize(nbCandidates).create();
	ContextVector sourceVector = sourceTerm.getContext();
	if(sourceVector == null)
		return new ArrayList<>();
	ContextVector translatedSourceVector = translateVector(
			sourceVector,
			dico,
			TRANSLATION_STRATEGY_MOST_SPECIFIC,
			targetTermino);
	ExplainedValue v;
	int nbVectorsNotComputed = 0;
	int nbVectorsComputed = 0;
	for(TermService targetTerm:targetTermino.terms().filter(TermService::isSingleWord).collect(Collectors.toList())) {
		if(targetTerm.getFrequency() < minCandidateFrequency)
			continue;
		if(targetTerm.getContext() != null) {
			nbVectorsComputed++;
			v = distance.getExplainedValue(translatedSourceVector, targetTerm.getContext());
			TranslationCandidate candidate = new TranslationCandidate(
					AlignmentMethod.DISTRIBUTIONAL,
					targetTerm, 
					v.getValue(), 
					sourceTerm,
					v.getExplanation());
			alignedCandidateQueue.add(candidate);
		}
	};
	if(nbVectorsNotComputed > 0) {
		LOGGER.warn(MSG_SEVERAL_VECTORS_NOT_COMPUTED, nbVectorsComputed, nbVectorsNotComputed);	
	}
	
	// sort alignedCandidates
	List<TranslationCandidate> alignedCandidates = Lists.newArrayListWithCapacity(alignedCandidateQueue.size());
	alignedCandidates.addAll(alignedCandidateQueue);
	normalizeCandidateScores(alignedCandidates);
	return Lists.newArrayList(alignedCandidateQueue);
}
 
Example #12
Source File: PhoenixQueues.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public static SizeAwareQueue<ResultEntry> newSizeBoundResultEntrySortedQueue(
        Comparator<ResultEntry> comparator, Integer limit, long maxSizeBytes) {
    limit = limit == null ? -1 : limit;
    MinMaxPriorityQueue<ResultEntry> queue =
            limit < 0 ? MinMaxPriorityQueue.<ResultEntry> orderedBy(comparator).create()
                    : MinMaxPriorityQueue.<ResultEntry> orderedBy(comparator).maximumSize(limit)
                            .create();
    return new SizeBoundQueue<ResultEntry>(maxSizeBytes, queue) {
        @Override
        public long sizeOf(org.apache.phoenix.iterate.OrderedResultIterator.ResultEntry e) {
            return ResultEntry.sizeOf(e);
        }

    };
}
 
Example #13
Source File: BufferedQueue.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void initMergedQueue() {
    if (mergedQueue == null && currentIndex >= 0) {
        mergedQueue = MinMaxPriorityQueue.<BufferedSegmentQueue<T>> orderedBy(
                getSegmentQueueComparator()).maximumSize(currentIndex + 1).create();
        for (BufferedSegmentQueue<T> queue : getSegmentQueues()) {
            T re = queue.peek();
            if (re != null) {
                mergedQueue.add(queue);
            }
        }
    }        
}
 
Example #14
Source File: BufferedSortedQueue.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public BufferedResultEntryPriorityQueue(int index,
        long thresholdBytes, int limit, Comparator<ResultEntry> comparator) {
    super(index, thresholdBytes, limit >= 0);
    this.results = limit < 0 ? 
            MinMaxPriorityQueue.<ResultEntry> orderedBy(comparator).create()
          : MinMaxPriorityQueue.<ResultEntry> orderedBy(comparator).maximumSize(limit).create();
}
 
Example #15
Source File: TopKPhaseJob.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
@Override
protected void cleanup(Context context) throws IOException, InterruptedException {

  for (String dimension : dimensionNames) {

    LOGGER.info("{} records passed metric threshold for dimension {}", thresholdPassCount.get(dimension), dimension);

    // Get top k
    TopKDimensionToMetricsSpec topkSpec = topKDimensionToMetricsSpecMap.get(dimension);
    if (topkSpec != null && topkSpec.getDimensionName() != null && topkSpec.getTopk() != null) {

      // Get top k for each metric specified
      Map<String, Integer> topkMetricsMap = topkSpec.getTopk();
      for (Entry<String, Integer> topKEntry : topkMetricsMap.entrySet()) {

        String metric = topKEntry.getKey();
        int k = topKEntry.getValue();
        MinMaxPriorityQueue<DimensionValueMetricPair> topKQueue = MinMaxPriorityQueue.maximumSize(k).create();

        Map<Object, Number[]> dimensionToMetricsMap = dimensionNameToValuesMap.get(dimension);
        for (Entry<Object, Number[]> entry : dimensionToMetricsMap.entrySet()) {
          topKQueue.add(new DimensionValueMetricPair(entry.getKey(), entry.getValue()[metricToIndexMapping.get(metric)]));
        }
        LOGGER.info("Picking Top {} values for {} based on Metric {} : {}", k, dimension, metric, topKQueue);
        for (DimensionValueMetricPair pair : topKQueue) {
          topkDimensionValues.addValue(dimension, String.valueOf(pair.getDimensionValue()));
        }
      }
    }
  }

  if (topkDimensionValues.getTopKDimensions().size() > 0) {
    String topkValuesPath = configuration.get(TOPK_PHASE_OUTPUT_PATH.toString());
    LOGGER.info("Writing top k values to {}",topkValuesPath);
    FSDataOutputStream topKDimensionValuesOutputStream = fileSystem.create(
        new Path(topkValuesPath + File.separator + ThirdEyeConstants.TOPK_VALUES_FILE));
    OBJECT_MAPPER.writeValue((DataOutput) topKDimensionValuesOutputStream, topkDimensionValues);
    topKDimensionValuesOutputStream.close();
  }
}
 
Example #16
Source File: MappedByteBufferSortedQueue.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public MappedByteBufferPriorityQueue(int index, int limit, int thresholdBytes,
        Comparator<ResultEntry> comparator) throws IOException {
    this.index = index;
    this.limit = limit;
    this.thresholdBytes = thresholdBytes;
    results = limit < 0 ? 
            MinMaxPriorityQueue.<ResultEntry> orderedBy(comparator).create()
          : MinMaxPriorityQueue.<ResultEntry> orderedBy(comparator).maximumSize(limit).create();
}
 
Example #17
Source File: IntervalRegistryLogger.java    From IOT-Technical-Guide with Apache License 2.0 5 votes vote down vote up
protected Map<String, Long> getTopElements(Map<String, Long> countMap) {
    MinMaxPriorityQueue<Map.Entry<String, Long>> topQueue = MinMaxPriorityQueue
            .orderedBy(Comparator.comparing((Function<Map.Entry<String, Long>, Long>) Map.Entry::getValue).reversed())
            .maximumSize(topSize)
            .create(countMap.entrySet());

    return topQueue.stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
 
Example #18
Source File: RandomLinkSelector.java    From ache with Apache License 2.0 5 votes vote down vote up
@Override
public void startSelection(int numberOfLinks) {
    links = MinMaxPriorityQueue
        .orderedBy(new Comparator<RandomLink>() {
            @Override
            public int compare(RandomLink o1, RandomLink o2) {
                return Double.compare(o1.relevance, o2.relevance);
            }
        })
        .maximumSize(numberOfLinks) // keep only top-k items
        .create();
}
 
Example #19
Source File: TopkLinkSelector.java    From ache with Apache License 2.0 5 votes vote down vote up
@Override
public void startSelection(int numberOfLinks) {
    this.topkLinks = MinMaxPriorityQueue
            .orderedBy(LinkRelevance.DESC_ORDER_COMPARATOR)
            .maximumSize(numberOfLinks) // keep only top-k items
            .create();
}
 
Example #20
Source File: MappedByteBufferQueue.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void initMergedQueue() {
    if (mergedQueue == null && currentIndex >= 0) {
        mergedQueue = MinMaxPriorityQueue.<MappedByteBufferSegmentQueue<T>> orderedBy(
                getSegmentQueueComparator()).maximumSize(currentIndex + 1).create();
        for (MappedByteBufferSegmentQueue<T> queue : getSegmentQueues()) {
            T re = queue.peek();
            if (re != null) {
                mergedQueue.add(queue);
            }
        }
    }        
}
 
Example #21
Source File: MappedByteBufferSortedQueue.java    From phoenix with Apache License 2.0 5 votes vote down vote up
public MappedByteBufferResultEntryPriorityQueue(int index,
        int thresholdBytes, int limit, Comparator<ResultEntry> comparator) {
    super(index, thresholdBytes, limit >= 0);
    this.results = limit < 0 ? 
            MinMaxPriorityQueue.<ResultEntry> orderedBy(comparator).create()
          : MinMaxPriorityQueue.<ResultEntry> orderedBy(comparator).maximumSize(limit).create();
}
 
Example #22
Source File: GoogleCloudStorageSource.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Override
protected List<ConfigIssue> init() {
  // Validate configuration values and open any required resources.
  List<ConfigIssue> issues = gcsOriginConfig.init(getContext(), super.init());
  minMaxPriorityQueue = MinMaxPriorityQueue.orderedBy((Blob o1, Blob o2) -> {
    int result = o1.getUpdateTime().compareTo(o2.getUpdateTime());
    if(result != 0) {
      return result;
    }
    //same modified time. Use generatedid (bucket/blob name/timestamp) to sort
    return o1.getGeneratedId().compareTo(o2.getGeneratedId());
  }).maximumSize(gcsOriginConfig.maxResultQueueSize).create();
  antPathMatcher = new AntPathMatcher();

  gcsOriginConfig.credentials.getCredentialsProvider(getContext(), issues)
      .ifPresent(p -> credentialsProvider = p);

  try {
    storage = StorageOptions.newBuilder()
        .setCredentials(credentialsProvider.getCredentials())
        .build()
        .getService();
  } catch (IOException e) {
    LOG.error("Error when initializing storage. Reason : {}", e);
    issues.add(getContext().createConfigIssue(
        Groups.CREDENTIALS.name(),
        "gcsOriginConfig.credentials.credentialsProvider",
        Errors.GCS_01,
        e
    ));
  }

  rateLimitElEval = FileRefUtil.createElEvalForRateLimit(getContext());
  rateLimitElVars = getContext().createELVars();
  errorBlobHandler = new GcsObjectPostProcessingHandler(storage, gcsOriginConfig.gcsOriginErrorConfig);
  return issues;
}
 
Example #23
Source File: MonoReilSolver.java    From binnavi with Apache License 2.0 5 votes vote down vote up
public MonoReilSolver(final IInstructionGraph instructionGraph,
    final AnalysisDirection analysisDirection, final ILattice<LatticeElementType> lattice) {
  m_graph = Preconditions.checkNotNull(instructionGraph,
      "Error: instruction graph argument can not be null");
  m_direction = Preconditions.checkNotNull(analysisDirection,
      "Error: analysis direction argument can not be null");
  m_lattice = Preconditions.checkNotNull(lattice, "Error: latice argument can not be null");

  m_workList = MinMaxPriorityQueue.expectedSize(m_graph.size()).create();
}
 
Example #24
Source File: IntervalRegistryLogger.java    From Groza with Apache License 2.0 5 votes vote down vote up
protected Map<String, Long> getTopElements(Map<String, Long> countMap) {
    MinMaxPriorityQueue<Map.Entry<String, Long>> topQueue = MinMaxPriorityQueue
            .orderedBy(Comparator.comparing((Function<Map.Entry<String, Long>, Long>) Map.Entry::getValue).reversed())
            .maximumSize(topSize)
            .create(countMap.entrySet());

    return topQueue.stream().collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
 
Example #25
Source File: Model.java    From fastText4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void dfs(int k,
                float threshold,
                int node,
                float score,
                MinMaxPriorityQueue<Pair<Float, Integer>> heap,
                Vector hidden) {
  if (score < stdLog(threshold)) return;
  if (heap.size() == k && score < heap.peekLast().first()) {
    return;
  }
  if (tree[node].left == -1 && tree[node].right == -1) {
    heap.add(new Pair<>(score, node));
    if (heap.size() > k) {
      Pair<Float, Integer> p = heap.pollLast();
    }
    return;
  }
  float f;
  if (quant && args.getQOut()) {
    f = qwo.dotRow(hidden, node - osz);
  } else {
    f = wo.dotRow(hidden, node - osz);
  }
  f = 1f / (1f + (float) Math.exp(-f));
  dfs(k, threshold, tree[node].left, score + stdLog(1.0f - f), heap, hidden);
  dfs(k, threshold, tree[node].right, score + stdLog(f), heap, hidden);
}
 
Example #26
Source File: Model.java    From fastText4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void predict(int[] input,
                    int k,
                    float threshold,
                    MinMaxPriorityQueue<Pair<Float, Integer>> heap,
                    Vector hidden,
                    Vector output) {
  Preconditions.checkArgument(k > 0);
  computeHidden(input, hidden);
  if (args.getLoss().equals(Args.LossName.HS)) {
    dfs(k, threshold, 2 * osz - 2, 0.0f, heap, hidden);
  } else {
    findKBest(k, threshold, heap, hidden, output);
  }
}
 
Example #27
Source File: MaximizeWebsitesLinkSelector.java    From ache with Apache License 2.0 4 votes vote down vote up
private MinMaxPriorityQueue<LinkRelevance> newPriorityQueue(int maxSize) {
    return MinMaxPriorityQueue
            .orderedBy(LinkRelevance.DESC_ORDER_COMPARATOR)
            .maximumSize(maxSize)
            .create();
}
 
Example #28
Source File: InvertedIndex.java    From SimpleTextSearch with MIT License 4 votes vote down vote up
@Override
public SearchResultBatch search(String searchTerm, int maxResults) {
    Stopwatch stopwatch = Stopwatch.createStarted();

    ParsedDocument searchDocument = searchTermParser.parseDocument(new Document(searchTerm, new Object()));
    Set<ParsedDocument> documentsToScanSet = getRelevantDocuments(searchDocument);

    if (searchDocument.isEmpty() || documentsToScanSet.isEmpty()) {
        return buildResultBatch(new ArrayList<SearchResult>(), stopwatch, 0);
    }

    // do scan
    final Collection<SearchResult> resultsP = new ConcurrentLinkedQueue<>();

    List<ParsedDocument> documentsToScan = new ArrayList<>(documentsToScanSet);
    final ParsedDocumentMetrics pdm = new ParsedDocumentMetrics(corpus, searchDocument, termToPostings);
    List<Future> futures = new ArrayList<>();

    for (final List<ParsedDocument> partition : Lists.partition(documentsToScan, THREAD_POOL_SIZE)) {

        Future future = executorService.submit(new Runnable() {
            @Override
            public void run() {
                for (ParsedDocument doc : partition) {
                    double cosine = computeCosine(pdm, doc);

                    SearchResult result = new SearchResult();
                    result.setRelevanceScore(cosine);
                    result.setUniqueIdentifier(doc.getUniqueId());
                    resultsP.add(result);
                }
            }
        });

        futures.add(future);
    }

    for (Future f : futures) {
        try {
            f.get();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    int heapSize = Math.min(resultsP.size(), maxResults);

    MinMaxPriorityQueue<SearchResult> maxHeap = MinMaxPriorityQueue.
            orderedBy(new Comparator<SearchResult>() {
                @Override
                public int compare(SearchResult o1, SearchResult o2) {
                    if (o1.getRelevanceScore() <= o2.getRelevanceScore()) {
                        return 1;
                    } else {
                        return -1;
                    }
                }
            }).
            maximumSize(heapSize).
            expectedSize(heapSize).create(resultsP);


    // return results
    ArrayList<SearchResult> r = new ArrayList<>();
    while (!maxHeap.isEmpty()) {
        SearchResult rs = maxHeap.removeFirst();
        r.add(rs);
    }

    return buildResultBatch(r, stopwatch, documentsToScan.size());
}
 
Example #29
Source File: ChartCell.java    From EasySRL with Apache License 2.0 4 votes vote down vote up
CellNoDynamicProgram(int nbest) {
	this.entries = MinMaxPriorityQueue.maximumSize(nbest).create();
}
 
Example #30
Source File: Model.java    From fastText4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void predict(int[] input, int k, float threshold, MinMaxPriorityQueue<Pair<Float, Integer>> heap) {
  predict(input, k, threshold, heap, hidden, output);
}