Sistema de Recomendação (projecto): Difference between revisions

From Wiki**3

(Created page with "Este projecto destina-.se a realizar um sistema de recomendação baseado em filtros colaborativos. == Collaborative Filtering Recommendation System == Automatic recommendat...")
 
Line 25: Line 25:
== Solution ==
== Solution ==


{{CollapsedCode|Store.java|
<java5>
<java5>
package scollafil;
package scollafil;
Line 158: Line 159:
}
}
</java5>
</java5>
}}


<java5>
<java5>

Revision as of 17:17, 2 November 2016

Este projecto destina-.se a realizar um sistema de recomendação baseado em filtros colaborativos.

Collaborative Filtering Recommendation System

Automatic recommendation algorithms are important in electronic commerce for matching users with products that may be of interest to them. A simple but effective way of achieving this match is called collaborative filtering. There are several versions of collaborative filtering recommendation systems, depending on the exact nature of the recommendation goal. In general, these algorithms start from a matrix of users by products and produce recommendations or rating predictions, by matching users (or products), using similarity measures between users (or products).

In our scenario, the users-products matrix encodes the associations between users and products they acquire. Furthermore, users-products pairs are unique, i.e., a user may acquire a product more than once, but there will be only one record connecting that user with that product. Although this binary representation has some limitations (it does not reflect product preferences, for instance), it makes processing simpler.

When computing suggestions for a given user, the algorithm first finds users that are similar to the one under consideration and, then, from the most popular products acquired by the most similar users, selects the products to recommend. User similarity is computed in terms of the products that they acquire and can be computed in various ways. A popular way is to consider the cosine distance, measured between the vectors formed by the products acquired by each user: each vector position corresponds to a product (considering all known products acquired by all users) and is, in our scenario, either 0 (the user has not acquired the product yet), or 1 (the user has already acquired the product).

Sim-cosine.png

In this formula u1 and u2 are two users, vecu1 and vecu2 are the corresponding product vectors, Pu1 and Pu2 are the sets of products each user has acquired. ||·|| is the vector norm operator and |·| is the set cardinality operator.

Running the Program

The program takes property db to specify the file containing the users-products database and a command line argument for specifying the user to provide recommendations for.

The output consists of a list of 10 products (one per line), ordered by their fit to the user or alphabetically if tied.

You may assume that the database does not contain errors and that there are no mistakes when invoking the program. As an example, if the database file is last.fm.100k.txt and the user to get recommendations for is user_000577, the command to invoke the recommender is (App is the class containing the main method):

 java -Ddb=last.fm.100k.txt App user_000577

Solution

Store.java
{{{2}}}

<java5> package scollafil;

/**

* Distance between two points: source and target.
* 
* @author David Martins de Matos
*/

class Distance implements Comparable<Distance> {

 /** Source. */
 private String _source;
 
 /** Target. */
 private String _target;
 
 /** Distance. */
 private double _distance;
 /**
  * @param source
  * @param target
  * @param distance
  */
 public Distance(String source, String target, double distance) {
   _source = source;
   _target = target;
   _distance = distance;
 }
 /**
  * Natural order is according to distance, except when distances are same. In
  * this case, the order is alphabetical (target).
  * 
  * @see java.lang.Comparable#compareTo(java.lang.Object)
  */
 @Override
 public int compareTo(Distance d) {
   return _distance > d._distance ? -1
       : (_distance < d._distance ? 1 : _target.compareTo(d._target));
 }
 /** @return source */
 public String getSource() {
   return _source;
 }
 /** @return target */
 public String getTarget() {
   return _target;
 }
 /** @return distance */
 public double getDistance() {
   return _distance;
 }

} </java5>

<java5> package scollafil;

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;

/**

* This class implements a simple collaborative filtering recommender.
* 
* The program uses property "db" to read a shopping database. After
* initialization, client names are requested and a product list is produced for
* each one. Only the top-3 items are presented.
* 
* @author David Martins de Matos
*/

public class App {

 /**
  * @param args
  *          command line arguments
  */
 @SuppressWarnings("nls")
 public static void main(String[] args) {
   String db = System.getProperty("db");
   if (db == null) {
     System.err.println("Please, provide a shopping database!");
     return;
   }
   Store store = new Store(db);
   BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
   String client = "";
   try {
     do {
       System.out.print("Client: ");
       if ((client = in.readLine()) == null || client.equals(""))
         break;
       System.out.println(store.getRecommendationsFor(client));
     } while (true);
   } catch (IOException e) {
     System.err.println("Error while reading standard input!");
     e.printStackTrace();
     System.exit(1);
   }
 }

} </java5>