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

The following examples show how to use it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap. 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: DistantSupervisionSyntheticFilter.java    From AffectiveTweets with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Calculates tweet vectors from a list of tokens
 * @param tokens a tokenized tweet
 * @return a mapping between attribute names and values
 */
public Object2IntMap<String> calculateDocVec(List<String> tokens) {

	Object2IntMap<String> docVec = new Object2IntOpenHashMap<String>();
	// add the word-based vector
	if(this.createWordAtts)
		docVec.putAll(affective.core.Utils.calculateTermFreq(tokens,UNIPREFIX,this.freqWeights));

	if(this.createClustAtts){
		// calcultates the vector of clusters
		List<String> brownClust=affective.core.Utils.clustList(tokens,brownDict);
		docVec.putAll(affective.core.Utils.calculateTermFreq(brownClust,CLUSTPREFIX,this.freqWeights));			
	}	


	return docVec;

}
 
Example #2
Source File: OnHeapStringDictionary.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
public OnHeapStringDictionary(PinotDataBuffer dataBuffer, int length, int numBytesPerValue, byte paddingByte) {
  super(dataBuffer, length, numBytesPerValue, paddingByte);

  _paddingByte = paddingByte;
  byte[] buffer = new byte[numBytesPerValue];
  _unpaddedStrings = new String[length];
  _unPaddedStringToIdMap = new Object2IntOpenHashMap<>(length);
  _unPaddedStringToIdMap.defaultReturnValue(NULL_VALUE_INDEX);

  for (int i = 0; i < length; i++) {
    String unpaddedString = getUnpaddedString(i, buffer);
    _unpaddedStrings[i] = unpaddedString;
    _unPaddedStringToIdMap.put(unpaddedString, i);
  }

  if (paddingByte == 0) {
    _paddedStrings = null;
  } else {
    _paddedStrings = new String[length];
    for (int i = 0; i < length; i++) {
      _paddedStrings[i] = getPaddedString(i, buffer);
    }
  }
}
 
Example #3
Source File: NoDictionaryMultiColumnGroupKeyGenerator.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
public NoDictionaryMultiColumnGroupKeyGenerator(TransformOperator transformOperator,
    ExpressionContext[] groupByExpressions, int numGroupsLimit) {
  _groupByExpressions = groupByExpressions;
  _numGroupByExpressions = groupByExpressions.length;
  _dataTypes = new FieldSpec.DataType[_numGroupByExpressions];
  _dictionaries = new Dictionary[_numGroupByExpressions];
  _onTheFlyDictionaries = new ValueToIdMap[_numGroupByExpressions];

  for (int i = 0; i < _numGroupByExpressions; i++) {
    ExpressionContext groupByExpression = groupByExpressions[i];
    TransformResultMetadata transformResultMetadata = transformOperator.getResultMetadata(groupByExpression);
    _dataTypes[i] = transformResultMetadata.getDataType();
    if (transformResultMetadata.hasDictionary()) {
      _dictionaries[i] = transformOperator.getDictionary(groupByExpression);
    } else {
      _onTheFlyDictionaries[i] = ValueToIdMapFactory.get(_dataTypes[i]);
    }
  }

  _groupKeyMap = new Object2IntOpenHashMap<>();
  _groupKeyMap.defaultReturnValue(INVALID_ID);
  _globalGroupIdUpperBound = numGroupsLimit;
}
 
Example #4
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 #5
Source File: BinomialMetric.java    From RankSys with Mozilla Public License 2.0 6 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());
    BinomialModel<U, I, F>.UserBinomialModel prob = binomialModel.getModel(recommendation.getUser());

    Object2IntOpenHashMap<F> count = new Object2IntOpenHashMap<>();
    count.defaultReturnValue(0);

    int rank = 0;
    int nrel = 0;
    for (Tuple2od<I> iv : recommendation.getItems()) {
        if (userRelModel.isRelevant(iv.v1)) {
            featureData.getItemFeatures(iv.v1)
                    .forEach(fv -> count.addTo(fv.v1, 1));
            nrel++;
        }

        rank++;
        if (rank >= cutoff) {
            break;
        }
    }

    return getResultFromCount(prob, count, nrel, rank);
}
 
