it.unimi.dsi.fastutil.objects.Object2DoubleMap Java Examples

The following examples show how to use it.unimi.dsi.fastutil.objects.Object2DoubleMap. 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: DioriteRandomUtils.java    From Diorite with MIT License 6 votes vote down vote up
@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;
}
 
Example #2
Source File: FastTreeHashVector.java    From spf with GNU General Public License v2.0 6 votes vote down vote up
@Override
public double dotProduct(IHashVectorImmutable other) {
	if (size() <= other.size()) {
		if (other instanceof FastTreeHashVector) {
			final FastTreeHashVector thv = (FastTreeHashVector) other;
			double sum = 0.0;
			for (final it.unimi.dsi.fastutil.objects.Object2DoubleMap.Entry<KeyArgs> entry : values
					.object2DoubleEntrySet()) {
				if (thv.values.containsKey(entry.getKey())) {
					sum += entry.getDoubleValue()
							* thv.values.get(entry.getKey());
				}
			}
			return sum;
		} else {
			return dotProduct(new FastTreeHashVector(other));
		}
	} else {
		return other.dotProduct(this);
	}
}
 
Example #3
Source File: FastTreeHashVector.java    From spf with GNU General Public License v2.0 6 votes vote down vote up
@Override
public FastTreeHashVector addTimes(final double times,
		IHashVectorImmutable other) {
	if (other instanceof FastTreeHashVector) {
		final FastTreeHashVector p = (FastTreeHashVector) other;
		final FastTreeHashVector ret = new FastTreeHashVector(this);
		for (final it.unimi.dsi.fastutil.objects.Object2DoubleMap.Entry<KeyArgs> entry : p.values
				.object2DoubleEntrySet()) {
			final KeyArgs key = entry.getKey();
			Double value = entry.getDoubleValue() * times;
			if (values.containsKey(key)) {
				value += values.getDouble(key);
			}
			ret.values.put(key, value);
		}
		return ret;
	} else {
		return addTimes(times, new FastTreeHashVector(other));
	}
}
 
Example #4
Source File: FastTreeHashVector.java    From spf with GNU General Public License v2.0 6 votes vote down vote up
@Override
public FastTreeHashVector pairWiseProduct(IHashVectorImmutable other) {
	if (other instanceof FastTreeHashVector) {
		final FastTreeHashVector p = (FastTreeHashVector) other;
		if (size() <= other.size()) {
			final FastTreeHashVector ret = new FastTreeHashVector(this);
			for (final it.unimi.dsi.fastutil.objects.Object2DoubleMap.Entry<KeyArgs> entry : values
					.object2DoubleEntrySet()) {
				final KeyArgs key = entry.getKey();
				if (p.values.containsKey(key)) {
					ret.values.put(key,
							entry.getDoubleValue() * p.values.get(key));
				}
			}
			return ret;
		} else {
			return p.pairWiseProduct(this);
		}
	} else {
		return pairWiseProduct(new FastTreeHashVector(other));
	}
}
 
Example #5
Source File: FastTreeHashVector.java    From spf with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String toString() {
	final StringBuilder ret = new StringBuilder();
	ret.append("{");
	final Iterator<it.unimi.dsi.fastutil.objects.Object2DoubleMap.Entry<KeyArgs>> iterator = values
			.object2DoubleEntrySet().iterator();
	while (iterator.hasNext()) {
		final it.unimi.dsi.fastutil.objects.Object2DoubleMap.Entry<KeyArgs> next = iterator
				.next();
		ret.append(next.getKey());
		ret.append("=");
		ret.append(String.format("%.3f", next.getDoubleValue()));
		if (iterator.hasNext()) {
			ret.append(", ");
		}
	}
	ret.append("}");
	return ret.toString();
}
 
