@Override public void add(Entity e) { Preconditions.checkState(entityLists != null, "build() already called"); if (e.hasAttribute(attributeName)) { long value = e.getLong(attributeName); ImmutableList.Builder<Entity> lb = entityLists.get(value); if (lb == null) { lb = ImmutableList.builder(); entityLists.put(value, lb); } lb.add(e); } }
@Override public Map<Long,List<Entity>> grouped(TypedName<Long> attr) { Preconditions.checkArgument(attr != CommonAttributes.ENTITY_ID, "cannot group by entity ID"); EntityIndex idx = indexes.get(attr.getName()); if (idx == null) { return store.values() .stream() .filter(e -> e.hasAttribute(attr)) .collect(Collectors.groupingBy(e -> e.getLong(attr))); } else { return idx.getValues() .stream() .collect(Collectors.toMap(l -> (Long) l, idx::getEntities)); } }
@Override public Map<Long, List<Entity>> grouped(TypedName<Long> attr) { Preconditions.checkArgument(attr != CommonAttributes.ENTITY_ID, "cannot group by entity ID"); int idx = attributes.lookup(attr); if (idx < 0) { return Collections.emptyMap(); } PackIndex index = indexes[idx]; if (index != null) { return index.getValues() .stream() .collect(Collectors.toMap(l -> (Long) l, l -> new EntityList(index.getPositions(l)))); } else { return stream() .filter(e -> e.hasAttribute(attr)) .collect(Collectors.groupingBy(e -> e.getLong(attr))); } }
public LongSet getTrainItems() { LongSet items = trainItems; if (items == null) { items = new LongOpenHashSet(); for (Entity e : trainHistory) { if(e.hasAttribute(CommonAttributes.ITEM_ID)) { items.add(e.getLong(CommonAttributes.ITEM_ID)); } } trainItems = items; } return items; }
public LongSet getTestItems() { LongSet items = testItems; if (items == null) { items = new LongOpenHashSet(); for (Entity e : testHistory) { if(e.hasAttribute(CommonAttributes.ITEM_ID)) { items.add(e.getLong(CommonAttributes.ITEM_ID)); } } testItems = items; } return items; }
@Override protected Long2DoubleMap makeVector(List<Entity> entities) { Long2DoubleOpenHashMap counts = new Long2DoubleOpenHashMap(); counts.defaultReturnValue(0); for (Entity e: entities) { long item = e.getLong(CommonAttributes.ITEM_ID); int count = e.getInteger(CommonAttributes.COUNT); counts.addTo(item, count); } return counts; } }
@Override protected Long2DoubleMap makeVector(List<Entity> entities) { Long2DoubleMap counts = new Long2DoubleOpenHashMap(); counts.defaultReturnValue(0); for (Entity e: entities) { long item = e.getLong(CommonAttributes.ITEM_ID); counts.put(item, counts.get(item) + 1); } return counts; } }
@Override public LongSet valueSet(TypedName<Long> attr) { LongSet values = new LongOpenHashSet(); try (ObjectStream<E> stream = stream()) { for (E entity: stream) { if (entity.hasAttribute(attr)) { values.add(entity.getLong(attr)); } } } return values; } }
/** * Derive bare entities from the values in another type of entity. This method only consults the entities added * so far, so it should be called *after* all other calls to {@link #addEntity(Entity)} and friends. If an entity * has already been added with the same type and ID as one of the derived entities, it is kept instead of the derived * entity. * * @param derived The derived entity type. * @param source The source entity type. * @param attr The source attribute. * @return The builder (for chaining). */ public EntityCollectionDAOBuilder deriveEntities(EntityType derived, EntityType source, TypedName<Long> attr) { EntityCollectionBuilder src = entitySets.get(source); if (src == null) { // no source entities, skip return this; } EntityCollectionBuilder ecb = entitySets.get(derived); if (ecb == null) { ecb = EntityCollection.newBareBuilder(derived); entitySets.put(derived, ecb); } for (Entity e: src.entities()) { if (e.hasAttribute(attr)) { long key = e.getLong(attr); ecb.add(Entities.create(derived, key), false); } } return this; }
@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); } }
@Nonnull @Override public MetricResult measureUserRecList(Recommender rec, TestUser user, int targetLength, List<Long> recommendations, Mean context) { if (recommendations == null) { return MetricResult.empty(); } Long2DoubleMap ratings = new Long2DoubleOpenHashMap(); for (Entity e: user.getTestHistory()) { long item = e.getLong(CommonAttributes.ITEM_ID); Object av = e.get(gainAttribute); if (av instanceof Number) { ratings.put(item, ((Number) av).doubleValue()); } else { throw new IllegalArgumentException("value " + av + " for attribute " + gainAttribute + " is not numeric"); } } List<Long> ideal = ratings.keySet() .stream() .sorted(LongUtils.keyValueComparator(ratings).reversed()) .limit(targetLength >= 0 ? targetLength : ratings.size()) .collect(Collectors.toList()); double idealGain = computeDCG(ideal, ratings); double gain = computeDCG(recommendations, ratings); double score = gain / idealGain; synchronized (context) { context.increment(score); } return MetricResult.singleton(columnName, score); }
@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); } }
@Override public void add(Entity e) { Preconditions.checkState(entityLists != null, "build() already called"); if (e.hasAttribute(attributeName)) { long value = e.getLong(attributeName); ImmutableList.Builder<Entity> lb = entityLists.get(value); if (lb == null) { lb = ImmutableList.builder(); entityLists.put(value, lb); } lb.add(e); } }
@Override protected Long2DoubleMap makeVector(List<Entity> entities) { Long2DoubleMap counts = new Long2DoubleOpenHashMap(); counts.defaultReturnValue(0); for (Entity e: entities) { long item = e.getLong(CommonAttributes.ITEM_ID); counts.put(item, counts.get(item) + 1); } return counts; } }
@Override protected Long2DoubleMap makeVector(List<Entity> entities) { Long2DoubleOpenHashMap counts = new Long2DoubleOpenHashMap(); counts.defaultReturnValue(0); for (Entity e: entities) { long item = e.getLong(CommonAttributes.ITEM_ID); int count = e.getInteger(CommonAttributes.COUNT); counts.addTo(item, count); } return counts; } }
@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); } }
@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); } }