org.lenskit.data.dao.Query.stream()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(5.6k)|赞(0)|评价(0)|浏览(82)

本文整理了Java中org.lenskit.data.dao.Query.stream方法的一些代码示例,展示了Query.stream的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query.stream方法的具体详情如下:
包路径:org.lenskit.data.dao.Query
类名称:Query
方法名:stream

Query.stream介绍

[英]Stream the results of this query.
[中]流式处理此查询的结果。

代码示例

代码示例来源:origin: lenskit/lenskit

@Override
  public String get() {
    String name = fileName;
    if (name == null) {
      name = "ratings-" + UUID.randomUUID().toString() + ".csv";
    }
    logger.info("writing ratings to {}", name);
    File file = new File(workingDir, name);
    try (PrintWriter writer = new PrintWriter(file, CHARSET_UTF_8);
       ObjectStream<Rating> ratings = dao.query(Rating.class).stream()) {
      for (Rating r: ratings) {
        writer.printf("%d,%d,", r.getUserId(), r.getItemId());
        writer.print(r.getValue());
        writer.print(",");
        long ts = r.getTimestamp();
        if (ts >= 0) {
          writer.print(ts);
        }
        writer.println();
      }
    } catch (IOException e) {
      throw new ExternalProcessException("Error creating ratings file", e);
    }
    return name;
  }
}

代码示例来源:origin: lenskit/lenskit

@Override
  public GlobalBiasModel get() {
    double sum = 0;
    int n = 0;
    try (ObjectStream<Rating> ratings = dao.query(Rating.class).stream()) {
      for (Rating r: ratings) {
        sum += r.getValue();
        n += 1;
      }
    }
    double mean = n > 0 ? sum / n : 0;
    return new GlobalBiasModel(mean);
  }
}

代码示例来源:origin: lenskit/lenskit

@Override
public void crossfold(DataAccessObject input, CrossfoldOutput output, EntityType type) throws IOException {
  final int count = output.getCount();
  logger.info("splitting {} data from {} to {} partitions by ratings", type, input, count);
  // make a list ourselves so we can shuffle it, makeList lists are immutable
  List<Rating> ratings;
  try (ObjectStream<Rating> stream = input.query(type).asType(Rating.class).stream()) {
    ratings = Lists.newArrayList(stream);
  }
  Collections.shuffle(ratings);
  final int n = ratings.size();
  for (int i = 0; i < n; i++) {
    for (int f = 0; f < count; f++) {
      int foldNum = i % count;
      if (f == foldNum) {
        output.getTestWriter(f).writeRating(ratings.get(i));
      } else {
        output.getTrainWriter(f).writeRating(ratings.get(i));
      }
    }
  }
}

代码示例来源:origin: lenskit/lenskit

@Nonnull
@Override
public Long2DoubleMap userRatingVector(long user) {
  IdBox<Long2DoubleMap> cached = cachedValue;
  if (cached != null && cached.getId() == user) {
    return cached.getValue();
  }
  Long2DoubleMap map;
  try (ObjectStream<Rating> stream = dao.query(Rating.class)
                     .withAttribute(CommonAttributes.USER_ID, user)
                     .stream()) {
    map = Ratings.userRatingVector(stream);
  }
  return map;
}

代码示例来源:origin: lenskit/lenskit

@Override
public void crossfold(DataAccessObject input, CrossfoldOutput output, EntityType type) throws IOException {
  final int count = output.getCount();
  logger.info("splitting {} data from {} to {} partitions by ratings", type, input, count);
  // make a list ourselves so we can shuffle it, makeList lists are immutable
  List<Rating> ratings;
  try (ObjectStream<Rating> stream = input.query(type).asType(Rating.class).stream()) {
    ratings = Lists.newArrayList(stream);
  }
  Collections.shuffle(ratings);
  final int n = ratings.size();
  for (int i = 0; i < count; i++) {
    int start = i * sampleSize;
    int stop = (i + 1) * sampleSize;
    RatingWriter trainWriter = output.getTrainWriter(i);
    RatingWriter testWriter = output.getTestWriter(i);
    for (int j = 0; j < n; j++) {
      Rating r = ratings.get(j);
      if (j < start) {
        trainWriter.writeRating(r);
      } else if (j < stop) {
        testWriter.writeRating(r);
      } else {
        trainWriter.writeRating(r);
      }
    }
  }
}

