/**
   * Load preferences from an input stream.
   *
   * <p>Each line is a different preference, with tab-separated fields indicating user, item, weight
   * and other information.
   *
   * @param <U> type of the users
   * @param <I> type of the items
   * @param in input stream to read from
   * @param uParser user type parser
   * @param iParser item type parser
   * @param dp double parse
   * @param uIndex user index
   * @param iIndex item index
   * @return a simple list-of-lists FastPreferenceData with the information read
   * @throws IOException when path does not exists of IO error
   */
  public static <U, I> SimpleFastPreferenceData<U, I> load(
      InputStream in,
      Parser<U> uParser,
      Parser<I> iParser,
      DoubleParser dp,
      FastUserIndex<U> uIndex,
      FastItemIndex<I> iIndex)
      throws IOException {
    AtomicInteger numPreferences = new AtomicInteger();

    List<List<IdxPref>> uidxList = new ArrayList<>();
    for (int uidx = 0; uidx < uIndex.numUsers(); uidx++) {
      uidxList.add(null);
    }

    List<List<IdxPref>> iidxList = new ArrayList<>();
    for (int iidx = 0; iidx < iIndex.numItems(); iidx++) {
      iidxList.add(null);
    }

    try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
      reader
          .lines()
          .forEach(
              l -> {
                CharSequence[] tokens = split(l, '\t', 4);
                U user = uParser.parse(tokens[0]);
                I item = iParser.parse(tokens[1]);
                double value;
                if (tokens.length >= 3) {
                  value = dp.parse(tokens[2]);
                } else {
                  value = dp.parse(null);
                }

                int uidx = uIndex.user2uidx(user);
                int iidx = iIndex.item2iidx(item);

                numPreferences.incrementAndGet();

                List<IdxPref> uList = uidxList.get(uidx);
                if (uList == null) {
                  uList = new ArrayList<>();
                  uidxList.set(uidx, uList);
                }
                uList.add(new IdxPref(iidx, value));

                List<IdxPref> iList = iidxList.get(iidx);
                if (iList == null) {
                  iList = new ArrayList<>();
                  iidxList.set(iidx, iList);
                }
                iList.add(new IdxPref(uidx, value));
              });
    }

    return new SimpleFastPreferenceData<>(
        numPreferences.intValue(), uidxList, iidxList, uIndex, iIndex);
  }
Example #2
0
 /**
  * Constructor.
  *
  * @param uIndex fast user index
  * @param iIndex fast item index
  * @param K dimension of the latent feature space
  * @param initFunction function to initialize the cells of the matrices
  */
 public Factorization(
     FastUserIndex<U> uIndex, FastItemIndex<I> iIndex, int K, DoubleFunction initFunction) {
   this.userMatrix = new DenseDoubleMatrix2D(uIndex.numUsers(), K);
   this.userMatrix.assign(initFunction);
   this.itemMatrix = new DenseDoubleMatrix2D(iIndex.numItems(), K);
   this.itemMatrix.assign(initFunction);
   this.K = K;
   this.uIndex = uIndex;
   this.iIndex = iIndex;
 }
Example #3
0
 @Override
 public boolean containsItem(I i) {
   return iIndex.containsItem(i);
 }
Example #4
0
 @Override
 public I iidx2item(int iidx) {
   return iIndex.iidx2item(iidx);
 }
Example #5
0
 @Override
 public int item2iidx(I i) {
   return iIndex.item2iidx(i);
 }
Example #6
0
 @Override
 public int numItems() {
   return iIndex.numItems();
 }