Esempio n. 1
0
  /**
   * Write a user or item index to the file.
   *
   * @param map The index to write.
   */
  private void writeIndex(Long2ObjectMap<IntList> map) throws IOException {
    LongSortedSet keys = LongUtils.packedSet(map.keySet());
    BinaryIndexTableWriter tableWriter =
        BinaryIndexTableWriter.create(format, channel, keys.size());

    SortComparator indexComparator = new SortComparator();

    LongIterator iter = keys.iterator();
    while (iter.hasNext()) {
      final long key = iter.nextLong();
      int[] indexes = map.get(key).toIntArray();
      if (needsSorting) {
        IntArrays.quickSort(indexes, indexComparator);
      }

      if (translationMap != null) {
        for (int i = 0; i < indexes.length; i++) {
          indexes[i] = translationMap[indexes[i]];
        }
      }

      logger.debug("writing {} indices for id {}", key, indexes.length);
      tableWriter.writeEntry(key, indexes);
    }
  }
  /**
   * Creates a matrix to process genre data and generate the second factor of the proximity matrix
   * needed for a {@code HIRItemScorer}.
   *
   * @param dao The DataAccessObject interfacing with the item data for the model
   * @param gDao The genreDataAccessObject interfacing with the genre data for the model
   */
  public TransposedFactorOfProximity(ItemDAO dao, ItemGenreDAO gDao) {

    LongSet items = dao.getItemIds();
    genreSize = gDao.getGenreSize();
    int itemSize = items.size();

    double[][] dataTransposed = new double[genreSize][itemSize];

    transposed = MatrixUtils.createRealMatrix(dataTransposed);

    int i = 0;
    LongIterator iter = items.iterator();
    while (iter.hasNext()) {
      long item = iter.nextLong();
      transposed.setColumnVector(i, gDao.getItemGenre(item));
      i++;
    }
  }
  /** Compute the DCG of a list of items with respect to a value vector. */
  static double computeDCG(LongList items, SparseVector values) {
    final double lg2 = log(2);

    double gain = 0;
    int rank = 0;

    LongIterator iit = items.iterator();
    while (iit.hasNext()) {
      final long item = iit.nextLong();
      final double v = values.get(item, 0);
      rank++;
      if (rank < 2) {
        gain += v;
      } else {
        gain += v * lg2 / log(rank);
      }
    }

    return gain;
  }
  @Test
  public void itShouldDecompressLists() throws IOException {
    final Input input =
        getInput(
            "1 1 01101 00101 0101 01100 1"
                + "0100 1 1 1 01101 00001 1 1 00100011"
                + "0101 1 1 1 1"
                + "1 0100 00001 001000111"
                + "0101 1 1 1 1"
                + "01101 0101 0 0101 01100 1 1");
    final PebbleOffsetsStore offsetsStore =
        new LongListPebbleOffsetsStore(new long[] {0L, 22L, 49L, 57L, 76L, 84L});
    final PebbleBytesStore bytesStore = new BytesArrayPebbleBytesStore(input.buffer, offsetsStore);
    final int valueBitSize = 5;
    final LongList[] expectedLists =
        new LongList[] {
          new LongArrayList(new long[] {5L, 8L, 12L, 13L}),
          new LongArrayList(new long[] {1L, 2L, 3L, 5L, 8L, 12L, 13L, 14L}),
          new LongArrayList(new long[] {5L, 8L, 12L, 13L}),
          new LongArrayList(
              new long[] {1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L}),
          new LongArrayList(new long[] {5L, 8L, 12L, 13L}),
          new LongArrayList(new long[] {5L, 8L, 12L, 13L})
        };
    final LongList[] lists = new LongList[expectedLists.length];
    LongList list;
    LongIterator iterator;

    for (int i = 0; i < lists.length; i++) {
      iterator = StrictlyIncrementalListIterator.build(i, valueBitSize, bytesStore);
      lists[i] = list = new LongArrayList();
      while (iterator.hasNext()) {
        list.add(iterator.nextLong());
      }
    }

    assertEquals(
        Helper.<Long, LongList>translateToUtilsCollection(expectedLists),
        Helper.<Long, LongList>translateToUtilsCollection(lists));
  }