Example #6
Source File: DL4JSequenceRecommender.java    From inception with Apache License 2.0 6 votes vote down vote up
@Override
public void train(RecommenderContext aContext, List<CAS> aCasses)
{
    // Prepare a map where we store the mapping from labels to numeric label IDs - i.e.
    // which index in the label vector represents which label
    Object2IntMap<String> tagsetCollector = new Object2IntOpenHashMap<>();
    
    try {
        ensureEmbeddingsAreAvailable();
        
        // Extract the training data from the CASes
        List<Sample> trainingData = extractData(aCasses, true);
        
        // Use the training data to train the network
        MultiLayerNetwork model = train(trainingData, tagsetCollector);
                    
        aContext.put(KEY_MODEL, model);
        aContext.put(KEY_TAGSET, compileTagset(tagsetCollector));
        aContext.put(KEY_UNKNOWN, randUnk);
    }
    catch (IOException e) {
        throw new IllegalStateException("Unable to train model", e);
    }
}
 
Example #7
Source File: ReadCountCollection.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Rearrange the targets so that they are in a particular order.
 * @return a new collection.
 * @throws IllegalArgumentException if any of the following is true:
 * <ul>
 *     <li>{@code targetsInOrder} is {@code null},</li>
 *     <li>is empty,</li>
 *     <li>it contains {@code null},</li>
 *     <li>contains any target not present in this collection.</li>
 * </ul>
 */
public ReadCountCollection arrangeTargets(final List<Target> targetsInOrder) {
    Utils.nonNull(targetsInOrder);
    Utils.nonEmpty(targetsInOrder, "the input targets list cannot be empty");
    final RealMatrix counts = new Array2DRowRealMatrix(targetsInOrder.size(), columnNames.size());
    final Object2IntMap<Target> targetToIndex = new Object2IntOpenHashMap<>(targets.size());
    for (int i = 0; i < targets.size(); i++) {
        targetToIndex.put(targets.get(i), i);
    }
    for (int i = 0; i < targetsInOrder.size(); i++) {
        final Target target = targetsInOrder.get(i);
        Utils.validateArg(targetToIndex.containsKey(target), () -> String.format("target '%s' is not present in the collection", target.getName()));
        counts.setRow(i, this.counts.getRow(targetToIndex.getInt(target)));
    }
    return new ReadCountCollection(new ArrayList<>(targetsInOrder), columnNames, counts, false);
}
 
Example #8
Source File: PositionListIndex.java    From metanome-algorithms with Apache License 2.0 6 votes vote down vote up
public boolean refines(int[][] lhsInvertedPlis, int[] rhs) {
	for (IntArrayList cluster : this.clusters) {
		Object2IntOpenHashMap<IntArrayList> clustersMap = new Object2IntOpenHashMap<>(cluster.size());
		
		// Check if all subclusters of this cluster point into the same other clusters
		for (int recordId : cluster) {
			IntArrayList additionalLhsCluster = this.buildClusterIdentifier(recordId, lhsInvertedPlis);
			if (additionalLhsCluster == null)
				continue;
			
			if (clustersMap.containsKey(additionalLhsCluster)) {
				if ((rhs[recordId] == -1) || (clustersMap.getInt(additionalLhsCluster) != rhs[recordId]))
					return false;
			}
			else {
				clustersMap.put(additionalLhsCluster, rhs[recordId]);
			}
		}
	}
	return true;
}
 
