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

The following examples show how to use it.unimi.dsi.fastutil.objects.Object2FloatMap. 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: MovieItemOutput.java    From jstarcraft-example with Apache License 2.0 5 votes vote down vote up
public static List<MovieItemOutput> instancesOf(Object2FloatMap<MovieItem> items) {
    List<MovieItemOutput> instances = new ArrayList<>(items.size());
    for (Object2FloatMap.Entry<MovieItem> term : items.object2FloatEntrySet()) {
        MovieItemOutput instance = new MovieItemOutput(term.getKey(), term.getFloatValue());
        instances.add(instance);
    }
    return instances;
}
 
Example #3
Source File: MovieController.java    From jstarcraft-example with Apache License 2.0 5 votes vote down vote up
/**
 * 获取推荐条目
 * 
 * @param userIndex
 * @param recommendKey
 * @return
 */
@ApiOperation(value = "获取推荐条目", notes = "获取推荐条目")
@GetMapping("/getRecommendItems")
public NormalOutput<List<MovieItemOutput>> getRecommendItems(@RequestParam int userIndex, @RequestParam String recommendKey) {
    Object2FloatMap<MovieItem> movies = movieService.getRecommendItems(userIndex, recommendKey);
    List<MovieItemOutput> instances = MovieItemOutput.instancesOf(movies);
    Collections.sort(instances, (left, right) -> {
        return Float.compare(right.getScore(), left.getScore());
    });
    return new NormalOutput<>(instances);
}
 
Example #4
Source File: MovieController.java    From jstarcraft-example with Apache License 2.0 5 votes vote down vote up
/**
 * 获取搜索条目
 * 
 * @param userIndex
 * @param searchKey
 * @return
 */
@ApiOperation(value = "获取搜索条目", notes = "获取搜索条目")
@GetMapping("/getSearchItems")
public NormalOutput<List<MovieItemOutput>> getSearchItems(@RequestParam int userIndex, @RequestParam String searchKey) throws Exception {
    Object2FloatMap<MovieItem> movies = movieService.getSearchItems(userIndex, searchKey);
    List<MovieItemOutput> instances = MovieItemOutput.instancesOf(movies);
    Collections.sort(instances, (left, right) -> {
        return Float.compare(right.getScore(), left.getScore());
    });
    return new NormalOutput<>(instances);
}
 
Example #5
Source File: MovieController.java    From jstarcraft-example with Apache License 2.0 5 votes vote down vote up
/**
 * 获取条目
 * 
 * @param userIndex
 * @param recommendKey
 * @param searchKey
 * @param filterClicked
 * @return
 * @throws Exception
 */
@ApiOperation(value = "获取条目", notes = "获取条目")
@GetMapping("/getItems")
public NormalOutput<List<MovieItemOutput>> getItems(@RequestParam int userIndex, @RequestParam String modelKey, @RequestParam String queryKey, @RequestParam boolean filterClicked) throws Exception {
    Object2FloatMap<MovieItem> movies = movieService.getItems(userIndex, modelKey, queryKey, filterClicked);
    List<MovieItemOutput> instances = MovieItemOutput.instancesOf(movies);
    Collections.sort(instances, (left, right) -> {
        return Float.compare(right.getScore(), left.getScore());
    });
    return new NormalOutput<>(instances);
}
 
Example #6
Source File: MovieService.java    From jstarcraft-example with Apache License 2.0 5 votes vote down vote up
/**
 * 个性化推荐
 * 
 * @param userIndex
 * @param recommendKey
 * @return
 */
@LockableMethod(strategy = HashLockableStrategy.class)
public Object2FloatMap<MovieItem> getRecommendItems(@LockableParameter int userIndex, String recommendKey) {
    // 标识-得分映射
    Object2FloatMap<MovieItem> item2ScoreMap = new Object2FloatOpenHashMap<>();

    long current = System.currentTimeMillis();
    Model model = models.get(recommendKey);
    ArrayInstance instance = new ArrayInstance(qualityOrder, quantityOrder);
    MovieUser user = users.get(userIndex);
    int itemSize = items.size();
    for (int itemIndex = 0; itemIndex < itemSize; itemIndex++) {
        // 过滤条目
        if (user.isClicked(itemIndex)) {
            continue;
        }
        instance.setQualityFeature(userDimension, userIndex);
        instance.setQualityFeature(itemDimension, itemIndex);
        model.predict(instance);
        MovieItem item = items.get(itemIndex);
        float score = instance.getQuantityMark();
        item2ScoreMap.put(item, score);
    }
    String message = StringUtility.format("推荐数量:{},推荐耗时:{}", itemSize, System.currentTimeMillis() - current);
    logger.info(message);

    return item2ScoreMap;
}
 
Example #7
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;
}