代码示例来源:origin: lenskit/lenskit

@Override
  public InteractionStatistics get() {
    Long2IntOpenHashMap counts = new Long2IntOpenHashMap();
    try (ObjectStream<Entity> stream = dao.query(entityType).stream()) {
      for (Entity e : stream) {
        long item = e.getLong(CommonAttributes.ITEM_ID);
        counts.addTo(item, 1);
      }
    }
    return new InteractionStatistics(entityType, counts);
  }
}

代码示例来源:origin: lenskit/lenskit

@Override
  public InteractionStatistics get() {
    Long2IntOpenHashMap counts = new Long2IntOpenHashMap();
    try (ObjectStream<Entity> stream = dao.query(entityType).stream()) {
      for (Entity e : stream) {
        long item = e.getLong(CommonAttributes.ITEM_ID);
        counts.addTo(item, e.getInteger(CommonAttributes.COUNT));
      }
    }
    return new InteractionStatistics(entityType, counts);
  }
}

代码示例来源:origin: lenskit/lenskit

int totalCount = 0;
try (ObjectStream<Rating> ratings = dao.query(Rating.class).stream()) {
  for (Rating r: ratings) {
    assert sums.size() == counts.size();

代码示例来源:origin: org.lenskit/lenskit-core

@Override
  public GlobalBiasModel get() {
    double sum = 0;
    int n = 0;
    try (ObjectStream<Rating> ratings = dao.query(Rating.class).stream()) {
      for (Rating r: ratings) {
        sum += r.getValue();
        n += 1;
      }
    }
    double mean = n > 0 ? sum / n : 0;
    return new GlobalBiasModel(mean);
  }
}

代码示例来源:origin: org.lenskit/lenskit-core

@Nonnull
@Override
public Long2DoubleMap userRatingVector(long user) {
  IdBox<Long2DoubleMap> cached = cachedValue;
  if (cached != null && cached.getId() == user) {
    return cached.getValue();
  }
  Long2DoubleMap map;
  try (ObjectStream<Rating> stream = dao.query(Rating.class)
                     .withAttribute(CommonAttributes.USER_ID, user)
                     .stream()) {
    map = Ratings.userRatingVector(stream);
  }
  return map;
}

代码示例来源:origin: org.lenskit/lenskit-core

@Override
  public InteractionStatistics get() {
    Long2IntOpenHashMap counts = new Long2IntOpenHashMap();
    try (ObjectStream<Entity> stream = dao.query(entityType).stream()) {
      for (Entity e : stream) {
        long item = e.getLong(CommonAttributes.ITEM_ID);
        counts.addTo(item, 1);
      }
    }
    return new InteractionStatistics(entityType, counts);
  }
}

代码示例来源:origin: org.lenskit/lenskit-core

@Override
  public InteractionStatistics get() {
    Long2IntOpenHashMap counts = new Long2IntOpenHashMap();
    try (ObjectStream<Entity> stream = dao.query(entityType).stream()) {
      for (Entity e : stream) {
        long item = e.getLong(CommonAttributes.ITEM_ID);
        counts.addTo(item, e.getInteger(CommonAttributes.COUNT));
      }
    }
    return new InteractionStatistics(entityType, counts);
  }
}

代码示例来源:origin: org.lenskit/lenskit-core

int totalCount = 0;
try (ObjectStream<Rating> ratings = dao.query(Rating.class).stream()) {
  for (Rating r: ratings) {
    assert sums.size() == counts.size();

相关文章