Example #9
Source File: PositionListIndex.java    From metanome-algorithms with Apache License 2.0 6 votes vote down vote up
public boolean isUniqueWith(int[][] compressedRecords, BitSet otherAttrs, List<IntegerPair> comparisonSuggestions) {
	int attrsSize = otherAttrs.cardinality();
	
	for (IntArrayList cluster : this.clusters) {
		Object2IntOpenHashMap<ClusterIdentifier> value2record = new Object2IntOpenHashMap<>(cluster.size());
		for (int recordId : cluster) {
			ClusterIdentifier value = this.buildClusterIdentifier(otherAttrs, attrsSize, compressedRecords[recordId]);
			if (value == null)
				continue;
			if (value2record.containsKey(value)) {
				comparisonSuggestions.add(new IntegerPair(recordId, value2record.getInt(value)));
				return false;
			}
			value2record.put(value, recordId);
		}
	}
	return true;
}
 
Example #10
Source File: PositionListIndex.java    From metanome-algorithms with Apache License 2.0 6 votes vote down vote up
public boolean refines(int[][] lhsInvertedPlis, int[] rhs) {
	for (IntArrayList cluster : this.clusters) {
		Object2IntOpenHashMap<IntArrayList> clustersMap = new Object2IntOpenHashMap<>(cluster.size());
		
		// Check if all subclusters of this cluster point into the same other clusters
		for (int recordId : cluster) {
			IntArrayList additionalLhsCluster = this.buildClusterIdentifier(recordId, lhsInvertedPlis);
			if (additionalLhsCluster == null)
				continue;
			
			if (clustersMap.containsKey(additionalLhsCluster)) {
				if ((rhs[recordId] == -1) || (clustersMap.getInt(additionalLhsCluster) != rhs[recordId]))
					return false;
			}
			else {
				clustersMap.put(additionalLhsCluster, rhs[recordId]);
			}
		}
	}
	return true;
}
 
Example #11
Source File: PositionListIndex.java    From winter with Apache License 2.0 6 votes vote down vote up
public boolean refines(int[][] lhsInvertedPlis, int[] rhs) {
	for (IntArrayList cluster : this.clusters) {
		Object2IntOpenHashMap<IntArrayList> clustersMap = new Object2IntOpenHashMap<>(cluster.size());
		
		// Check if all subclusters of this cluster point into the same other clusters
		for (int recordId : cluster) {
			IntArrayList additionalLhsCluster = this.buildClusterIdentifier(recordId, lhsInvertedPlis);
			if (additionalLhsCluster == null)
				continue;
			
			if (clustersMap.containsKey(additionalLhsCluster)) {
				if ((rhs[recordId] == -1) || (clustersMap.getInt(additionalLhsCluster) != rhs[recordId]))
					return false;
			}
			else {
				clustersMap.put(additionalLhsCluster, rhs[recordId]);
			}
		}
	}
	return true;
}
 
Example #12
Source File: PositionListIndex.java    From metanome-algorithms with Apache License 2.0 6 votes vote down vote up
public boolean refines(int[][] lhsInvertedPlis, int[] rhs) {
	for (IntArrayList cluster : this.clusters) {
		Object2IntOpenHashMap<IntArrayList> clustersMap = new Object2IntOpenHashMap<>(cluster.size());
		
		// Check if all subclusters of this cluster point into the same other clusters
		for (int recordId : cluster) {
			IntArrayList additionalLhsCluster = this.buildClusterIdentifier(recordId, lhsInvertedPlis);
			if (additionalLhsCluster == null)
				continue;
			
			if (clustersMap.containsKey(additionalLhsCluster)) {
				if ((rhs[recordId] == -1) || (clustersMap.getInt(additionalLhsCluster) != rhs[recordId]))
					return false;
			}
			else {
				clustersMap.put(additionalLhsCluster, rhs[recordId]);
			}
		}
	}
	return true;
}
 
Example #13
Source File: TweetCentroid.java    From AffectiveTweets with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Calculates tweet vectors from a list of tokens
 * @param tokens a tokenized tweet
 * @return a mapping between attribute names and values
 */
