@Nullable @Override public T apply(@Nullable Entity input) { return input == null ? null : input.maybeGet(name); } };
/** * Create a new entity builder that is initialized with a copy of an entity. * @param e The entity. * @return An entity builder initialized to build a copy of {@code e}. */ public static EntityBuilder copyBuilder(Entity e) { EntityBuilder eb = newBuilder(e.getType(), e.getId()); for (TypedName a: e.getTypedAttributeNames()) { eb.setAttribute(a, e.get(a)); } return eb; }
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; }
Entity first = stream.readObject(); assertThat(first, instanceOf(Rating.class)); assertThat(first.getType(), equalTo(EntityType.forName("rating"))); assertThat(first.getId(), equalTo(1L)); assertThat(first.get(CommonAttributes.ENTITY_ID), equalTo(1L)); assertThat(first.get(CommonAttributes.USER_ID), equalTo(10L)); assertThat(first.get(CommonAttributes.ITEM_ID), equalTo(20L)); assertThat(first.get(CommonAttributes.RATING), equalTo(3.5)); assertThat(first.hasAttribute(CommonAttributes.TIMESTAMP), equalTo(false)); assertThat(second.getType(), equalTo(EntityType.forName("rating"))); assertThat(second.getId(), equalTo(2L)); assertThat(second.get(CommonAttributes.ENTITY_ID), equalTo(2L)); assertThat(second.get(CommonAttributes.USER_ID), equalTo(11L)); assertThat(second.get(CommonAttributes.ITEM_ID), equalTo(20L)); assertThat(second.get(CommonAttributes.RATING), equalTo(4.0)); assertThat(second.hasAttribute(CommonAttributes.TIMESTAMP), equalTo(false)); assertThat(((Rating) second).getUserId(),
@Test public void testConfigureReader() throws IOException { ObjectReader reader = new ObjectMapper().reader(); JsonNode json = reader.readTree("{\"entity_type\": \"item\"}"); EntityFormat fmt = JSONEntityFormat.fromJSON(null, ClassLoaders.inferDefault(), json); assertThat(fmt.getEntityType(), equalTo(CommonTypes.ITEM)); LineEntityParser lep = fmt.makeParser(Collections.EMPTY_LIST); Entity res = lep.parse("{\"id\": 204, \"name\": \"hamster\", \"extra\": \"wumpus\"}"); assertThat(res, notNullValue()); assertThat(res.getId(), equalTo(204L)); assertThat(res.get(CommonAttributes.NAME), equalTo("hamster")); assertThat(res.get("extra"), (Matcher) equalTo("wumpus")); }
@Test public void testThingNullField() { JSONEntityFormat fmt = new JSONEntityFormat(); fmt.setEntityType(CommonTypes.ITEM); fmt.addAttribute(CommonAttributes.ENTITY_ID); fmt.addAttribute("title", CommonAttributes.NAME); LineEntityParser lep = fmt.makeParser(Collections.EMPTY_LIST); Entity res = lep.parse("{\"id\": 204, \"title\": null, \"extra\": \"wumpus\"}"); assertThat(res, notNullValue()); assertThat(res.getId(), equalTo(204L)); assertThat(res.maybeGet(CommonAttributes.NAME), nullValue()); assertThat(res.hasAttribute("extra"), equalTo(false)); }
/** * Get the ID of this user. * * @return The user's ID. */ public long getUserId() { return user.getId(); }
@Test public void testThingFields() { JSONEntityFormat fmt = new JSONEntityFormat(); fmt.setEntityType(CommonTypes.ITEM); fmt.addAttribute(CommonAttributes.ENTITY_ID); fmt.addAttribute("title", CommonAttributes.NAME); LineEntityParser lep = fmt.makeParser(Collections.EMPTY_LIST); Entity res = lep.parse("{\"id\": 204, \"title\": \"hamster\", \"extra\": \"wumpus\"}"); assertThat(res, notNullValue()); assertThat(res.getId(), equalTo(204L)); assertThat(res.get(CommonAttributes.NAME), equalTo("hamster")); assertThat(res.hasAttribute("extra"), equalTo(false)); }
/** * Determine whether this query matches the specified entity. * @param entity The entity to test. * @return The entity. */ public boolean matches(Entity entity) { if (!entity.getType().equals(entityType)) { return false; } for (Attribute<?> attr: filterFields) { Object val = entity.maybeGet(attr.getTypedName()); if (!attr.getValue().equals(val)) { return false; } } return true; } }
@Override public EntityCollectionBuilder add(Entity e, boolean replace) { Preconditions.checkState(store != null, "build() already called"); Preconditions.checkArgument(e.getType().equals(type)); if (!replace && store.containsKey(e.getId())) { return this; } store.add(e); hasher.putInt(e.hashCode()); for (EntityIndexBuilder ib: indexBuilders.values()) { ib.add(e); } return this; }
@Test public void testConfigureItemJSON() throws IOException, URISyntaxException { URI baseURI = TextEntitySourceTest.class.getResource("header-ratings.csv").toURI(); JsonNode node = reader.readTree("{\"file\": \"items.json\", \"format\": \"json\", \"entity_type\": \"item\"}"); TextEntitySource fr = TextEntitySource.fromJSON("test", node, baseURI); try (ObjectStream<Entity> stream = fr.openStream()) { Entity first = stream.readObject(); assertThat(first.getType(), equalTo(CommonTypes.ITEM)); assertThat(first.getId(), equalTo(42L)); assertThat(first.get(CommonAttributes.ENTITY_ID), equalTo(42L)); assertThat(first.get(CommonAttributes.NAME), equalTo("woozle")); Entity second = stream.readObject(); assertThat(second.getType(), equalTo(CommonTypes.ITEM)); assertThat(second.getId(), equalTo(37L)); assertThat(second.get(CommonAttributes.ENTITY_ID), equalTo(37L)); assertThat(second.get(CommonAttributes.NAME), equalTo("heffalump")); assertThat(stream.readObject(), nullValue()); } }
@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; } }
LongList items = null; logger.debug("generating recommendations for user {}, item {}", testUser.getUserId(), te.maybeGet(CommonAttributes.ITEM_ID)); if (useDetails) { results = itemRecommender.recommendWithDetails(tu2.getUserId(), n, ArrayList<Object> row = new ArrayList<>(); row.add(tu2.getUserId()); row.add(te.getLong(CommonAttributes.ITEM_ID)); while (il != null && row.size() < il.getColumnCount()) { row.add(null); writeRecommendations(tu2, te.getLong(CommonAttributes.ITEM_ID), results);
@Override public boolean apply(@Nullable Entity input) { return (input != null) && input.getType().equals(type); } };
@Override public EntityCollectionBuilder add(Entity e, boolean replace) { long id = e.getId(); isSorted &= id > lastEntityId; for (Attribute<?> a: e.getAttributes()) { int ap = attributes.lookup(a.getTypedName()); if (ap >= 0) {
@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); }
EntityBuilder builder = null; try { builder = ctor.get().newInstance(e.getType()); } catch (IllegalAccessException | InstantiationException ex) { throw new VerifyException(ctor.get() + " cannot be instantiated", ex); throw new UncheckedExecutionException("error invoking " + ctor.get(), ex); for (Attribute<?> attr: e.getAttributes()) { builder.setAttribute(attr);