本文整理了Java中org.lenskit.data.dao.Query
类的一些代码示例,展示了Query
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query
类的具体详情如下:
包路径:org.lenskit.data.dao.Query
类名称:Query
[英]Fluent interface for DAO queries. This interface is immutable - new objects are always returned - so it is safe to save partial queries and use them to build up more sophisticated queries. An implementation on top of the base DAO methods is used by AbstractDataAccessObject. Other DAO implementations can reimplement this interface in other ways, for example to generate SQL queries.
[中]流畅的DAO查询界面。这个接口是不可变的——总是会返回新对象——所以保存部分查询并使用它们构建更复杂的查询是安全的。AbstractDataAccessObject使用基本DAO方法之上的实现。其他DAO实现可以用其他方式重新实现这个接口,例如生成SQL查询。
代码示例来源:origin: lenskit/lenskit
/**
* Extract a user vector from a data source.
*
* @param uid The user ID.
* @param dao The DAO.
* @return The user rating vector.
*/
private Long2DoubleMap makeUserVector(long uid, DataAccessObject dao) {
List<Rating> history = dao.query(Rating.class)
.withAttribute(CommonAttributes.USER_ID, uid)
.get();
Long2DoubleMap vector = null;
if (!history.isEmpty()) {
vector = Ratings.userRatingVector(history);
}
return vector;
}
代码示例来源:origin: lenskit/lenskit
@Override
public ObjectStream<IdBox<Long2DoubleMap>> streamUsers() {
ObjectStream<IdBox<List<Rating>>> stream = dao.query(Rating.class)
.groupBy(CommonAttributes.USER_ID)
.stream();
return ObjectStreams.wrap(stream.map(u -> u.mapValue(Ratings::userRatingVector)),
stream);
}
}
代码示例来源:origin: lenskit/lenskit
/**
* Get the default exclude set for a user. The base implementation gets
* all the items they have interacted with.
*
* @param user The user ID.
* @return The set of items to exclude.
*/
protected LongSet getDefaultExcludes(long user) {
// FIXME Support things other than ratings
return dao.query(CommonTypes.RATING)
.withAttribute(CommonAttributes.USER_ID, user)
.valueSet(CommonAttributes.ITEM_ID);
}
代码示例来源: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
.orderBy(CommonAttributes.ENTITY_ID)
.get();
assertThat(built, hasSize(ratings.size()));
assertThat(built, equalTo(sorted));
.collect(Collectors.toList());
List<Rating> fromDAO = dao.query(Rating.class)
.withAttribute(CommonAttributes.USER_ID, user)
.orderBy(CommonAttributes.TIMESTAMP)
.get();
assertThat(fromDAO, equalTo(fromData));
.collect(Collectors.toList());
List<Rating> fromDAO = dao.query(Rating.class)
.withAttribute(CommonAttributes.ITEM_ID, item)
.orderBy(CommonAttributes.TIMESTAMP)
.get();
assertThat(fromDAO, equalTo(fromData));
代码示例来源: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
.orderBy(CommonAttributes.TIMESTAMP)
.get();
代码示例来源:origin: lenskit/lenskit
@Test
public void testGroupEntitiesFluently() {
List<Entity> entities = new ArrayList<>();
entities.add(Entities.newBuilder(LIKE, 1)
.setAttribute(CommonAttributes.USER_ID, 42L)
.setAttribute(CommonAttributes.ITEM_ID, 39L)
.build());
entities.add(Entities.newBuilder(LIKE, 2)
.setAttribute(CommonAttributes.USER_ID, 67L)
.setAttribute(CommonAttributes.ITEM_ID, 28L)
.build());
entities.add(Entities.newBuilder(LIKE, 3)
.setAttribute(CommonAttributes.USER_ID, 42L)
.setAttribute(CommonAttributes.ITEM_ID, 28L)
.build());
EntityCollectionDAO dao = EntityCollectionDAO.create(entities);
EntityQuery<Entity> query = EntityQuery.newBuilder(LIKE)
.addSortKey(CommonAttributes.ITEM_ID)
.build();
List<IdBox<List<Entity>>> results = dao.query(LIKE)
.orderBy(CommonAttributes.ITEM_ID)
.groupBy(CommonAttributes.USER_ID).get();
assertThat(results, hasSize(2));
assertThat(results,
containsInAnyOrder(IdBox.create(42L, (List) ImmutableList.of(entities.get(2), entities.get(0))),
IdBox.create(67L, ImmutableList.of(entities.get(1)))));
}
代码示例来源:origin: lenskit/lenskit
splits.defaultReturnValue(-1); // unpartitioned users should only be trained
try (ObjectStream<IdBox<List<Rating>>> userStream = input.query(type)
.asType(Rating.class)
.groupBy(groupAttribute)
.stream()) {
for (IdBox<List<Rating>> history : userStream) {
代码示例来源:origin: lenskit/lenskit
pctFormat.setMaximumFractionDigits(2);
pctFormat.setMinimumFractionDigits(2);
final int nusers = testData.query(CommonTypes.USER).count();
test.start(nusers);
logger.info("Testing {} on {} ({} users)", algorithm, dataSet, nusers);
List<Entity> users = testData.query(CommonTypes.USER).get();
Stream<Entity> userStream;
if (inForkJoinPool()) {
代码示例来源: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
assertThat(dao.getEntityTypes(), containsInAnyOrder(ets));
List<Entity> ratings = dao.query(CommonTypes.RATING).get();
assertThat(ratings, hasSize(2));
assertThat(dao.query(CommonTypes.USER).get(),
containsInAnyOrder(Entities.create(CommonTypes.USER, 10),
Entities.create(CommonTypes.USER, 11)));
assertThat(dao.query(CommonTypes.ITEM).get(),
contains(Entities.create(CommonTypes.ITEM, 20)));
.get();
assertThat(rlist, hasSize(2));
assertThat(rlist, (Matcher) equalTo(ratings));
代码示例来源:origin: lenskit/lenskit
.orderBy(CommonAttributes.ENTITY_ID)
.get();
assertThat(built, hasSize(ratings.size()));
assertThat(built, equalTo(sorted));
.collect(Collectors.toList());
List<Rating> fromDAO = dao.query(Rating.class)
.withAttribute(CommonAttributes.USER_ID, user)
.orderBy(CommonAttributes.TIMESTAMP)
.get();
assertThat(fromDAO, equalTo(fromData));
.collect(Collectors.toList());
List<Rating> fromDAO = dao.query(Rating.class)
.withAttribute(CommonAttributes.ITEM_ID, item)
.orderBy(CommonAttributes.TIMESTAMP)
.get();
assertThat(fromDAO, equalTo(fromData));
代码示例来源: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: 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 < 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
@Test
public void testAddEntityLayout() {
EntityCollectionDAOBuilder b = EntityCollectionDAO.newBuilder();
b.addEntityLayout(Rating.ENTITY_TYPE, Rating.ATTRIBUTES);
Rating r = Rating.newBuilder()
.setId(42)
.setUserId(100)
.setItemId(50)
.setRating(3.5)
.setTimestamp(1034801)
.build();
b.addEntity(r);
EntityCollectionDAO dao = b.build();
assertThat(dao.getEntityIds(CommonTypes.RATING), contains(42L));
assertThat(dao.streamEntities(CommonTypes.RATING)
.collect(Collectors.toList()),
contains(r));
assertThat(dao.query(Rating.class)
.get(),
contains(r));
}
代码示例来源: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;
List<Entity> entities = dao.query(getEntityType())
.withAttribute(CommonAttributes.USER_ID, user)
.get();
map = makeVector(entities);
cachedValue = IdBox.create(user, map);
return map;
}
代码示例来源:origin: lenskit/lenskit
@Override
protected LongList recommend(long user, int n, @Nullable LongSet candidates, @Nullable LongSet exclude) {
if (exclude == null) {
exclude = data.query(statistics.getEntityType())
.withAttribute(CommonAttributes.USER_ID, user)
.valueSet(CommonAttributes.ITEM_ID);
}
return recommendWithSets(n, candidates, exclude);
}
代码示例来源:origin: lenskit/lenskit
@Override
public ObjectStream<IdBox<Long2DoubleMap>> streamUsers() {
ObjectStream<IdBox<List<Entity>>> stream = dao.query(getEntityType())
.groupBy(CommonAttributes.USER_ID)
.stream();
return ObjectStreams.transform(stream, new Function<IdBox<List<Entity>>, IdBox<Long2DoubleMap>>() {
@Nullable
@Override
public IdBox<Long2DoubleMap> apply(@Nullable IdBox<List<Entity>> input) {
if (input == null) {
return null;
}
return IdBox.create(input.getId(),
makeVector(input.getValue()));
}
});
}
}
内容来源于网络,如有侵权,请联系作者删除!