Example #6
Source File: FieldBlendingPredictor.java    From samantha with MIT License 6 votes vote down vote up
public List<Prediction> predict(List<ObjectNode> entityList, RequestContext requestContext) {
    for (EntityExpander expander : entityExpanders) {
        entityList = expander.expand(entityList, requestContext);
    }
    List<Prediction> scoredList = new ArrayList<>(entityList.size());
    for (ObjectNode entity : entityList) {
        double score = 0.0;
        for (Object2DoubleMap.Entry<String> entry : defaults.object2DoubleEntrySet()) {
            String key = entry.getKey();
            if (entity.has(key)) {
                score += (entry.getDoubleValue() * entity.get(key).asDouble());
            }
        }
        scoredList.add(new Prediction(entity, null, score, null));
    }
    return scoredList;
}
 
Example #7
Source File: ERRIA.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Returns a score for the recommendation list.
 *
 * @param recommendation recommendation list
 * @return score of the metric to the recommendation
 */
@Override
public double evaluate(Recommendation<U, I> recommendation) {
    RelevanceModel.UserRelevanceModel<U, I> userRelModel = relModel.getModel(recommendation.getUser());
    UserIntentModel<U, I, F> uim = intentModel.getModel(recommendation.getUser());

    DoubleAdder erria = new DoubleAdder();

    Object2DoubleMap<F> pNoPrevRel = new Object2DoubleOpenHashMap<>();
    pNoPrevRel.defaultReturnValue(0.0);
    uim.getIntents().forEach(f -> pNoPrevRel.put(f, 1.0));

    AtomicInteger rank = new AtomicInteger();
    recommendation.getItems().stream().limit(cutoff).forEach(iv -> {
        if (userRelModel.isRelevant(iv.v1)) {
            double gain = userRelModel.gain(iv.v1);
            uim.getItemIntents(iv.v1).forEach(f -> {
                double red = pNoPrevRel.getDouble(f);
                erria.add(uim.pf_u(f) * gain * red / (1.0 + rank.intValue()));
                pNoPrevRel.put(f, red * (1 - gain));
            });
        }
        rank.incrementAndGet();
    });

    return erria.doubleValue();
}
 
Example #8
Source File: FastTreeHashVector.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean valuesInRange(final double min, final double max) {
	for (final it.unimi.dsi.fastutil.objects.Object2DoubleMap.Entry<KeyArgs> entry : values
			.object2DoubleEntrySet()) {
		final Double value = entry.getDoubleValue();
		if (value < min || value > max) {
			return false;
		}
	}
	return true;
}
 
Example #9
Source File: BinomialModel.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
private Object2DoubleMap<F> getGlobalFeatureProbs() {
    Object2DoubleMap<F> probs = new Object2DoubleOpenHashMap<>();
    probs.defaultReturnValue(0.0);

    int n = recommenderData.numPreferences();
    featureData.getAllFeatures().sequential().forEach(f -> {
        int numPrefs = featureData.getFeatureItems(f)
                .map(Tuple2::v1)
                .mapToInt(recommenderData::numUsers)
                .sum();
        probs.put(f, numPrefs / (double) n);
    });

    return probs;
}
 
Example #10
Source File: ItemNoveltyReranker.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public int[] rerankPermutation(Recommendation<U, I> recommendation, int maxLength) {
    U user = recommendation.getUser();
    List<Tuple2od<I>> items = recommendation.getItems();
    int M = items.size();
    int N = min(maxLength, M);
    
    if (lambda == 0.0) {
        return getBasePerm(N);
    }

    ItemNovelty.UserItemNoveltyModel<U, I> uinm = novelty.getModel(user);
    if (uinm == null) {
        return new int[0];
    }

    Object2DoubleMap<I> novMap = new Object2DoubleOpenHashMap<>();
    Stats relStats = new Stats();
    Stats novStats = new Stats();
    recommendation.getItems().forEach(itemValue -> {
        double nov = uinm.novelty(itemValue.v1);
        novMap.put(itemValue.v1, nov);
        relStats.accept(itemValue.v2);
        novStats.accept(nov);
    });

    IntDoubleTopN topN = new IntDoubleTopN(N);
    for (int i = 0; i < M; i++) {
        topN.add(M - i, value(items.get(i), relStats, novMap, novStats));
    }
    topN.sort();

    return topN.reverseStream()
            .mapToInt(e -> M - e.v1)
            .toArray();
}
 
