it.unimi.dsi.fastutil.floats.FloatList Java Examples

The following examples show how to use it.unimi.dsi.fastutil.floats.FloatList. 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: MovieService.java    From jstarcraft-example with Apache License 2.0 6 votes vote down vote up
/**
 * 个性化搜索
 * 
 * @param userIndex
 * @param searchKey
 * @return
 * @throws Exception
 */
@LockableMethod(strategy = HashLockableStrategy.class)
public Object2FloatMap<MovieItem> getSearchItems(@LockableParameter int userIndex, String searchKey) throws Exception {
    // 标识-得分映射
    Object2FloatMap<MovieItem> item2ScoreMap = new Object2FloatOpenHashMap<>();

    long current = System.currentTimeMillis();
    Query query = queryParser.parse(searchKey, MovieItem.TITLE);
    KeyValue<List<Document>, FloatList> search = engine.retrieveDocuments(query, null, 0, 1000);
    List<Document> documents = search.getKey();
    FloatList scores = search.getValue();
    for (int index = 0, size = documents.size(); index < size; index++) {
        Document document = documents.get(index);
        MovieItem item = items.get(document.getField(MovieItem.INDEX).numericValue().intValue());
        float score = scores.getFloat(index);
        item2ScoreMap.put(item, score);
    }
    String message = StringUtility.format("搜索数量:{},搜索耗时:{}", documents.size(), System.currentTimeMillis() - current);
    logger.info(message);

    return item2ScoreMap;
}
 
Example #2
Source File: RatingTask.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
@Override
protected FloatList recommend(Model recommender, int userIndex) {
    ReferenceModule testModule = testModules[userIndex];
    ArrayInstance copy = new ArrayInstance(testMarker.getQualityOrder(), testMarker.getQuantityOrder());
    List<Integer2FloatKeyValue> rateList = new ArrayList<>(testModule.getSize());
    for (DataInstance instance : testModule) {
        copy.copyInstance(instance);
        recommender.predict(copy);
        rateList.add(new Integer2FloatKeyValue(copy.getQualityFeature(itemDimension), copy.getQuantityMark()));
    }

    FloatList recommendList = new FloatArrayList(rateList.size());
    for (Integer2FloatKeyValue keyValue : rateList) {
        recommendList.add(keyValue.getValue());
    }
    return recommendList;
}
 
Example #3
Source File: MPEEvaluator.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Override
protected float measure(FloatList checkCollection, FloatList rateList) {
    float value = 0F;
    Iterator<Float> iterator = checkCollection.iterator();
    for (float estimate : rateList) {
        float score = iterator.next();
        if (estimate > maximum) {
            estimate = maximum;
        }
        if (estimate < minimum) {
            estimate = minimum;
        }
        if (Math.abs(score - estimate) > mpe) {
            value++;
        }
    }
    return value;
}
 
Example #4
Source File: MAEEvaluator.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Override
protected float measure(FloatList checkCollection, FloatList rateList) {
    float value = 0F;
    Iterator<Float> iterator = checkCollection.iterator();
    for (float estimate : rateList) {
        float score = iterator.next();
        if (estimate > maximum) {
            estimate = maximum;
        }
        if (estimate < minimum) {
            estimate = minimum;
        }
        value += Math.abs(score - estimate);
    }
    return value;
}
 
Example #5
Source File: MSEEvaluator.java    From jstarcraft-ai with Apache License 2.0 6 votes vote down vote up
@Override
protected float measure(FloatList checkCollection, FloatList rateList) {
    float value = 0F;
    Iterator<Float> iterator = checkCollection.iterator();
    for (float estimate : rateList) {
        float score = iterator.next();
        if (estimate > maximum) {
            estimate = maximum;
        }
        if (estimate < minimum) {
            estimate = minimum;
        }
        value += Math.pow(score - estimate, 2);
    }
    return value;
}
 
Example #6
Source File: LuceneAccessor.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public <K extends Comparable, T extends IdentityObject<K>> List<T> queryUnion(Class<T> clazz, Map<String, Object> condition, StoragePagination pagination) {
	LuceneMetadata metadata = metadatas.get(clazz);
	Query query = null;
	BooleanQuery.Builder buffer = new BooleanQuery.Builder();
	for (Entry<String, Object> term : condition.entrySet()) {
		KeyValue<Field, IndexConverter> keyValue = metadata.getIndexKeyValue(term.getKey());
		Field key = keyValue.getKey();
		IndexConverter value = keyValue.getValue();
		query = value.query(context, term.getKey(), key, key.getAnnotation(LuceneIndex.class), key.getGenericType(), ConditionType.Equal, term.getValue());
		buffer.add(query, Occur.SHOULD);
	}
	query = buffer.build();
	int offset = pagination == null ? 0 : pagination.getFirst();
	int size = pagination == null ? Integer.MAX_VALUE : pagination.getSize();
	KeyValue<List<Document>, FloatList> retrieve = engine.retrieveDocuments(query, null, offset, size);
	List<Document> documents = retrieve.getKey();
	List<T> list = new ArrayList<>(BATCH_SIZE);
	for (Document document : documents) {
		list.add((T) metadata.decodeDocument(document));
	}
	return list;
}
 
