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

The following examples show how to use it.unimi.dsi.fastutil.objects.Object2DoubleOpenHashMap. 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: BinomialNonRedundancyReranker.java    From RankSys with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Constructor.
 *
 * @param recommendation input recommendation to be re-ranked
 * @param maxLength number of items to be greedily selected
 */
public BinomialNonRedundancyUserReranker(Recommendation<U, I> recommendation, int maxLength) {
    super(recommendation, maxLength);

    ubm = binomialModel.getModel(recommendation.getUser());

    featureCount = new Object2IntOpenHashMap<>();
    featureCount.defaultReturnValue(0);

    patienceNow = new Object2DoubleOpenHashMap<>();
    patienceLater = new Object2DoubleOpenHashMap<>();
    ubm.getFeatures().forEach(f -> {
        patienceNow.put(f, ubm.patience(0, f, cutoff));
        patienceLater.put(f, ubm.patience(1, f, cutoff));
    });
}
 
Example #2
Source File: ScoresRelevanceAspectModel.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public ItemAspectModel<I, F> getItemAspectModel(List<Tuple2od<I>> items) {
    Object2DoubleOpenHashMap<F> probNorm = new Object2DoubleOpenHashMap<>();
    items.forEach(iv -> getItemIntents(iv.v1)
            .forEach(f -> {
                if (iv.v2 > probNorm.getOrDefault(f, 0.0)) {
                    probNorm.put(f, iv.v2);
                }
            }));

    return (iv, f) -> (Math.pow(2, iv.v2 / probNorm.getDouble(f)) - 1) / 2.0;
}
 
Example #3
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 #4
Source File: PCItemNovelty.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
public UserPCItemNoveltyModel(PreferenceData<U, I> recommenderData) {
    itemNovelty = new Object2DoubleOpenHashMap<>();
    itemNovelty.defaultReturnValue(1.0);
    int numUsers = recommenderData.numUsersWithPreferences();
    recommenderData.getItemsWithPreferences()
            .forEach(i -> itemNovelty.put(i, 1 - recommenderData.numUsers(i) / (double) numUsers));
}
 
Example #5
Source File: FDItemNovelty.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
public UserFDItemNoveltyModel(PreferenceData<U, I> recommenderData) {
    IntSummaryStatistics stats = recommenderData.getItemsWithPreferences().mapToInt(recommenderData::numUsers).summaryStatistics();
    double norm = stats.getSum();
    double maxNov = -log(stats.getMin() / norm) / log(2);

    itemNovelty = new Object2DoubleOpenHashMap<>();
    itemNovelty.defaultReturnValue(maxNov);
    recommenderData.getItemsWithPreferences()
            .forEach(i -> itemNovelty.put(i, -log(recommenderData.numUsers(i) / norm) / log(2)));
}
 
Example #6
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 #7
Source File: LambdaReranker.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
@Override
protected int selectItem(IntSortedSet remainingI, List<Tuple2od<I>> list) {
    novMap = new Object2DoubleOpenHashMap<>();
    relStats = new Stats();
    novStats = new Stats();
    remainingI.forEach(i -> {
        Tuple2od<I> itemValue = list.get(i);
        double nov = nov(itemValue);
        novMap.put(itemValue.v1, nov);
        relStats.accept(itemValue.v2);
        novStats.accept(nov);
    });
    return super.selectItem(remainingI, list);
}
 
Example #8
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 #9
Source File: NDCG.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Constructor.
 *
 * @param user user whose relevance model is computed
 */
public UserNDCGRelevanceModel(U user) {
    this.gainMap = new Object2DoubleOpenHashMap<>();
    gainMap.defaultReturnValue(0.0);

    testData.getUserPreferences(user)
            .filter(iv -> iv.v2 >= threshold)
            .forEach(iv -> gainMap.put(iv.v1, Math.pow(2, iv.v2 - threshold + 1.0) - 1.0));
}
 
Example #10
Source File: BackgroundBinaryRelevanceModel.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
public UserSmo4RelevanceModel(U user) {
    this.gainMap = new Object2DoubleOpenHashMap<>();
    this.gainMap.defaultReturnValue(background);

    testData.getUserPreferences(user).forEach(iv -> {
        if (iv.v2 >= threshold) {
            gainMap.put(iv.v1, 1.0);
        } else {
            gainMap.put(iv.v1, 0.0);
        }
    });
}
 
Example #11
Source File: MMR.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Constructor.
 *
 * @param recommendation input recommendation to be re-ranked
 * @param maxLength maximum length of the re-ranked recommendation
 */