public Object2IntMap<String> calculateDocVec(List<String> tokens) {

	Object2IntMap<String> docVec = new Object2IntOpenHashMap<String>();
	// add the word-based vector
	if(this.createWordAtts)
		docVec.putAll(affective.core.Utils.calculateTermFreq(tokens,UNIPREFIX,this.freqWeights));

	if(this.createClustAtts){
		// calcultates the vector of clusters
		List<String> brownClust=affective.core.Utils.clustList(tokens,brownDict);
		docVec.putAll(affective.core.Utils.calculateTermFreq(brownClust,CLUSTPREFIX,this.freqWeights));			
	}	

	return docVec;

}
 
Example #14
Source File: FastSessionCoOccurrence.java    From StreamingRec with Apache License 2.0 6 votes vote down vote up
/**
 * Updates the co-occurrence map based on the current click of the user
 * @param session  -
 */
protected void updateMap(List<Transaction> session) {
	for (int i = 0; i < session.size() - 1; i++) {
		if (getCoOccurrenceKey(session.get(i)) == getCoOccurrenceKey(session.get(session.size()-1))) {
			// ignore co-occurrences of items with themselves
			continue;
		}
		// we add one entry to the map with the "correct" order (time-based)
		addTuple(session.get(i), session.get(session.size()-1));
		// map with opposite order
		addTuple(session.get(session.size()-1), session.get(i));
	}
	//if we are supposed to only look at the last N clicks,
	//this part of the method checks the buffer and cleans out old clicks.
	if(buffer){
		while(ringBuffer.size()>bufferSize){
			//adjust map
			Entry<String, String> first = ringBuffer.poll();
			Object2IntOpenHashMap<String> map = coOcurrenceMap.get(first.getKey());
			map.addTo(first.getValue(), -1);
			map.remove(first.getValue(), 0);//remove if 0
		}
	}		
}
 
Example #15
Source File: Item.java    From StreamingRec with Apache License 2.0 6 votes vote down vote up
/**
 * Instantiates an item from a line in a csv file
 * 
 * @param csvLine -
 * @throws ParseException -
 */
public Item(String csvLine) throws ParseException {
	String[] split = csvLine.split(",");
	publisher = Integer.parseInt(split[0]);
	createdAt = Constants.DATE_FORMAT.parse(split[1]);
	id = Long.parseLong(split[2]);
	url = split[3];
	title = split[4];
	if (split.length > 5) {
		category = Integer.parseInt(split[5]);
	}
	if (split.length > 6) {
		text = split[6];
	}
	if (split.length > 7) {
		keywords = new Object2IntOpenHashMap<>();
		String[] keywordArr = split[7].split(Pattern.quote("#"));
		for (String string : keywordArr) {
			String[] split2 = string.split(Pattern.quote("-"));
			keywords.addTo(split2[0], Integer.parseInt(split2[1]));
		}
	}
}
 
Example #16
Source File: LuceneLinkTokenizer.java    From WikipediaEntities with GNU Affero General Public License v3.0 6 votes vote down vote up
public void close() throws IOException {
  System.err.format("Closing %s output.\n", getClass().getSimpleName());
  PrintStream writer = Util.openOutput(out);
  // We sort everything here. This is expensive, but makes the output
  // files nicer to use in the future.
  ArrayList<String> keys = new ArrayList<>(links.size());
  for(ObjectIterator<Object2IntOpenHashMap.Entry<String>> it = links.object2IntEntrySet().fastIterator(); it.hasNext();) {
    Object2IntOpenHashMap.Entry<String> ent = it.next();
    if(ent.getIntValue() >= MINSUPP) {
      keys.add(ent.getKey());
    }
  }
  Collections.sort(keys);
  for(String key : keys) {
    writer.append(key);
    writer.append('\n');
  }
  if(writer != System.out)
    writer.close();
}
 
