How to use the heap.nlargest function in heap

To help you get started, we’ve selected a few heap examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github NUInnovation / auto-timeline / twitterFilter.js View on Github external
var tweetsPerSect = 5
	for (var sect in tweetsBySect){
		//Select the tweets within the sect
		var sectTweets = tweetsBySect[sect];
		// Setup a hastable and list for heap
		var hashtable = new HashTable();
		var scoresList = []

		for (var i = 0; i < sectTweets.length; i++) {
			var tweet = sectTweets[i];
			var score = evaluate(tweet);
			scoresList.push(score);
			hashtable.put(score, tweet);
		}
		//Get the largest few scores in the scoresList
		var largeScores = Heap.nlargest(unique(scoresList), tweetsPerSect);
		//Push those tweets into the final list
		for (var i=0; i < tweetsPerSect; i++){
			if (largeScores[i]) {
				finalList.push(hashtable.get(largeScores[i]))
			}
		    
		}
	}

	return finalList;	
}
github zjohn77 / retrieval / src / score_selection / lib / n_argmax.js View on Github external
module.exports = function(arr, n=10) {
    // Subset the reverse index so that both the largest elements
    // and the indices where they occur are returned.
    return _.pick(_.invertBy(arr),
                  Heap.nlargest(arr, n)
                 );
};
github nol13 / fuzzball.js / lite / fuzzball_lite_browser.js View on Github external
}
                if (isArray) idx = parseInt(c);
                else idx = c;
                if (result > options.cutoff) results.push([choices[c], result, idx]);
            }
            if (isArray && c < choices.length - 1) {
                setImmediate(function () { searchLoop(c + 1) })
            }
            else if (i < keys.length - 1) {
                setImmediate(function () { searchLoop(keys[i + 1], i + 1) });
            }
            else {
                if (anyblank) console.log("One or more choices were empty. (post-processing if applied)")
                if (options.limit && typeof options.limit === "number" && options.limit > 0 && options.limit < numchoices && !options.unsorted) {
                    var cmp = function (a, b) { return a[1] - b[1]; }
                    results = Heap.nlargest(results, options.limit, cmp);
                }
                else if (!options.unsorted) {
                    results = results.sort(function (a, b) { return b[1] - a[1]; });
                }
                callback(null, results);
            }
        }
    }
github nol13 / fuzzball.js / fuzzball.js View on Github external
}
            else {
                mychoice = pre_processor(options.processor(value), options);
                if (typeof mychoice !== "string" || mychoice.length === 0) anyblank = true;
                if (normalize && typeof mychoice === "string") mychoice = mychoice.normalize();
                result = options.scorer(query, mychoice, options);
            }
            if (result > options.cutoff) {
                if (options.returnObjects) results.push({choice: value, score: result, key: key});
                else results.push([value, result, key]);
            }
        });

        if (anyblank) if (typeof console !== undefined) console.log("One or more choices were empty. (post-processing if applied)")
        if (options.limit && typeof options.limit === "number" && options.limit > 0 && options.limit < numchoices && !options.unsorted) {
            results = Heap.nlargest(results, options.limit, cmpHeap);
        }
        else if (!options.unsorted) {
            results = results.sort(cmpSort);
        }
        return results;
    }
github nol13 / fuzzball.js / fuzzball_browser.js View on Github external
if (choices[c].tokens) options.tokens = [query_tokens, choices[c].tokens];
                else options.tokens = [query_tokens, tokenize(mychoice)]
                //query and mychoice only used for validation here
                var result = options.scorer(query, mychoice, options);
            }
            else {
                var mychoice = pre_processor(options.processor(choices[c]), options.force_ascii).valueOf();
                if (typeof mychoice !== "string" || (typeof mychoice === "string" && mychoice.length === 0)) anyblank = true;
                var result = options.scorer(query, mychoice, options);
            }
            if (result > options.cutoff) results.push([choices[c],result]);
        } 
        if(anyblank) console.log("One or more choices were empty. (post-processing if applied)")
        if (options.limit && typeof options.limit === "number" && options.limit > 0 && options.limit < choices.length) {
            var cmp = function(a, b) { return a[1] - b[1]; }
            results = Heap.nlargest(results, options.limit, cmp);
        }
        else {
            results = results.sort(function(a,b){return b[1]-a[1];});
        }
        return results;
    }
