Skip to content

Commit

Permalink
Create interface for Datamodel. First step for issues #83, #107, #108 ,
Browse files Browse the repository at this point in the history
  • Loading branch information
abellogin committed Sep 4, 2016
1 parent f42f516 commit ec29ea5
Show file tree
Hide file tree
Showing 38 changed files with 1,132 additions and 217 deletions.
88 changes: 21 additions & 67 deletions rival-core/src/main/java/net/recommenders/rival/core/DataModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,21 @@
* @param <U> generic type for users
* @param <I> generic type for items
*/
public class DataModel<U, I> {
public class DataModel<U, I> implements DataModelIF<U, I> {

/**
* Preference map between users and items.
*/
private Map<U, Map<I, Double>> userItemPreferences;
protected Map<U, Map<I, Double>> userItemPreferences;
/**
* Preference map between items and users.
* Set containing all the items.
*/
private Map<I, Map<U, Double>> itemUserPreferences;
/**
* The map with the timestamps between users and items.
*/
private Map<U, Map<I, Set<Long>>> userItemTimestamps;
protected Set<I> items;
/**
* Flag to indicate if duplicate preferences should be ignored or not.
* Default: false.
*/
private boolean ignoreDuplicatePreferences;
protected boolean ignoreDuplicatePreferences;

/**
* Default constructor.
Expand All @@ -64,7 +60,7 @@ public DataModel() {
* should be ignored.
*/
public DataModel(final boolean ignoreDupPreferences) {
this(ignoreDupPreferences, new HashMap<U, Map<I, Double>>(), new HashMap<I, Map<U, Double>>(), new HashMap<U, Map<I, Set<Long>>>());
this(ignoreDupPreferences, new HashMap<U, Map<I, Double>>(), new HashSet<I>());
}

/**
Expand All @@ -73,52 +69,32 @@ public DataModel(final boolean ignoreDupPreferences) {
* @param ignoreDupPreferences The flag to indicate whether preferences
* should be ignored.
* @param userItemPreference The preference map between users and items.
* @param itemUserPreference The preference map between items and users.
* @param userItemTimestamp The map with the timestamps between users and
* items
* @param itemSet The items.
*/
public DataModel(final boolean ignoreDupPreferences, final Map<U, Map<I, Double>> userItemPreference, final Map<I, Map<U, Double>> itemUserPreference,
final Map<U, Map<I, Set<Long>>> userItemTimestamp) {
public DataModel(final boolean ignoreDupPreferences, final Map<U, Map<I, Double>> userItemPreference, final Set<I> itemSet) {
this.ignoreDuplicatePreferences = ignoreDupPreferences;
this.userItemPreferences = userItemPreference;
this.itemUserPreferences = itemUserPreference;
this.userItemTimestamps = userItemTimestamp;
}

/**
* Method that returns the preference map between items and users.
*
* @return the preference map between items and users.
*/
public Map<I, Map<U, Double>> getItemUserPreferences() {
return itemUserPreferences;
this.items = itemSet;
}

/**
* Method that returns the preference map between users and items.
*
* @return the preference map between users and items.
*/
@Override
public Map<U, Map<I, Double>> getUserItemPreferences() {
return userItemPreferences;
}

/**
* Method that returns the map with the timestamps between users and items.
*
* @return the map with the timestamps between users and items.
*/
public Map<U, Map<I, Set<Long>>> getUserItemTimestamps() {
return userItemTimestamps;
}

/**
* Method that adds a preference to the model between a user and an item.
*
* @param u the user.
* @param i the item.
* @param d the preference.
*/
@Override
public void addPreference(final U u, final I i, final Double d) {
// update direct map
Map<I, Double> userPreferences = userItemPreferences.get(u);
Expand All @@ -136,51 +112,27 @@ public void addPreference(final U u, final I i, final Double d) {
if (preference != null) {
preference += d;
userPreferences.put(i, preference);
// update inverse map
Map<U, Double> itemPreferences = itemUserPreferences.get(i);
if (itemPreferences == null) {
itemPreferences = new HashMap<>();
itemUserPreferences.put(i, itemPreferences);
}
itemPreferences.put(u, preference);
}
}

/**
* Method that adds a timestamp to the model between a user and an item.
*
* @param u the user.
* @param i the item.
* @param t the timestamp.
*/
public void addTimestamp(final U u, final I i, final Long t) {
Map<I, Set<Long>> userTimestamps = userItemTimestamps.get(u);
if (userTimestamps == null) {
userTimestamps = new HashMap<>();
userItemTimestamps.put(u, userTimestamps);
}
Set<Long> timestamps = userTimestamps.get(i);
if (timestamps == null) {
timestamps = new HashSet<>();
userTimestamps.put(i, timestamps);
}
timestamps.add(t);
// update items
items.add(i);
}

/**
* Method that returns the items in the model.
*
* @return the items in the model.
*/
@Override
public Set<I> getItems() {
return getItemUserPreferences().keySet();
return items;
}

/**
* Method that returns the users in the model.
*
* @return the users in the model.
*/
@Override
public Set<U> getUsers() {
return getUserItemPreferences().keySet();
}
Expand All @@ -190,25 +142,27 @@ public Set<U> getUsers() {
*
* @return the number of items in the model.
*/
@Override
public int getNumItems() {
return getItems().size();
return items.size();
}

/**
* Method that returns the number of users in the model.
*
* @return the number of users in the model.
*/
@Override
public int getNumUsers() {
return getUsers().size();
}

/**
* Method that clears all the maps contained in the model.
*/
@Override
public void clear() {
userItemPreferences.clear();
userItemTimestamps.clear();
itemUserPreferences.clear();
items.clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2015 recommenders.net.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.recommenders.rival.core;

import java.util.Map;
import java.util.Set;

/**
* Interface for the data model used throughout the toolkit. Able to store
* users, items, and preferences.
*
* @author <a href="http://github.com/abellogin">Alejandro</a>, <a
* href="http://github.com/alansaid">Alan</a>
*
* @param <U> generic type for users
* @param <I> generic type for items
*/
public interface DataModelIF<U, I> {

/**
* Method that returns the preference map between users and items.
*
* @return the preference map between users and items.
*/
public Map<U, Map<I, Double>> getUserItemPreferences();

/**
* Method that adds a preference to the model between a user and an item.
*
* @param u the user.
* @param i the item.
* @param d the preference.
*/
public void addPreference(final U u, final I i, final Double d);

/**
* Method that returns the items in the model.
*
* @return the items in the model.
*/
public Set<I> getItems();

/**
* Method that returns the users in the model.
*
* @return the users in the model.
*/
public Set<U> getUsers();

/**
* Method that returns the number of items in the model.
*
* @return the number of items in the model.
*/
public int getNumItems();

/**
* Method that returns the number of users in the model.
*
* @return the number of users in the model.
*/
public int getNumUsers();

/**
* Method that clears all the maps contained in the model.
*/
public void clear();
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private DataModelUtils() {
* @throws UnsupportedEncodingException when the requested encoding (UTF-8)
* is not available.
*/
public static <U, I> void saveDataModel(final DataModel<U, I> dm, final String outfile, final boolean overwrite)
public static <U, I> void saveDataModel(final TemporalDataModelIF<U, I> dm, final String outfile, final boolean overwrite)
throws FileNotFoundException, UnsupportedEncodingException {
if (new File(outfile).exists() && !overwrite) {
System.out.println("Ignoring " + outfile);
Expand Down
13 changes: 11 additions & 2 deletions rival-core/src/main/java/net/recommenders/rival/core/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,20 @@
public interface Parser<U, I> {

/**
* Parse data file.
* Parse a temporal data file.
*
* @param f The file to be parsed.
* @return A dataset created from the file.
* @throws IOException if the file cannot be read.
*/
DataModel<U, I> parseData(File f) throws IOException;
TemporalDataModelIF<U, I> parseTemporalData(File f) throws IOException;

/**
* Parse a data file.
*
* @param f The file to be parsed.
* @return A dataset created from the file.
* @throws IOException if the file cannot be read.
*/
DataModelIF<U, I> parseData(File f) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,18 @@
* @param <U> generic type of users
* @param <I> generic type of items
*/
public interface ParserWithIdMapping<U, I> {
public interface ParserWithIdMapping<U, I> extends Parser<U, I> {

/**
* Parse a temporal data file.
*
* @param f The file to be parsed.
* @param mapIdsPrefix The prefix of the file where the id mapping will be
* stored (and will be read from).
* @return A dataset created from the file.
* @throws IOException if the file cannot be read.
*/
TemporalDataModelIF<U, I> parseTemporalData(File f, String mapIdsPrefix) throws IOException;

/**
* Parse data file.
Expand All @@ -37,5 +48,5 @@ public interface ParserWithIdMapping<U, I> {
* @return The data model created from the file.
* @throws IOException if the file cannot be read.
*/
DataModel<U, I> parseData(File f, String mapIdsPrefix) throws IOException;
DataModelIF<U, I> parseData(File f, String mapIdsPrefix) throws IOException;
}
Loading

0 comments on commit ec29ea5

Please sign in to comment.