Example #17
Source File: Utils.java    From AffectiveTweets with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Calculates a vector of attributes from a list of tokens
 * 
 * @param tokens the input tokens 
 * @param prefix the prefix of each vector attribute
 * @param freqWeights true for considering term-frequency weights (booleans weights are used otherwise)
 * @return an Object2IntMap object mapping the attributes to their values
 */		
public static Object2IntMap<String> calculateTermFreq(List<String> tokens, String prefix, boolean freqWeights) {
	Object2IntMap<String> termFreq = new Object2IntOpenHashMap<String>();

	// Traverse the strings and increments the counter when the token was
	// already seen before
	for (String token : tokens) {
		// add frequency weights if the flat is set
		if(freqWeights)
			termFreq.put(prefix+token, termFreq.getInt(prefix+token) + 1);
		// otherwise, just consider boolean weights
		else{
			if(!termFreq.containsKey(token))
				termFreq.put(prefix+token, 1);
		}
	}

	return termFreq;
}
 
Example #18
Source File: Config.java    From tagme with Apache License 2.0 6 votes vote down vote up
public int getDegree(String l){
		if(degree==null){
			degree=new Object2IntOpenHashMap<String>();
//			for(String lang:langs){
//				IntSet allwid= DatasetLoader.get(new AllWIDs(lang));
//				IntSet ignore = DatasetLoader.get(new IgnoreWIDs(lang));
//				IntSet disambiguation = DatasetLoader.get(new DisambiguationWIDs(lang));
//				IntSet list = DatasetLoader.get(new ListPageWIDs(lang));
//				Int2IntMap redirect = DatasetLoader.get(new RedirectMap(lang));
//				allwid.removeAll(ignore);
//				allwid.removeAll(list);
//				allwid.removeAll(redirect.keySet());
//				allwid.removeAll(disambiguation);
		}
		try{
			degree.put(l, new TopicSearcher(l).numTopics());
		} catch (IOException ioe) {
				throw new ConfigurationException("Unable to load Topic Index", ioe);
		}
		
		return degree.get(l);
	}
 
Example #19
Source File: LuceneLinkTokenizer.java    From WikipediaEntities with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void close() {
  synchronized(LuceneLinkTokenizer.this) {
    Object2IntOpenHashMap<String> plinks = LuceneLinkTokenizer.this.links;
    if(plinks.size() == 0) {
      LuceneLinkTokenizer.this.links = links;
    }
    else {
      for(ObjectIterator<Object2IntOpenHashMap.Entry<String>> it = links.object2IntEntrySet().fastIterator(); it.hasNext();) {
        Object2IntOpenHashMap.Entry<String> ent = it.next();
        plinks.addTo(ent.getKey(), ent.getIntValue());
      }
    }
    links = null;
  }
}
 
Example #20
Source File: CounterSet.java    From WikipediaEntities with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Merge second counter set.
 *
 * @param other Other set of counters.
 */
public static <O> void update(Object2IntOpenHashMap<O> first, Object2IntOpenHashMap<O> second) {
  for(Iterator<Object2IntMap.Entry<O>> iter = second.object2IntEntrySet().fastIterator(); iter.hasNext();) {
    Object2IntMap.Entry<O> entry = iter.next();
    second.addTo(entry.getKey(), entry.getIntValue());
  }
}
 
Example #21
Source File: CounterSet.java    From WikipediaEntities with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Get a descending list of counted items.
 *
 * @return List of items.
 */
public static <O> List<Entry<O>> descending(Object2IntOpenHashMap<O> counters) {
  ArrayList<Entry<O>> copy = new ArrayList<>(counters.size());
  for(Iterator<Object2IntMap.Entry<O>> iter = counters.object2IntEntrySet().fastIterator(); iter.hasNext();) {
    // Note: fast iterator will recycle this object!
    Object2IntMap.Entry<O> entry = iter.next();
    copy.add(new Entry<O>(entry.getKey(), entry.getIntValue()));
  }
  Collections.sort(copy);
  return copy;
}
 