Example #11
Source File: FastTreeHashVector.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public String printValues(IHashVectorImmutable other) {
	final StringBuilder ret = new StringBuilder();
	ret.append("{");
	if (other instanceof FastTreeHashVector) {
		final FastTreeHashVector p = (FastTreeHashVector) other;
		for (final Iterator<it.unimi.dsi.fastutil.objects.Object2DoubleMap.Entry<KeyArgs>> iterator = p.values
				.object2DoubleEntrySet().iterator(); iterator.hasNext();) {
			final it.unimi.dsi.fastutil.objects.Object2DoubleMap.Entry<KeyArgs> entry = iterator
					.next();
			if (values.containsKey(entry.getKey())) {
				ret.append(entry.getKey()
						+ "="
						+ String.format("%.3f", values.get(entry.getKey()))
						+ "("
						+ String.format("%.3f",
								p.values.get(entry.getKey())) + ")");
			} else {
				ret.append(entry.getKey()
						+ "="
						+ ZERO_VALUE
						+ "("
						+ String.format("%.3f",
								p.values.get(entry.getKey())) + ")");
			}
			if (iterator.hasNext()) {
				ret.append(",");
			}
		}
		ret.append("}");
		return ret.toString();
	} else {
		return printValues(new FastTreeHashVector(other));
	}
}
 
Example #12
Source File: VectorFeatureItemDistanceModel.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Returns a function that returns the feature-based similarity to the
 * features of an item.
 *
 * @param features1 stream of features of an item
 * @return function that returns the feature-based similarity to the
 * features of an item
 */
@Override
public ToDoubleFunction<Stream<Tuple2<F, Double>>> dist(Stream<Tuple2<F, Double>> features1) {
    Object2DoubleMap<F> auxMap = new Object2DoubleOpenHashMap<>();
    auxMap.defaultReturnValue(0.0);
    DoubleAdder norm1 = new DoubleAdder();

    features1.forEach(fv -> {
        auxMap.put(fv.v1, fv.v2);
        norm1.add(fv.v2 * fv.v2);
    });

    if (norm1.doubleValue() == 0) {
        return features2 -> Double.NaN;
    }

    return features2 -> {
        DoubleAdder prod = new DoubleAdder();
        DoubleAdder norm2 = new DoubleAdder();
        features2.forEach(fv -> {
            prod.add(fv.v2 * auxMap.getDouble(fv.v1));
            norm2.add(fv.v2 * fv.v2);
        });

        if (norm2.doubleValue() == 0) {
            return Double.NaN;
        }

        return dist(prod.doubleValue(), norm1.doubleValue(), norm2.doubleValue());
    };
}
 
Example #13
Source File: FastTreeHashVector.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void multiplyBy(double value) {
	for (final it.unimi.dsi.fastutil.objects.Object2DoubleMap.Entry<KeyArgs> entry : values
			.object2DoubleEntrySet()) {
		entry.setValue(entry.getDoubleValue() * value);
	}
}
 
Example #14
Source File: FastTreeHashVector.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void iterate(EntryFunction function) {
	for (final it.unimi.dsi.fastutil.objects.Object2DoubleMap.Entry<KeyArgs> entry : values
			.object2DoubleEntrySet()) {
		function.apply(entry.getKey(), entry.getDoubleValue());
	}
}
 
Example #15
Source File: FastTreeHashVector.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public IHashVector getAll(final String arg1, final String arg2,
		final String arg3, final String arg4, final String arg5) {
	final FastTreeHashVector result = new FastTreeHashVector();
	for (final it.unimi.dsi.fastutil.objects.Object2DoubleMap.Entry<KeyArgs> entry : values
			.object2DoubleEntrySet()) {
		final KeyArgs key = entry.getKey();
		if (arg1.equals(key.arg1) && arg2.equals(key.arg2)
				&& arg3.equals(key.arg3) && arg4.equals(key.arg4)
				&& arg5.equals(key.arg5)) {
			result.values.put(key, entry.getDoubleValue());
		}
	}
	return result;
}
 