github nol13 / fuzzball.js / lite / fuzzball_lite.js View on Github external
callback(new Error("canceled"));
                return;
            }

            if (isArray && c < choices.length - 1) {
                if (c % loopOffset === 0) { setImmediate(function () { searchLoop(c + 1); }); }
                else searchLoop(c + 1);
            }
            else if (i < keys.length - 1) {
                if (i % loopOffset === 0) { setImmediate(function () { searchLoop(keys[i + 1], i + 1); }); }
                else { searchLoop(keys[i + 1], i + 1); }
            }
            else {
                if (anyblank) if (typeof console !== undefined) console.log("One or more choices were empty. (post-processing if applied)")
                if (options.limit && typeof options.limit === "number" && options.limit > 0 && options.limit < numchoices && !options.unsorted) {
                    results = Heap.nlargest(results, options.limit, cmpHeap);
                }
                else if (!options.unsorted) {
                    results = results.sort(cmpSort);
                }
                callback(null, results);
            }
        }
    }
github nol13 / fuzzball.js / fuzzball.js View on Github external
callback(new Error("canceled"));
                return;
            }

            if (isArray && c < choices.length - 1) {
                if (c % loopOffset === 0) { setImmediate(function () { searchLoop(c + 1) }); }
                else { searchLoop(c + 1); }
            }
            else if (i < keys.length - 1) {
                if (i % loopOffset === 0) {setImmediate(function () { searchLoop(keys[i + 1], i + 1); }); }
                else { searchLoop(keys[i + 1], i + 1); }
            }
            else {
                if (anyblank) if (typeof console !== undefined) console.log("One or more choices were empty. (post-processing if applied)")
                if (options.limit && typeof options.limit === "number" && options.limit > 0 && options.limit < numchoices && !options.unsorted) {
                    results = Heap.nlargest(results, options.limit, cmpHeap);
                }
                else if (!options.unsorted) {
                    results = results.sort(cmpSort);
                }
                callback(null, results);
            }
        }
    }
github nol13 / fuzzball.js / lite / fuzzball_lite.js View on Github external
mychoice = pre_processor(options.processor(choices[c]), options);
                    if (typeof mychoice !== "string" || mychoice.length === 0) anyblank = true;
                    if (normalize && typeof mychoice === "string") mychoice = mychoice.normalize();
                    result = options.scorer(query, mychoice, options);
                }
                if (isArray) idx = parseInt(c);
                else idx = c;
                if (result > options.cutoff) {
                    if (options.returnObjects) results.push({ choice: choices[c], score: result, key: idx });
                    else results.push([choices[c], result, idx]);;
                }
            }
        }
        if (anyblank) if (typeof console !== undefined) console.log("One or more choices were empty. (post-processing if applied)")
        if (options.limit && typeof options.limit === "number" && options.limit > 0 && options.limit < numchoices && !options.unsorted) {
            results = Heap.nlargest(results, options.limit, cmpHeap);
        }
        else if (!options.unsorted) {
            results = results.sort(cmpSort);
        }
        return results;
    }
github nol13 / fuzzball.js / lite / fuzzball_lite_browser.js View on Github external
}
                else {
                    mychoice = pre_processor(options.processor(choices[c]), options.force_ascii);
                    if (typeof mychoice !== "string" || mychoice.length === 0) anyblank = true;
                    if (normalize && typeof mychoice === "string") mychoice = mychoice.normalize();
                    result = options.scorer(query, mychoice, options);
                }
                if (isArray) idx = parseInt(c);
                else idx = c;
                if (result > options.cutoff) results.push([choices[c], result, idx]);
            }
        }
        if(anyblank) console.log("One or more choices were empty. (post-processing if applied)")
        if (options.limit && typeof options.limit === "number" && options.limit > 0 && options.limit < numchoices && !options.unsorted) {
            var cmp = function(a, b) { return a[1] - b[1]; }
            results = Heap.nlargest(results, options.limit, cmp);
        }
        else if (!options.unsorted) {
            results = results.sort(function(a,b){return b[1]-a[1];});
        }
        return results;
    }

heap

binary heap (priority queue) algorithms (ported from Python's heapq module)

MIT
Latest version published 3 years ago

Package Health Score

67 / 100
Full package analysis