Example #22
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>();
}
 
Example #23
Source File: CategoricalColumn.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default Table countByCategory() {

    final Table t = new Table("Column: " + name());
    final CategoricalColumn<?> categories = (CategoricalColumn<?>) type().create("Category");
    final IntColumn counts = IntColumn.create("Count");

    final Object2IntMap<String> valueToCount = new Object2IntOpenHashMap<>();

    for (int i = 0; i < size(); i++) {
      if (!isMissing(i)) {
        final String next = getString(i);
        if (valueToCount.containsKey(next)) {
          valueToCount.put(next, valueToCount.getInt(next) + 1);
        } else {
          valueToCount.put(next, 1);
        }
      }
    }
    for (Map.Entry<String, Integer> entry : valueToCount.object2IntEntrySet()) {
      categories.appendCell(entry.getKey());
      counts.append(entry.getValue());
    }
    if (countMissing() > 0) {
      categories.appendMissing();
      counts.append(countMissing());
    }
    t.addColumns(categories);
    t.addColumns(counts);
    return t;
  }
 
Example #24
Source File: ForwardBackwardAlgorithm.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Composes a object ot index map given an object list.
 * @param list the list to index.
 * @param <E> the element type.
 * @return never {@code null}.
 */
private <E> Object2IntMap<E> composeIndexMap(final List<E> list) {
    return IntStream.range(0, list.size())
            .collect(
                    () -> new Object2IntOpenHashMap<>(list.size()),
                    (map, i) -> map.put(list.get(i), i),
                    (map1, map2) -> map2.object2IntEntrySet().forEach(
                            e -> map1.put(e.getKey(), e.getIntValue())
                    ));
}
 
Example #25
Source File: AlphaNDCG.java    From RankSys with Mozilla Public License 2.0 5 votes vote down vote up
private double idcg(UserIdealRelevanceModel<U, I> urm) {
    double ideal = 0;

    Object2IntOpenHashMap<F> redundancy = new Object2IntOpenHashMap<>();
    redundancy.defaultReturnValue(0);
    Set<I> candidates = new HashSet<>(urm.getRelevantItems());
    int rank = 0;

    while (rank <= cutoff && !candidates.isEmpty()) {
        I bi = null;
        double bg = Double.NEGATIVE_INFINITY;
        for (I i : candidates) {
            double gain = featureData.getItemFeatures(i)
                    .map(Tuple2::v1)
                    .mapToDouble(f -> Math.pow(1 - alpha, redundancy.getInt(f)))
                    .sum();
            if (gain > bg) {
                bg = gain;
                bi = i;
            }
        }
        candidates.remove(bi);
        featureData.getItemFeatures(bi).sequential()
                .map(Tuple2::v1)
                .forEach(f -> redundancy.addTo(f, 1));
        ideal += bg * disc.disc(rank);
        rank++;
    }

    return ideal;
}
 
Example #26
Source File: FastUtilObjectIntMapTest.java    From hashmapTest with The Unlicense 5 votes vote down vote up
@Override
public int test() {
    final Object2IntOpenHashMap<Integer> m_map = new Object2IntOpenHashMap<>( m_keys.length / 2 + 1, m_fillFactor );
    int add = 0, remove = 0;
    while ( add < m_keys.length )
    {
        m_map.put( m_keys[ add ], add );
        ++add;
        m_map.put( m_keys[ add ], add );
        ++add;
        m_map.removeInt( m_keys[ remove++ ] );
    }
    return m_map.size();
}
 
