/** * {@inheritDoc} * * @deprecated Please use the corresponding type-specific method instead. */ @Deprecated @Override default Double getValue() { return Double.valueOf(getDoubleValue()); } /**
@SuppressWarnings("unchecked") @Override public boolean equals(final Object o) { if (!(o instanceof Map.Entry)) return false; if (o instanceof Object2DoubleMap.Entry) { final Object2DoubleMap.Entry<K> e = (Object2DoubleMap.Entry<K>) o; return java.util.Objects.equals(key, e.getKey()) && (Double.doubleToLongBits(value) == Double.doubleToLongBits(e.getDoubleValue())); } final Map.Entry<?, ?> e = (Map.Entry<?, ?>) o; final Object key = e.getKey(); final Object value = e.getValue(); if (value == null || !(value instanceof Double)) return false; return java.util.Objects.equals(this.key, (key)) && (Double.doubleToLongBits(this.value) == Double .doubleToLongBits(((Double) (value)).doubleValue())); } @Override
@Override public String toString() { final StringBuilder s = new StringBuilder(); final ObjectIterator<Object2DoubleMap.Entry<K>> i = Object2DoubleMaps.fastIterator(this); int n = size(); Object2DoubleMap.Entry<K> e; boolean first = true; s.append("{"); while (n-- != 0) { if (first) first = false; else s.append(", "); e = i.next(); if (this == e.getKey()) s.append("(this map)"); else s.append(String.valueOf(e.getKey())); s.append("=>"); s.append(String.valueOf(e.getDoubleValue())); } s.append("}"); return s.toString(); } }
public static void main(String[] args) throws Exception { final CLIOpts opts = new CLIOpts(args); final SourceType sourceType = opts.enumOptional("t", SourceType.class, SourceType.FIRST, "The corpus type"); final File refFile = opts.roFile("reference", "The reference ontology"); final File corpusFile = opts.roFile("corpus", "The corpus file"); final File wordMapFile = opts.roFile("wordMap", "The word map"); final int N = opts.intValue("N", "The maximal n-gram to consider"); final double thresh = opts.doubleValue("threshold", "The threshold of salience to filter at"); final File outFile = opts.woFile("out", "The file to write the salient n-gram list to"); if(!opts.verify(MostSalient.class)) { return; } final int W = WordMap.calcW(wordMapFile); final String[] wordMap = WordMap.inverseFromFile(wordMapFile, W, true); final Object2DoubleMap<NGram> salientNGrams = mostSalientNGrams(refFile, corpusFile, N, sourceType); final DataOutputStream out = new DataOutputStream(CLIOpts.openOutputAsMaybeZipped(outFile)); for(Object2DoubleMap.Entry<NGram> e : salientNGrams.object2DoubleEntrySet()) { if(e.getDoubleValue() > thresh) { final NGram ng = e.getKey(); out.writeInt(ng.ngram.length); for(int i = 0; i < ng.ngram.length; i++) { out.writeInt(ng.ngram[i]); } out.writeDouble(e.getDoubleValue()); } } out.flush(); out.close(); } }
public static Object2DoubleMap<NGram> mostSalientNGrams(final File reference, final File corpus, int N, SourceType sourceType) throws IOException { final Object2DoubleMap<NGram> referenceCounts = countInReference(reference, N); final Object2DoubleMap<NGram> corpusCounts = countInCorpus(corpus, N, referenceCounts.keySet(), sourceType); final Object2DoubleMap<NGram> salience = new Object2DoubleRBTreeMap<NGram>(); for (Object2DoubleMap.Entry<NGram> e : referenceCounts.object2DoubleEntrySet()) { if (corpusCounts.containsKey(e.getKey())) { salience.put(e.getKey(), e.getDoubleValue()/corpusCounts.getDouble(e.getKey())); } } referenceCounts.clear(); corpusCounts.clear(); final Object2DoubleMap<NGram> rankedSalience = new Object2DoubleRBTreeMap<NGram>(new Comparator<NGram>() { @Override public int compare(NGram o1, NGram o2) { final double salience1 = salience.getDouble(o1); final double salience2 = salience.getDouble(o2); if(salience1 < salience2) { return +1; } else if(salience1 > salience2) { return -1; } else { return o1.compareTo(o2); } } }); rankedSalience.putAll(salience); return rankedSalience; }
@Nullable public static <T> T getWeightedRandom(Random random, Object2DoubleMap<T> choices) { double i = 0; DoubleCollection doubles = choices.values(); for (DoubleIterator iterator = doubles.iterator(); iterator.hasNext(); ) { double x = iterator.nextDouble(); i += x; } i = getRandomDouble(random, 0, i); for (Object2DoubleMap.Entry<T> entry : choices.object2DoubleEntrySet()) { i -= entry.getDoubleValue(); if (i < 0) { return entry.getKey(); } } return null; }
/** {@inheritDoc} */ @SuppressWarnings({"unchecked", "deprecation"}) @Override public void putAll(final Map<? extends K, ? extends Double> m) { if (m instanceof Object2DoubleMap) { ObjectIterator<Object2DoubleMap.Entry<K>> i = Object2DoubleMaps.fastIterator((Object2DoubleMap<K>) m); while (i.hasNext()) { final Object2DoubleMap.Entry<? extends K> e = i.next(); put(e.getKey(), e.getDoubleValue()); } } else { int n = m.size(); final Iterator<? extends Map.Entry<? extends K, ? extends Double>> i = m.entrySet().iterator(); Map.Entry<? extends K, ? extends Double> e; while (n-- != 0) { e = i.next(); put(e.getKey(), e.getValue()); } } } /**
@SuppressWarnings("unchecked") @Override public boolean contains(final Object o) { if (!(o instanceof Map.Entry)) return false; if (o instanceof Object2DoubleMap.Entry) { final Object2DoubleMap.Entry<K> e = (Object2DoubleMap.Entry<K>) o; final K k = e.getKey(); return map.containsKey(k) && (Double.doubleToLongBits(map.getDouble(k)) == Double.doubleToLongBits(e.getDoubleValue())); } final Map.Entry<?, ?> e = (Map.Entry<?, ?>) o; final Object k = e.getKey(); final Object value = e.getValue(); if (value == null || !(value instanceof Double)) return false; return map.containsKey(k) && (Double.doubleToLongBits(map.getDouble(k)) == Double .doubleToLongBits(((Double) (value)).doubleValue())); } @SuppressWarnings("unchecked")
/** * Calculates the norm (sum of relevance scores) of each item. * * @param <U> type of the users * @param <I> type of the items * @param recommendations set of recommendations used to calculate the norm * @return item-norm map */ public static <U, I> Object2DoubleMap<I> calculateNorm(Stream<Recommendation<U, I>> recommendations) { return recommendations.parallel() .flatMap(recommendation -> recommendation.getItems().stream()) .collect( Object2DoubleOpenHashMap::new, (m, iv) -> m.addTo(iv.v1, iv.v2), (m1, m2) -> m2.object2DoubleEntrySet().forEach(e -> m1.addTo(e.getKey(), e.getDoubleValue())) ); }
@SuppressWarnings("unchecked") @Override public boolean remove(final Object o) { if (!(o instanceof Map.Entry)) return false; if (o instanceof Object2DoubleMap.Entry) { final Object2DoubleMap.Entry<K> e = (Object2DoubleMap.Entry<K>) o; return map.remove(e.getKey(), e.getDoubleValue()); } Map.Entry<?, ?> e = (Map.Entry<?, ?>) o; final Object k = e.getKey(); final Object value = e.getValue(); if (value == null || !(value instanceof Double)) return false; final double v = ((Double) (value)).doubleValue(); return map.remove(k, v); } @Override
@Override public void combine(SystemMetric<U, I> other) { AbstractSalesDiversityMetric<U, I> otherM = (AbstractSalesDiversityMetric<U, I>) other; otherM.itemCount.object2DoubleEntrySet() .forEach(e -> itemCount.addTo(e.getKey(), e.getDoubleValue())); otherM.itemWeight.object2DoubleEntrySet() .forEach(e -> itemWeight.addTo(e.getKey(), e.getDoubleValue())); freeNorm += otherM.freeNorm; numUsers += otherM.numUsers; }
@Override public double nextDouble() { return i.next().getDoubleValue(); }; @Override