/** * Internal set method that takes individual indexes. * * @param ci The chunk index. * @param ei The element index. * @param user The user ID to set. * @param item The item ID to set. * @param pref The preference value to set. */ private void set(int ci, int ei, long user, long item, double pref) { users[ci][ei] = userIndex.internId(user); items[ci][ei] = itemIndex.internId(item); values[ci][ei] = pref; }
/** * Construct a new indexer. It maps each long key to its position in the list, and then be ready for more keys. * * @param keys The list of keys to store. */ public static HashKeyIndex create(LongList keys) { HashKeyIndex idx = create(); LongIterator iter = keys.iterator(); while (iter.hasNext()) { idx.internId(iter.nextLong()); } return idx; }
@Test public void testImmutableCopy() { HashKeyIndex idx = new HashKeyIndex(); assertThat(idx.internId(42), equalTo(0)); assertThat(idx.internId(39), equalTo(1)); KeyIndex imm = idx.frozenCopy(); assertThat(imm.getKey(0), equalTo(42L)); assertThat(imm.getKey(1), equalTo(39L)); assertThat(imm.getIndex(42), equalTo(0)); } }
/** * Create a new key index. * * @param keys The keys. * @return A key index containing the elements of {@code keys}. */ public static FrozenHashKeyIndex create(LongCollection keys) { if (keys instanceof LongList) { return create((LongList) keys); } else { HashKeyIndex index = new HashKeyIndex(); LongIterator iter = keys.iterator(); while (iter.hasNext()) { index.internId(iter.nextLong()); } return index.frozenCopy(); } }
@Test public void testFromArray() { HashKeyIndex map = new HashKeyIndex(); map.internId(42); map.internId(37); map.internId(62); double[] values = { 3.5, 4.9, 1.8 }; Long2DoubleMap res = Long2DoubleSortedArrayMap.fromArray(map, values); assertThat(res.size(), equalTo(3)); assertThat(res.keySet(), contains(37L, 42L, 62L)); assertThat(res, hasEntry(37L, 4.9)); assertThat(res, hasEntry(42L, 3.5)); assertThat(res, hasEntry(62L, 1.8)); }
/** * Internal set method that takes individual indexes. * * @param ci The chunk index. * @param ei The element index. * @param user The user ID to set. * @param item The item ID to set. * @param pref The preference value to set. */ private void set(int ci, int ei, long user, long item, double pref) { users[ci][ei] = userIndex.internId(user); items[ci][ei] = itemIndex.internId(item); values[ci][ei] = pref; }
@Test public void testInternId() { HashKeyIndex idx = new HashKeyIndex(); assertThat(idx.size(), equalTo(0)); assertThat(idx.tryGetIndex(42), lessThan(0)); assertThat(idx.internId(42), equalTo(0)); assertThat(idx.getKey(0), equalTo(42L)); assertThat(idx.tryGetIndex(42), equalTo(0)); }
@Before public void createModel() { baseline = PrecomputedItemScorer.newBuilder() .addScore(1, 42, 3.0) .addScore(1, 39, 2.5) .addScore(1, 25, 4.2) .addScore(5, 42, 3.7) .addScore(5, 39, 2.8) .addScore(3, 42, 2.2) .addScore(3, 39, 3.2) .addScore(17, 42, 2.5) .build(); RealMatrix umat = MatrixUtils.createRealMatrix(3, 2); umat.setRow(0, new double[]{0.1, 0.3}); umat.setRow(1, new double[]{-0.2, 0.2}); umat.setRow(2, new double[]{0.0, 0.15}); HashKeyIndex uidx = new HashKeyIndex(); uidx.internId(1); uidx.internId(5); uidx.internId(3); RealMatrix imat = MatrixUtils.createRealMatrix(2, 2); imat.setRow(0, new double[]{0.52, 0.29}); imat.setRow(1, new double[]{0.3, -1.2}); HashKeyIndex iidx = new HashKeyIndex(); iidx.internId(42); iidx.internId(39); model = new MFModel(umat, imat, uidx, iidx); // scorer = new BiasedMFItemScorer(model, baseline); }
assert sums.size() == counts.size(); long item = r.getItemId(); int idx = index.internId(item); if (idx >= sums.size()) { assert idx == sums.size() && idx == counts.size();
/** * Construct a new indexer. It maps each long key to its position in the list, and then be ready for more keys. * * @param keys The list of keys to store. */ public static HashKeyIndex create(LongList keys) { HashKeyIndex idx = create(); LongIterator iter = keys.iterator(); while (iter.hasNext()) { idx.internId(iter.nextLong()); } return idx; }
/** * Create a new key index. * * @param keys The keys. * @return A key index containing the elements of {@code keys}. */ public static FrozenHashKeyIndex create(LongCollection keys) { if (keys instanceof LongList) { return create((LongList) keys); } else { HashKeyIndex index = new HashKeyIndex(); LongIterator iter = keys.iterator(); while (iter.hasNext()) { index.internId(iter.nextLong()); } return index.frozenCopy(); } }
assert sums.size() == counts.size(); long item = r.getItemId(); int idx = index.internId(item); if (idx >= sums.size()) { assert idx == sums.size() && idx == counts.size();