Example #16
Source File: FastTreeHashVector.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public IHashVector getAll(final String arg1, final String arg2,
		final String arg3, final String arg4) {
	final FastTreeHashVector result = new FastTreeHashVector();
	for (final it.unimi.dsi.fastutil.objects.Object2DoubleMap.Entry<KeyArgs> entry : values
			.object2DoubleEntrySet()) {
		final KeyArgs key = entry.getKey();
		if (arg1.equals(key.arg1) && arg2.equals(key.arg2)
				&& arg3.equals(key.arg3) && arg4.equals(key.arg4)) {
			result.values.put(key, entry.getDoubleValue());
		}
	}
	return result;
}
 
Example #17
Source File: AlphaBayesRuleReranker.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * 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()))
            );
}
 
Example #18
Source File: FastTreeHashVector.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public IHashVector getAll(final String arg1, final String arg2,
		final String arg3) {
	final FastTreeHashVector result = new FastTreeHashVector();
	for (final it.unimi.dsi.fastutil.objects.Object2DoubleMap.Entry<KeyArgs> entry : values
			.object2DoubleEntrySet()) {
		final KeyArgs key = entry.getKey();
		if (arg1.equals(key.arg1) && arg2.equals(key.arg2)
				&& arg3.equals(key.arg3)) {
			result.values.put(key, entry.getDoubleValue());
		}
	}
	return result;
}
 
Example #19
Source File: FastTreeHashVector.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public IHashVector getAll(final String arg1, final String arg2) {
	final FastTreeHashVector result = new FastTreeHashVector();
	for (final it.unimi.dsi.fastutil.objects.Object2DoubleMap.Entry<KeyArgs> entry : values
			.object2DoubleEntrySet()) {
		final KeyArgs key = entry.getKey();
		if (arg1.equals(key.arg1) && arg2.equals(key.arg2)) {
			result.values.put(key, entry.getDoubleValue());
		}
	}
	return result;
}
 
Example #20
Source File: FastTreeHashVector.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public IHashVector getAll(KeyArgs partialKey) {
	final FastTreeHashVector result = new FastTreeHashVector();
	for (final it.unimi.dsi.fastutil.objects.Object2DoubleMap.Entry<KeyArgs> entry : values
			.object2DoubleEntrySet()) {
		final KeyArgs key = entry.getKey();
		if (partialKey.contains(key)) {
			result.values.put(key, entry.getDoubleValue());
		}
	}
	return result;
}
 
Example #21
Source File: FastTreeHashVector.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public IHashVector getAll(final String arg1) {
	final FastTreeHashVector result = new FastTreeHashVector();
	for (final it.unimi.dsi.fastutil.objects.Object2DoubleMap.Entry<KeyArgs> entry : values
			.object2DoubleEntrySet()) {
		final KeyArgs key = entry.getKey();
		if (arg1.equals(key.arg1)) {
			result.values.put(key, entry.getDoubleValue());
		}
	}
	return result;
}
 
Example #22
Source File: TweetCentroid.java    From AffectiveTweets with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Adds values of additional attributes to the word vector.
 * @param metaVector the values of the additional attributes of the document
 */
public void addMetaData(Object2DoubleMap<String> metaVector){
	for(String metaName:metaVector.keySet()){
		double metaVal=metaVector.getDouble(metaName);
		this.metaData.put(metaName, metaVal+this.metaData.getDouble(metaName));
	}
}
 
Example #23
Source File: FieldBlendingPredictor.java    From samantha with MIT License 5 votes vote down vote up
public FieldBlendingPredictor(Configuration config, Configuration daoConfigs, Injector injector,
                              List<EntityExpander> entityExpanders,
                              Object2DoubleMap<String> defaults, String daoConfigKey) {
    super(config, daoConfigs, daoConfigKey, injector);
    this.entityExpanders = entityExpanders;
    this.defaults = defaults;
}
 
