/** * 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); }
@Override public int item2iidx(I i) { return iIndex.item2iidx(i); }