Example #7
Source File: LuceneAccessor.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public <K extends Comparable, T extends IdentityObject<K>> List<T> queryIntersection(Class<T> clazz, Map<String, Object> condition, StoragePagination pagination) {
	LuceneMetadata metadata = metadatas.get(clazz);
	Query query = null;
	BooleanQuery.Builder buffer = new BooleanQuery.Builder();
	for (Entry<String, Object> term : condition.entrySet()) {
		KeyValue<Field, IndexConverter> keyValue = metadata.getIndexKeyValue(term.getKey());
		Field key = keyValue.getKey();
		IndexConverter value = keyValue.getValue();
		query = value.query(context, term.getKey(), key, key.getAnnotation(LuceneIndex.class), key.getGenericType(), ConditionType.Equal, term.getValue());
		buffer.add(query, Occur.MUST);
	}
	query = buffer.build();
	int offset = pagination == null ? 0 : pagination.getFirst();
	int size = pagination == null ? Integer.MAX_VALUE : pagination.getSize();
	KeyValue<List<Document>, FloatList> retrieve = engine.retrieveDocuments(query, null, offset, size);
	List<Document> documents = retrieve.getKey();
	List<T> list = new ArrayList<>(BATCH_SIZE);
	for (Document document : documents) {
		list.add((T) metadata.decodeDocument(document));
	}
	return list;
}
 
Example #8
Source File: LuceneAccessor.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public <K extends Comparable, I, T extends IdentityObject<K>> List<T> queryInstances(Class<T> clazz, String name, StorageCondition<I> condition) {
	LuceneMetadata metadata = metadatas.get(clazz);
	Query query;
	{
		KeyValue<Field, IndexConverter> keyValue = metadata.getIndexKeyValue(name);
		Field key = keyValue.getKey();
		IndexConverter value = keyValue.getValue();
		query = value.query(context, metadata.getPrimaryName(), key, key.getAnnotation(LuceneIndex.class), key.getGenericType(), condition.getType(), condition.getValues());
	}
	KeyValue<List<Document>, FloatList> retrieve = engine.retrieveDocuments(query, null, 0, Integer.MAX_VALUE);
	List<Document> documents = retrieve.getKey();
	List<T> list = new ArrayList<>(BATCH_SIZE);
	for (Document document : documents) {
		list.add((T) metadata.decodeDocument(document));
	}
	return list;
}
 
Example #9
Source File: MovieService.java    From jstarcraft-example with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param userIndex
 * @param modelKey
 * @param queryKey
 * @param filterClicked
 * @return
 * @throws Exception
 */
@LockableMethod(strategy = HashLockableStrategy.class)
public Object2FloatMap<MovieItem> getItems(@LockableParameter int userIndex, String modelKey, String queryKey, boolean filterClicked) throws Exception {
    // 标识-得分映射
    Object2FloatMap<MovieItem> item2ScoreMap = new Object2FloatOpenHashMap<>();

    long current = System.currentTimeMillis();
    Model model = models.get(modelKey);
    ArrayInstance instance = new ArrayInstance(qualityOrder, quantityOrder);
    MovieUser user = users.get(userIndex);
    Query query = StringUtility.isBlank(queryKey) ? new MatchAllDocsQuery() : queryParser.parse(queryKey, MovieItem.TITLE);
    KeyValue<List<Document>, FloatList> retrieve = engine.retrieveDocuments(query, null, 0, 1000);
    List<Document> documents = retrieve.getKey();
    for (int index = 0, size = documents.size(); index < size; index++) {
        Document document = documents.get(index);
        MovieItem item = items.get(document.getField(MovieItem.INDEX).numericValue().intValue());
        int itemIndex = item.getIndex();
        // 过滤条目
        if (filterClicked && user.isClicked(itemIndex)) {
            continue;
        }
        instance.setQualityFeature(userDimension, userIndex);
        instance.setQualityFeature(itemDimension, itemIndex);
        model.predict(instance);
        float score = instance.getQuantityMark();
        item2ScoreMap.put(item, score);
    }
    String message = StringUtility.format("预测数量:{},预测耗时:{}", modelKey, documents.size(), System.currentTimeMillis() - current);
    logger.info(message);

    return item2ScoreMap;
}
 
Example #10
Source File: ValueInTransformFunction.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
private static float[] filterFloats(FloatSet floatSet, float[] source) {
  FloatList floatList = new FloatArrayList();
  for (float value : source) {
    if (floatSet.contains(value)) {
      floatList.add(value);
    }
  }
  if (floatList.size() == source.length) {
    return source;
  } else {
    return floatList.toFloatArray();
  }
}
 
Example #11
Source File: LuceneEngine.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
/**
 * 检索文档
 * 
 * @param query
 * @param sort
 * @param offset
 * @param size
 * @return
 */