Example #24
Source File: FieldBlendingRanker.java    From samantha with MIT License 5 votes vote down vote up
public FieldBlendingRanker(Object2DoubleMap<String> defaults, int offset, int limit, int pageSize,
                           RequestContext requestContext, Injector injector, Configuration config) {
    super(config, requestContext, injector);
    this.defaults = defaults;
    this.offset = offset;
    this.limit = limit;
    this.pageSize = pageSize;
}
 
Example #25
Source File: PercentileBlendingRanker.java    From samantha with MIT License 5 votes vote down vote up
public PercentileBlendingRanker(Object2DoubleMap<String> defaults, int offset, int limit, int pageSize,
                                Configuration config, RequestContext requestContext, Injector injector) {
    super(config, requestContext, injector);
    this.defaults = defaults;
    this.offset = offset;
    this.limit = limit;
    this.pageSize = pageSize;
}
 
Example #26
Source File: PercentileBlendingRankerConfig.java    From samantha with MIT License 5 votes vote down vote up
private PercentileBlendingRankerConfig(int pageSize, Object2DoubleMap<String> defaults,
                                       Injector injector,
                                       Configuration config) {
    this.pageSize = pageSize;
    this.defaults = defaults;
    this.injector = injector;
    this.config = config;
}
 
Example #27
Source File: PercentileBlendingRankerConfig.java    From samantha with MIT License 5 votes vote down vote up
public static RankerConfig getRankerConfig(Configuration rankerConfig,
                                           Injector injector) {
    int pageSize = 24;
    if (rankerConfig.asMap().containsKey(ConfigKey.RANKER_PAGE_SIZE.get())) {
        rankerConfig.getInt(ConfigKey.RANKER_PAGE_SIZE.get());
    }
    Object2DoubleMap<String> defaults = new Object2DoubleOpenHashMap<>();
    Configuration defaultConfig = rankerConfig.getConfig("blendingDefaults");
    for (String key : defaultConfig.keys()) {
        defaults.put(key, defaultConfig.getDouble(key));
    }
    return new PercentileBlendingRankerConfig(pageSize, defaults, injector, rankerConfig);
}
 
Example #28
Source File: FieldBlendingRankerConfig.java    From samantha with MIT License 5 votes vote down vote up
private FieldBlendingRankerConfig(int pageSize, Object2DoubleMap<String> defaults,
                                  Injector injector,
                                  Configuration config) {
    this.pageSize = pageSize;
    this.defaults = defaults;
    this.injector = injector;
    this.config = config;
}
 
Example #29
Source File: FieldBlendingRankerConfig.java    From samantha with MIT License 5 votes vote down vote up
public static RankerConfig getRankerConfig(Configuration rankerConfig,
                                           Injector injector) {
    int pageSize = 24;
    if (rankerConfig.asMap().containsKey(ConfigKey.RANKER_PAGE_SIZE.get())) {
        rankerConfig.getInt(ConfigKey.RANKER_PAGE_SIZE.get());
    }
    Object2DoubleMap<String> defaults = new Object2DoubleOpenHashMap<>();
    Configuration defaultConfig = rankerConfig.getConfig("blendingDefaults");
    for (String key : defaultConfig.keys()) {
        defaults.put(key, defaultConfig.getDouble(key));
    }
    return new FieldBlendingRankerConfig(pageSize, defaults, injector, rankerConfig);
}
 
Example #30
Source File: FastTreeHashVector.java    From spf with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void dropZeros() {
	final Iterator<it.unimi.dsi.fastutil.objects.Object2DoubleMap.Entry<KeyArgs>> iterator = values
			.object2DoubleEntrySet().iterator();
	while (iterator.hasNext()) {
		if (Math.abs(iterator.next().getValue()) == ZERO_VALUE) {
			iterator.remove();
		}
	}
}