Example #27
Source File: AlphaNDCG.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) {
    UserRelevanceModel<U, I> urm = relModel.getModel(recommendation.getUser());

    double ndcg = 0.0;
    int rank = 0;
    Object2IntOpenHashMap<F> redundancy = new Object2IntOpenHashMap<>();
    redundancy.defaultReturnValue(0);

    for (Tuple2od<I> pair : recommendation.getItems()) {
        if (urm.isRelevant(pair.v1)) {
            double gain = featureData.getItemFeatures(pair.v1).sequential()
                    .map(Tuple2::v1)
                    .mapToDouble(f -> {
                        int r = redundancy.addTo(f, 1);
                        return Math.pow(1 - alpha, r);
                    }).sum();
            ndcg += gain * disc.disc(rank);
        }

        rank++;
        if (rank >= cutoff) {
            break;
        }
    }
    if (ndcg > 0) {
        ndcg /= idcg.getModel(recommendation.getUser()).ideal;
    }

    return ndcg;
}
 
Example #28
Source File: SymmetricImmutablePairTest.java    From liblevenshtein-java with MIT License 5 votes vote down vote up
@Test(dataProvider = "equivalentPairs")
public void testEquivalentPairs(
    final SymmetricImmutablePair<String> lhs,
    final SymmetricImmutablePair<String> rhs) {

  assertThat(lhs).isEqualByComparingTo(lhs);
  assertThat(rhs).isEqualByComparingTo(rhs);
  assertThat(lhs).isEqualByComparingTo(rhs);
  assertThat(rhs).isEqualByComparingTo(lhs);

  assertThat(lhs).isEqualTo(lhs);
  assertThat(rhs).isEqualTo(rhs);
  assertThat(lhs).isEqualTo(rhs);
  assertThat(rhs).isEqualTo(lhs);

  assertThat(lhs.hashCode()).isEqualTo(rhs.hashCode());

  Object2IntMap<SymmetricImmutablePair<String>> map;

  map = new Object2IntOpenHashMap<>(2);

  map.put(lhs, 1);
  assertThat(map).containsEntry(lhs, 1);
  assertThat(map).containsEntry(rhs, 1);

  map.put(rhs, 2);
  assertThat(map).containsEntry(rhs, 2);
  assertThat(map).containsEntry(lhs, 2);

  map = new Object2IntRBTreeMap<>();

  map.put(lhs, 1);
  assertThat(map).containsEntry(lhs, 1);
  assertThat(map).containsEntry(rhs, 1);

  map.put(rhs, 2);
  assertThat(map).containsEntry(rhs, 2);
  assertThat(map).containsEntry(lhs, 2);
}
 
Example #29
Source File: FastUtilObjectIntMapTest.java    From hashmapTest with The Unlicense 5 votes vote down vote up
@Override
public int test() {
    final Object2IntOpenHashMap<Integer> m_map = new Object2IntOpenHashMap<>( m_keys.length, m_fillFactor );
    for ( int i = 0; i < m_keys.length; ++i )
        m_map.put( m_keys[ i ], i );
    for ( int i = 0; i < m_keys2.length; ++i )
        m_map.put( m_keys2[ i ], i );
    return m_map.size();
}
 
Example #30
Source File: CategoricalColumn.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default Table countByCategory() {

    final Table t = new Table("Column: " + name());
    final CategoricalColumn<?> categories = (CategoricalColumn<?>) type().create("Category");
    final IntColumn counts = IntColumn.create("Count");

    final Object2IntMap<String> valueToCount = new Object2IntOpenHashMap<>();

    for (int i = 0; i < size(); i++) {
      if (!isMissing(i)) {
        final String next = getString(i);
        if (valueToCount.containsKey(next)) {
          valueToCount.put(next, valueToCount.getInt(next) + 1);
        } else {
          valueToCount.put(next, 1);
        }
      }
    }
    for (Map.Entry<String, Integer> entry : valueToCount.object2IntEntrySet()) {
      categories.appendCell(entry.getKey());
      counts.append(entry.getValue());
    }
    if (countMissing() > 0) {
      categories.appendMissing();
      counts.append(countMissing());
    }
    t.addColumns(categories);
    t.addColumns(counts);
    return t;
  }