public KeyValue<List<Document>, FloatList> retrieveDocuments(Query query, Sort sort, int offset, int size) {
    try {
        readLock.lock();
        lockRead();
        synchronized (this.semaphore) {
            if (this.transienceManager.isChanged() || this.persistenceManager.isChanged()) {
                this.searcher = new LuceneSearcher(this.transienceManager, this.persistenceManager);
            }
        }
        ScoreDoc[] search = null;
        int begin = offset;
        int end = offset + size;
        if (sort == null) {
            search = this.searcher.search(query, end).scoreDocs;
        } else {
            search = this.searcher.search(query, end, sort).scoreDocs;
        }
        end = search.length;
        size = end - begin;
        size = size < 0 ? 0 : size;
        ArrayList<Document> documents = new ArrayList<>(size);
        FloatList scores = new FloatArrayList(size);
        for (int index = begin; index < end; index++) {
            ScoreDoc score = search[index];
            Document document = this.searcher.doc(score.doc);
            documents.add(document);
            scores.add(score.score);
        }
        return new KeyValue<>(documents, scores);
    } catch (Exception exception) {
        throw new StorageException(exception);
    } finally {
        unlockRead();
        readLock.unlock();
    }
}
 
Example #12
Source File: LuceneAccessor.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public <K extends Comparable, T extends IdentityObject<K>> List<T> queryInstances(Class<T> clazz, StoragePagination pagination) {
	LuceneMetadata metadata = metadatas.get(clazz);
	Query query = new MatchAllDocsQuery();
	int offset = pagination == null ? 0 : pagination.getFirst();
	int size = pagination == null ? Integer.MAX_VALUE : pagination.getSize();
	KeyValue<List<Document>, FloatList> retrieve = engine.retrieveDocuments(query, null, offset, size);
	List<Document> documents = retrieve.getKey();
	List<T> list = new ArrayList<>(BATCH_SIZE);
	for (Document document : documents) {
		list.add((T) metadata.decodeDocument(document));
	}
	return list;
}
 
Example #13
Source File: LuceneAccessor.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public <K extends Comparable, T extends IdentityObject<K>> T getInstance(Class<T> clazz, K id) {
	LuceneMetadata metadata = metadatas.get(clazz);
	KeyValue<Field, IndexConverter> keyValue = metadata.getIndexKeyValue(metadata.getPrimaryName());
	Field key = keyValue.getKey();
	IndexConverter value = keyValue.getValue();
	Query query = value.query(context, metadata.getPrimaryName(), key, key.getAnnotation(LuceneIndex.class), key.getGenericType(), ConditionType.Equal, id);
	KeyValue<List<Document>, FloatList> retrieve = engine.retrieveDocuments(query, null, 0, 100);
	List<Document> documents = retrieve.getKey();
	if (documents.size() > 0) {
		return (T) metadata.decodeDocument(documents.get(0));
	} else {
		return null;
	}
}
 
Example #14
Source File: AbstractRatingEvaluatorTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
protected FloatList getRight(MathVector vector) {
    FloatList recommendList = new FloatArrayList(vector.getElementSize());
    for (VectorScalar scalar : vector) {
        if (RandomUtility.randomFloat(1F) < 0.5F) {
            recommendList.add(scalar.getValue());
        } else {
            recommendList.add(scalar.getValue() * 0.5F);
        }
    }
    return recommendList;
}
 
Example #15
Source File: AbstractRatingEvaluatorTestCase.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
protected FloatList getLeft(MathVector vector) {
    FloatList scoreList = new FloatArrayList(vector.getElementSize());
    for (VectorScalar scalar : vector) {
        scoreList.add(scalar.getValue());
    }
    return scoreList;
}
 
Example #16
Source File: RatingTask.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
@Override
protected FloatList check(int userIndex) {
    ReferenceModule testModule = testModules[userIndex];
    FloatList scoreList = new FloatArrayList(testModule.getSize());
    for (DataInstance instance : testModule) {
        scoreList.add(instance.getQuantityMark());
    }
    return scoreList;
}
 
Example #17
Source File: MAEEvaluatorTestCase.java    From jstarcraft-ai with Apache License 2.0 4 votes vote down vote up
@Override
protected Evaluator<FloatList, FloatList> getEvaluator(SparseMatrix featureMatrix) {
    return new MAEEvaluator(0F, 1F);
}
 
Example #18
Source File: MPEEvaluatorTestCase.java    From jstarcraft-ai with Apache License 2.0 4 votes vote down vote up
@Override
protected Evaluator<FloatList, FloatList> getEvaluator(SparseMatrix featureMatrix) {
    return new MPEEvaluator(0F, 1F, 0.01F);
}
 
Example #19
Source File: MSEEvaluatorTestCase.java    From jstarcraft-ai with Apache License 2.0 4 votes vote down vote up
@Override
protected Evaluator<FloatList, FloatList> getEvaluator(SparseMatrix featureMatrix) {
    return new MSEEvaluator(0F, 1F);
}
 
Example #20
Source File: RatingEvaluator.java    From jstarcraft-ai with Apache License 2.0 4 votes vote down vote up
@Override
protected int count(FloatList checkCollection, FloatList rateList) {
    return checkCollection.size();
}