public UserMMR(Recommendation<U, I> recommendation, int maxLength) {
    super(recommendation, maxLength);

    n = 0;
    avgDist = new Object2DoubleOpenHashMap<>();
    avgDist.defaultReturnValue(0.0);
    recommendation.getItems().stream().sequential()
            .map(Tuple2od::v1)
            .forEach(i -> avgDist.put(i, 0.0));
}
 
Example #12
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 #13
Source File: FeatureIntentModel.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
private void init() {
    featureNorms = new Object2DoubleOpenHashMap<>();
    featureData.getAllFeatures().forEach(f -> {
        int count = featureData.getFeatureItems(f)
                .map(Tuple2::v1)
                .mapToInt(totalData::numUsers)
                .sum();
        featureNorms.put(f, count);
    });
}
 
Example #14
Source File: FastSessionCoOccurrence.java    From StreamingRec with Apache License 2.0 5 votes vote down vote up
@Override
public LongArrayList recommendInternal(ClickData clickData) {
	//create a list of scores for each item, which is the sum of all co-occurrence counts
	Map<String, Double> combineWeights = new Object2DoubleOpenHashMap<String>();
	//depending on if we are supposed to use the whole session or not,
	//this for loop only does one iteration on the last element or it iterates over
	//all click in the current user sesssion
	for (int i = wholeSession?0:(clickData.session.size() - 1); i < clickData.session.size(); i++) {
		Transaction click = clickData.session.get(i);
		// if the inner map is empty, we cannot recommend anything
		if (!coOcurrenceMap.containsKey(getCoOccurrenceKey(click))) {
			continue;
		}
		// get the inner map of items that this item has co-occurred with
		Map<String, Integer> m = coOcurrenceMap.get(getCoOccurrenceKey(click));
		for (Entry<String, Integer> entry : m.entrySet()) {
			// sum up the co-occurrence weights for each item
			Double currVal = combineWeights.get(entry.getKey());
			if(currVal == null){
				currVal = 0d;
			}
			combineWeights.put(entry.getKey(), currVal + entry.getValue());
		}
	}

	// sort the weighted sums
	Map<String, Double> sortedKeys = Util.sortByValue(combineWeights, false);
	return generateResultList(sortedKeys, clickData);		
}
 
Example #15
Source File: AlphaXQuAD.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Constructor.
 *
 * @param recommendation input recommendation to be re-ranked
 * @param maxLength maximum length to be re-ranked with xQuAD
 */
public UserAlphaXQuAD(Recommendation<U, I> recommendation, int maxLength) {
    super(recommendation, maxLength);

    this.uam = aspectModel.getModel(recommendation.getUser());
    this.iam = uam.getItemAspectModel(recommendation.getItems());
    this.redundancy = new Object2DoubleOpenHashMap<>();
    this.redundancy.defaultReturnValue(1.0);
}
 
Example #16
Source File: ScoresAspectModel.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public ItemAspectModel<I, F> getItemAspectModel(List<Tuple2od<I>> items) {
    Object2DoubleOpenHashMap<F> probNorm = new Object2DoubleOpenHashMap<>();
    items.forEach(iv -> getItemIntents(iv.v1).forEach(f -> probNorm.addTo(f, iv.v2)));

    return (iv, f) -> iv.v2 / probNorm.getDouble(f);
}
 
Example #17
Source File: ERRIA.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Constructor.
 *
 * @param user user whose relevance model is created
 */
public UserERRRelevanceModel(U user) {
    this.gainMap = new Object2DoubleOpenHashMap<>();
    gainMap.defaultReturnValue(0.0);

    testData.getUserPreferences(user)
            .filter(iv -> iv.v2 >= threshold)
            .forEach(iv -> gainMap.put(iv.v1, (Math.pow(2, iv.v2) - 1) / Math.pow(2, maxPreference)));
}
 
Example #18
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 #19
Source File: AbstractSalesDiversityMetric.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Constructor
 *
 * @param cutoff maximum length of the recommendation lists that is evaluated
 * @param disc ranking discount model
 * @param rel relevance model
 */
public AbstractSalesDiversityMetric(int cutoff, RankingDiscountModel disc, RelevanceModel<U, I> rel) {
    this.cutoff = cutoff;
    this.disc = disc;
    this.rel = rel;

    this.itemCount = new Object2DoubleOpenHashMap<>();
    this.itemCount.defaultReturnValue(0.0);
    this.itemWeight = new Object2DoubleOpenHashMap<>();
    this.itemWeight.defaultReturnValue(0.0);
    this.freeNorm = 0;
    this.numUsers = 0;
}
 
