use of java.util.PriorityQueue in project aerosolve by airbnb.
the class AdditiveModel method debugScoreItem.
@Override
public float debugScoreItem(FeatureVector combinedItem, StringBuilder builder) {
Map<String, Map<String, Double>> flatFeatures = Util.flattenFeature(combinedItem);
float sum = 0.0f;
// order by the absolute value
PriorityQueue<Map.Entry<String, Float>> scores = new PriorityQueue<>(100, new LinearModel.EntryComparator());
for (Map.Entry<String, Map<String, Double>> featureFamily : flatFeatures.entrySet()) {
Map<String, Function> familyWeightMap = weights.get(featureFamily.getKey());
if (familyWeightMap == null)
continue;
for (Map.Entry<String, Double> feature : featureFamily.getValue().entrySet()) {
Function func = familyWeightMap.get(feature.getKey());
if (func == null)
continue;
float val = feature.getValue().floatValue();
float subScore = func.evaluate(val);
sum += subScore;
String str = featureFamily.getKey() + ":" + feature.getKey() + "=" + val + " = " + subScore + "<br>\n";
scores.add(new AbstractMap.SimpleEntry<>(str, subScore));
}
}
Map<String, List<Double>> denseFeatures = combinedItem.getDenseFeatures();
if (denseFeatures != null) {
assert (denseWeights != null);
for (Map.Entry<String, List<Double>> feature : denseFeatures.entrySet()) {
String featureName = feature.getKey();
Function fun = denseWeights.get(featureName);
float[] val = toFloat(feature.getValue());
float subScore = fun.evaluate(val);
sum += subScore;
String str = DENSE_FAMILY + ":" + featureName + "=" + val + " = " + subScore + "<br>\n";
scores.add(new AbstractMap.SimpleEntry<>(str, subScore));
}
}
final int MAX_COUNT = 100;
builder.append("Top scores ===>\n");
if (!scores.isEmpty()) {
int count = 0;
float subsum = 0.0f;
while (!scores.isEmpty()) {
Map.Entry<String, Float> entry = scores.poll();
builder.append(entry.getKey());
float val = entry.getValue();
subsum += val;
count = count + 1;
if (count >= MAX_COUNT) {
builder.append("Leftover = " + (sum - subsum) + '\n');
break;
}
}
}
builder.append("Total = " + sum + '\n');
return sum;
}
use of java.util.PriorityQueue in project Cloud9 by lintool.
the class SequentialPageRank method main.
@SuppressWarnings({ "static-access" })
public static void main(String[] args) throws IOException {
Options options = new Options();
options.addOption(OptionBuilder.withArgName("path").hasArg().withDescription("input path").create(INPUT));
options.addOption(OptionBuilder.withArgName("val").hasArg().withDescription("random jump factor").create(JUMP));
CommandLine cmdline = null;
CommandLineParser parser = new GnuParser();
try {
cmdline = parser.parse(options, args);
} catch (ParseException exp) {
System.err.println("Error parsing command line: " + exp.getMessage());
System.exit(-1);
}
if (!cmdline.hasOption(INPUT)) {
System.out.println("args: " + Arrays.toString(args));
HelpFormatter formatter = new HelpFormatter();
formatter.setWidth(120);
formatter.printHelp(SequentialPageRank.class.getName(), options);
ToolRunner.printGenericCommandUsage(System.out);
System.exit(-1);
}
String infile = cmdline.getOptionValue(INPUT);
float alpha = cmdline.hasOption(JUMP) ? Float.parseFloat(cmdline.getOptionValue(JUMP)) : 0.15f;
int edgeCnt = 0;
DirectedSparseGraph<String, Integer> graph = new DirectedSparseGraph<String, Integer>();
BufferedReader data = new BufferedReader(new InputStreamReader(new FileInputStream(infile)));
String line;
while ((line = data.readLine()) != null) {
line.trim();
String[] arr = line.split("\\t");
for (int i = 1; i < arr.length; i++) {
graph.addEdge(new Integer(edgeCnt++), arr[0], arr[i]);
}
}
data.close();
WeakComponentClusterer<String, Integer> clusterer = new WeakComponentClusterer<String, Integer>();
Set<Set<String>> components = clusterer.transform(graph);
int numComponents = components.size();
System.out.println("Number of components: " + numComponents);
System.out.println("Number of edges: " + graph.getEdgeCount());
System.out.println("Number of nodes: " + graph.getVertexCount());
System.out.println("Random jump factor: " + alpha);
// Compute PageRank.
PageRank<String, Integer> ranker = new PageRank<String, Integer>(graph, alpha);
ranker.evaluate();
// Use priority queue to sort vertices by PageRank values.
PriorityQueue<Ranking<String>> q = new PriorityQueue<Ranking<String>>();
int i = 0;
for (String pmid : graph.getVertices()) {
q.add(new Ranking<String>(i++, ranker.getVertexScore(pmid), pmid));
}
// Print PageRank values.
System.out.println("\nPageRank of nodes, in descending order:");
Ranking<String> r = null;
while ((r = q.poll()) != null) {
System.out.println(r.rankScore + "\t" + r.getRanked());
}
}
use of java.util.PriorityQueue in project CoreNLP by stanfordnlp.
the class ShiftReduceParserQuery method parseInternal.
private boolean parseInternal() {
final int maxBeamSize = Math.max(parser.op.testOptions().beamSize, 1);
success = true;
unparsable = false;
PriorityQueue<State> beam = new PriorityQueue<>(maxBeamSize + 1, ScoredComparator.ASCENDING_COMPARATOR);
beam.add(initialState);
// TODO: don't construct as many PriorityQueues
while (beam.size() > 0) {
if (Thread.interrupted()) {
// Allow interrupting the parser
throw new RuntimeInterruptedException();
}
// log.info("================================================");
// log.info("Current beam:");
// log.info(beam);
PriorityQueue<State> oldBeam = beam;
beam = new PriorityQueue<>(maxBeamSize + 1, ScoredComparator.ASCENDING_COMPARATOR);
State bestState = null;
for (State state : oldBeam) {
if (Thread.interrupted()) {
// Allow interrupting the parser
throw new RuntimeInterruptedException();
}
Collection<ScoredObject<Integer>> predictedTransitions = parser.model.findHighestScoringTransitions(state, true, maxBeamSize, constraints);
// log.info("Examining state: " + state);
for (ScoredObject<Integer> predictedTransition : predictedTransitions) {
Transition transition = parser.model.transitionIndex.get(predictedTransition.object());
State newState = transition.apply(state, predictedTransition.score());
// log.info(" Transition: " + transition + " (" + predictedTransition.score() + ")");
if (bestState == null || bestState.score() < newState.score()) {
bestState = newState;
}
beam.add(newState);
if (beam.size() > maxBeamSize) {
beam.poll();
}
}
}
if (beam.size() == 0) {
// will result in some sort of parse.
for (State state : oldBeam) {
Transition transition = parser.model.findEmergencyTransition(state, constraints);
if (transition != null) {
State newState = transition.apply(state);
if (bestState == null || bestState.score() < newState.score()) {
bestState = newState;
}
beam.add(newState);
}
}
}
// If the bestState is finished, we are done
if (bestState == null || bestState.isFinished()) {
break;
}
}
if (beam.size() == 0) {
success = false;
unparsable = true;
debinarized = null;
finalState = null;
bestParses = Collections.emptyList();
} else {
// TODO: filter out beam elements that aren't finished
bestParses = Generics.newArrayList(beam);
Collections.sort(bestParses, beam.comparator());
Collections.reverse(bestParses);
finalState = bestParses.get(0);
debinarized = debinarizer.transformTree(finalState.stack.peek());
debinarized = Tsurgeon.processPattern(rearrangeFinalPunctuationTregex, rearrangeFinalPunctuationTsurgeon, debinarized);
}
return success;
}
use of java.util.PriorityQueue in project CoreNLP by stanfordnlp.
the class TopNGramRecord method toString.
public String toString() {
StringBuilder result = new StringBuilder();
for (int prediction = 0; prediction < numClasses; ++prediction) {
result.append("Best scores for class " + prediction + "\n");
Map<Integer, PriorityQueue<Tree>> ngrams = classToNGrams.get(prediction);
for (Map.Entry<Integer, PriorityQueue<Tree>> entry : ngrams.entrySet()) {
List<Tree> trees = Generics.newArrayList(entry.getValue());
Collections.sort(trees, scoreComparator(prediction));
result.append(" Len " + entry.getKey() + "\n");
for (int i = trees.size() - 1; i >= 0; i--) {
Tree tree = trees.get(i);
result.append(" " + SentenceUtils.listToString(tree.yield()) + " [" + RNNCoreAnnotations.getPredictions(tree).get(prediction) + "]\n");
}
}
}
return result.toString();
}
use of java.util.PriorityQueue in project asterixdb by apache.
the class IndexingScheduler method scheduleLocalSlots.
/**
* Schedule data-local slots to each machine.
*
* @param splits
* The HDFS file splits.
* @param workloads
* The current capacity of each machine.
* @param locations
* The result schedule.
* @param slots
* The maximum slots of each machine.
* @param random
* The random generator.
* @param scheduled
* Indicate which slot is scheduled.
* @throws IOException
* @throws UnknownHostException
*/
private void scheduleLocalSlots(InputSplit[] splits, int[] workloads, String[] locations, int slots, Random random, boolean[] scheduled, final Map<String, IntWritable> locationToNumSplits, final HashMap<String, Integer> locationToNumOfAssignement) throws IOException, UnknownHostException {
/** scheduling candidates will be ordered inversely according to their popularity */
PriorityQueue<String> scheduleCadndiates = new PriorityQueue<String>(3, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
int assignmentDifference = locationToNumOfAssignement.get(s1).compareTo(locationToNumOfAssignement.get(s2));
if (assignmentDifference != 0) {
return assignmentDifference;
}
return locationToNumSplits.get(s1).compareTo(locationToNumSplits.get(s2));
}
});
for (int i = 0; i < splits.length; i++) {
if (scheduled[i]) {
continue;
}
/**
* get the location of all the splits
*/
String[] locs = splits[i].getLocations();
if (locs.length > 0) {
scheduleCadndiates.clear();
for (int j = 0; j < locs.length; j++) {
scheduleCadndiates.add(locs[j]);
}
for (String candidate : scheduleCadndiates) {
/**
* get all the IP addresses from the name
*/
InetAddress[] allIps = InetAddress.getAllByName(candidate);
/**
* iterate overa all ips
*/
for (InetAddress ip : allIps) {
/**
* if the node controller exists
*/
if (ipToNcMapping.get(ip.getHostAddress()) != null) {
/**
* set the ncs
*/
List<String> dataLocations = ipToNcMapping.get(ip.getHostAddress());
int arrayPos = random.nextInt(dataLocations.size());
String nc = dataLocations.get(arrayPos);
int pos = ncNameToIndex.get(nc);
/**
* check if the node is already full
*/
if (workloads[pos] < slots) {
locations[i] = nc;
workloads[pos]++;
scheduled[i] = true;
locationToNumOfAssignement.put(candidate, locationToNumOfAssignement.get(candidate) + 1);
break;
}
}
}
/**
* break the loop for data-locations if the schedule has
* already been found
*/
if (scheduled[i] == true) {
break;
}
}
}
}
}
Aggregations