@Nullable @Override public T apply(@Nullable Entity input) { return input == null ? null : input.maybeGet(name); } };
@Override public void add(Entity e) { Preconditions.checkState(builder != null, "build() already called"); Object value = e.maybeGet(attributeName); if (value != null) { builder.put(value, e); } }
@Override @Nonnull public List<Entity> find(String name, Object value) { Preconditions.checkNotNull(name, "attribute name"); Preconditions.checkNotNull(value, "attribute value"); EntityIndex index = indexes.get(name); if (index != null) { return index.getEntities(value); } // no index, go ahead and search ImmutableList.Builder<Entity> results = ImmutableList.builder(); for (Entity e: store) { if (value.equals(e.maybeGet(name))) { results.add(e); } } return results.build(); }
void predict(RatingPredictor pred, DataAccessObject dao, long user, List<Long> items, TableWriter outW) throws IOException { logger.info("predicting {} items", items.size()); Stopwatch timer = Stopwatch.createStarted(); Map<Long, Double> preds = pred.predict(user, items); System.out.format("predictions for user %d:%n", user); for (Map.Entry<Long, Double> e : preds.entrySet()) { if (outW != null) { outW.writeRow(user, e.getKey(), e.getValue()); } System.out.format(" %d", e.getKey()); Entity item = dao.lookupEntity(CommonTypes.ITEM, e.getKey()); String name = item == null ? null : item.maybeGet(CommonAttributes.NAME); if (name != null) { System.out.format(" (%s)", name); } System.out.format(": %.3f", e.getValue()); System.out.println(); } timer.stop(); logger.info("predicted for {} items in {}", items.size(), timer); }
@Nonnull @Override public <T> List<Entity> find(TypedName<T> name, T value) { int idx = attributes.lookup(name); if (idx < 0) { return ImmutableList.of(); } PackIndex index = indexes[idx]; if (index != null) { return new EntityList(index.getPositions(value)); } else { return stream().filter(e -> value.equals(e.maybeGet(name))) .collect(Collectors.toList()); } }
@Override public void writeUser(long user, ResultList recs) throws IOException { generator.writeStartObject(); generator.writeNumberField("user", user); generator.writeArrayFieldStart("recommendations"); for (Result r: recs) { generator.writeStartObject(); generator.writeNumberField("item", r.getId()); generator.writeNumberField("score", r.getScore()); Entity item = dao.lookupEntity(CommonTypes.ITEM, r.getId()); String name = item == null ? null : item.maybeGet(CommonAttributes.NAME); if (name != null) { generator.writeStringField("name", name); } generator.writeEndObject(); } generator.writeEndArray(); generator.writeEndObject(); }
@Override public void writeUser(long user, ResultList recs) { System.out.format("recommendations for user %d:%n", user); for (Result res : recs) { System.out.format(" %d", res.getId()); Entity item = dao.lookupEntity(CommonTypes.ITEM, res.getId()); String name = item == null ? null : item.maybeGet(CommonAttributes.NAME); if (name != null) { System.out.format(" (%s)", name); } System.out.format(": %.3f", res.getScore()); System.out.println(); } }
@Nonnull @Override public List<Entity> find(String name, Object value) { int idx = attributes.lookup(name); if (idx < 0) { return ImmutableList.of(); } PackIndex index = indexes[idx]; if (index != null) { IntList positions = index.getPositions(value); return new EntityList(positions); } else { return stream().filter(e -> value.equals(e.maybeGet(name))) .collect(Collectors.toList()); } }
/** * 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; } }
System.out.format("%d", res.getId()); Entity item = dao.lookupEntity(CommonTypes.ITEM, res.getId()); String name = item == null ? null : item.maybeGet(CommonAttributes.NAME); if (name != null) { System.out.format(" (%s)", name);
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,
@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)); }
@Nullable @Override public T apply(@Nullable Entity input) { return input == null ? null : input.maybeGet(name); } };
@Override public void add(Entity e) { Preconditions.checkState(builder != null, "build() already called"); Object value = e.maybeGet(attributeName); if (value != null) { builder.put(value, e); } }
@Override @Nonnull public List<Entity> find(String name, Object value) { Preconditions.checkNotNull(name, "attribute name"); Preconditions.checkNotNull(value, "attribute value"); EntityIndex index = indexes.get(name); if (index != null) { return index.getEntities(value); } // no index, go ahead and search ImmutableList.Builder<Entity> results = ImmutableList.builder(); for (Entity e: store) { if (value.equals(e.maybeGet(name))) { results.add(e); } } return results.build(); }
@Nonnull @Override public <T> List<Entity> find(TypedName<T> name, T value) { int idx = attributes.lookup(name); if (idx < 0) { return ImmutableList.of(); } PackIndex index = indexes[idx]; if (index != null) { return new EntityList(index.getPositions(value)); } else { return stream().filter(e -> value.equals(e.maybeGet(name))) .collect(Collectors.toList()); } }
@Nonnull @Override public List<Entity> find(String name, Object value) { int idx = attributes.lookup(name); if (idx < 0) { return ImmutableList.of(); } PackIndex index = indexes[idx]; if (index != null) { IntList positions = index.getPositions(value); return new EntityList(positions); } else { return stream().filter(e -> value.equals(e.maybeGet(name))) .collect(Collectors.toList()); } }
/** * 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; } }
String name = null; if (itemData != null) { name = itemData.maybeGet(CommonAttributes.NAME);