Example #20
Source File: PM.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
public UserPM(Recommendation<U, I> recommendation, int maxLength) {
    super(recommendation, maxLength);

    this.ubm = binomialModel.getModel(recommendation.getUser());
    this.featureCount = new Object2DoubleOpenHashMap<>();
    featureCount.defaultReturnValue(0.0);
    this.probNorm = new Object2DoubleOpenHashMap<>();
    recommendation.getItems()
            .forEach(i -> featureData.getItemFeatures(i.v1).sequential()
                    .forEach(fv -> probNorm.addTo(fv.v1, i.v2)));
    this.lcf = getLcf();
}
 
Example #21
Source File: SVDFeature.java    From samantha with MIT License 5 votes vote down vote up
public Object2DoubleMap<String> getFactorFeatures(int minSupport) {
    int numFeas = indexSpace.getKeyMapSize(SVDFeatureKey.FACTORS.get());
    Object2DoubleMap<String> fea2sup = new Object2DoubleOpenHashMap<>();
    for (int i=0; i<numFeas; i++) {
        String feature = (String)indexSpace.getKeyForIndex(SVDFeatureKey.FACTORS.get(), i);
        if (indexSpace.containsKey(SVDFeatureKey.BIASES.get(), feature)) {
            int idx = indexSpace.getIndexForKey(SVDFeatureKey.BIASES.get(), feature);
            double support = variableSpace.getScalarVarByNameIndex(SVDFeatureKey.SUPPORT.get(), idx);
            if (support >= minSupport) {
                fea2sup.put(feature, support);
            }
        }
    }
    return fea2sup;
}
 
Example #22
Source File: KnnModelTrigger.java    From samantha with MIT License 5 votes vote down vote up
public List<ObjectNode> getTriggeredFeatures(List<ObjectNode> bases) {
    Object2DoubleMap<String> item2score = new Object2DoubleOpenHashMap<>();
    int numInter = 0;
    for (ObjectNode inter : bases) {
        double weight = 1.0;
        if (inter.has(weightAttr)) {
            weight = inter.get(weightAttr).asDouble();
        }
        String key = FeatureExtractorUtilities.composeConcatenatedKey(inter, feaAttrs);
        if (weight >= 0.5 && featureKnnModel != null) {
            getNeighbors(item2score, featureKnnModel, key, weight);
        }
        if (weight < 0.5 && featureKdnModel != null) {
            getNeighbors(item2score, featureKdnModel, key, weight);
        }
        numInter++;
        if (numInter >= maxInter) {
            break;
        }
    }
    List<ObjectNode> results = new ArrayList<>();
    for (Map.Entry<String, Double> entry : item2score.entrySet()) {
        ObjectNode entity = Json.newObject();
        Map<String, String> attrVals = FeatureExtractorUtilities.decomposeKey(entry.getKey());
        for (Map.Entry<String, String> ent : attrVals.entrySet()) {
            entity.put(ent.getKey(), ent.getValue());
        }
        entity.put(scoreAttr, entry.getValue());
        results.add(entity);
    }
    results.sort(SortingUtilities.jsonFieldReverseComparator(scoreAttr));
    return results;
}
 
Example #23
Source File: FieldBlendingPredictorConfig.java    From samantha with MIT License 5 votes vote down vote up
public static PredictorConfig getPredictorConfig(Configuration predictorConfig,
                                                 Injector injector) {
    Object2DoubleMap<String> defaults = new Object2DoubleOpenHashMap<>();
    Configuration defaultConfig = predictorConfig.getConfig("blendingDefaults");
    for (String key : defaultConfig.keys()) {
        defaults.put(key, defaultConfig.getDouble(key));
    }
    List<Configuration> expanders = ExpanderUtilities.getEntityExpandersConfig(predictorConfig);
    return new FieldBlendingPredictorConfig(predictorConfig,
            predictorConfig.getConfig(ConfigKey.ENTITY_DAOS_CONFIG.get()),
            injector, expanders, defaults, predictorConfig.getString("daoConfigKey"));
}
 
Example #24
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 #25
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 #26
Source File: TweetCentroid.java    From AffectiveTweets with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new WordRep object.
 * @param word the word
 */
public WordRep(String word){
	this.word=word;
	this.numDoc=0;
	this.wordSpace=new Object2IntOpenHashMap<String>();
	this.metaData=new Object2DoubleOpenHashMap<String>();
}