Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.be.music.blast.musicBlast;
- import org.springframework.stereotype.Service;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.sql.Date;
- import javax.sql.DataSource;
- import org.apache.mahout.cf.taste.common.TasteException;
- import org.apache.mahout.cf.taste.impl.common.FastByIDMap;
- import org.apache.mahout.cf.taste.impl.model.GenericDataModel;
- import org.apache.mahout.cf.taste.impl.model.GenericPreference;
- import org.apache.mahout.cf.taste.impl.model.GenericUserPreferenceArray;
- import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
- import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood;
- import org.apache.mahout.cf.taste.impl.recommender.CachingRecommender;
- import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
- import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
- import org.apache.mahout.cf.taste.model.DataModel;
- import org.apache.mahout.cf.taste.model.Preference;
- import org.apache.mahout.cf.taste.model.PreferenceArray;
- import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
- import org.apache.mahout.cf.taste.recommender.RecommendedItem;
- import org.apache.mahout.cf.taste.recommender.Recommender;
- import org.apache.mahout.cf.taste.recommender.UserBasedRecommender;
- import org.apache.mahout.cf.taste.similarity.UserSimilarity;
- import com.mysql.jdbc.Connection;
- import com.mysql.jdbc.Statement;
- import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
- @Service
- public class RecommendersService {
- public RecommendersService()
- {
- }
- public void generateStartingData(ResultSet rs, Connection con) throws SQLException {
- int id_product;
- int id_rate;
- int id_customer;
- float rateValue;
- long miliSeconds;
- String query2 = "INSERT INTO ps_product_comment "
- + "(id_product_comment, id_product, id_customer, grade, content, title, customer_name, validate, deleted, date_add) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
- PreparedStatement prepStmt = con.prepareStatement(query2);
- try {
- while(rs.next()){
- id_product = rs.getInt("id_product");
- id_customer = rs.getInt("id_customer");
- rateValue = rs.getFloat("rate");
- id_rate = rs.getInt("id_rate");
- prepStmt.setInt(1, id_rate);
- prepStmt.setInt(2, id_product);
- prepStmt.setInt(3, id_customer);
- prepStmt.setFloat(4, rateValue);
- prepStmt.setString(5, "Random content");
- prepStmt.setString(6, "Random title");
- prepStmt.setString(7, "Random name");
- prepStmt.setBoolean(8, true);
- prepStmt.setBoolean(9, false);
- miliSeconds = (long)46* (long)365* (long)24* (long)60* (long)60* (long)1000;
- prepStmt.setDate(10, new Date(miliSeconds));
- prepStmt.execute();
- System.out.print(id_product + " ");
- System.out.print(id_customer + " ");
- System.out.print(rateValue + " ");
- System.out.println(id_rate + " ");
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public CachingRecommender getRecommender(Connection con, ResultSet rs, Statement stmt) throws SQLException, TasteException {
- String comment_stmt = "SELECT * FROM ps_product_comment";
- List<Preference> liOfRatePrefs = new ArrayList<Preference>();
- GenericUserPreferenceArray genUserRatePrefs;
- FastByIDMap<PreferenceArray> fastRatePrefsMap;
- GenericDataModel genericRatesModel;
- UserSimilarity userSimilarity;
- UserNeighborhood neighborhood;
- Recommender recommender, cachingRecommender;
- //Creating list of lists of rates for one customer
- ArrayList<ArrayList<Preference>> liOfliOfRatePref= new ArrayList<ArrayList<Preference>>();
- for (int i=0 ;i<getNumOfCustomers(con, rs, stmt) ;i++) {
- liOfliOfRatePref.add(new ArrayList<Preference>());
- }
- writePrefsToList(con, rs, stmt, liOfliOfRatePref);
- for (int i=0 ;i<getNumOfCustomers(con, rs, stmt) ;i++) {
- for (Preference arr : liOfliOfRatePref.get(i)) {
- // System.out.println(arr.getUserID() + " " + arr.getValue() + " " + arr.getItemID());
- }
- }
- fastRatePrefsMap = new FastByIDMap<PreferenceArray>();
- for (int i=0; i<liOfliOfRatePref.size(); i++){
- fastRatePrefsMap.put((long) i, new GenericUserPreferenceArray(liOfliOfRatePref.get(i)));
- GenericUserPreferenceArray gen = (GenericUserPreferenceArray) fastRatePrefsMap.get(i);
- for (int j = 0; j < gen.length(); j ++) {
- // System.out.println(gen.get(j).getItemID() + " " + gen.get(j).getUserID() + " " + gen.get(j).getValue());
- }
- }
- genericRatesModel = new GenericDataModel(fastRatePrefsMap);
- userSimilarity = new PearsonCorrelationSimilarity(genericRatesModel);
- neighborhood = new ThresholdUserNeighborhood(0.1, userSimilarity, genericRatesModel);
- recommender = new GenericUserBasedRecommender(genericRatesModel, neighborhood, userSimilarity);
- return new CachingRecommender(recommender);
- }
- public void writePrefsToList(Connection con, ResultSet rs, Statement stmt, ArrayList<ArrayList<Preference>> liOfliOfRatePref) throws SQLException {
- int id_product = 0;
- int id_customer = 0;
- int id_rate = 0;
- float rateValue = (float) 0.0;
- String comment_stmt = "SELECT * FROM ps_product_comment";
- stmt = (Statement) con.createStatement();
- rs = stmt.executeQuery(comment_stmt);
- while(rs.next()){
- id_product = rs.getInt("id_product");
- id_customer = rs.getInt("id_customer");
- rateValue = rs.getFloat("grade");
- id_rate = rs.getInt("id_product_comment");
- // Adding preference to the list by correct id_customer (id_customer - 1, because in DB there are customers counted from 1)
- liOfliOfRatePref.get(id_customer - 1).add(new GenericPreference(id_customer, id_product, rateValue));
- }
- }
- public int getNumOfCustomers(Connection con, ResultSet rs, Statement stmt) throws SQLException {
- String customers_stmt = "SELECT * FROM ps_customer";
- stmt = (Statement) con.createStatement();
- rs = stmt.executeQuery(customers_stmt);
- int numOfCustomers = 0;
- while(rs.next()) {
- numOfCustomers++;
- System.out.println(numOfCustomers);
- }
- return numOfCustomers;
- }
- public String getItemIds(int idOfUser) throws TasteException
- {
- Connection conn = null;
- Connection con = null;
- Statement stmt = null;
- ResultSet rs = null;
- try {
- // Get connection from DataSource
- con = (Connection) DriverManager.getConnection("jdbc:mysql://172.22.0.2:3306/zabawa","root","szachownica");
- stmt = (Statement) con.createStatement();
- List<RecommendedItem> recommendations = getRecommender(con, rs, stmt).recommend(20, 3);
- String itemIDs = null;
- for(int i=0; i<recommendations.size(); i++) {
- itemIDs += recommendations.get(i).getItemID();
- if(i != recommendations.size()-1) itemIDs += ",";;
- }
- return itemIDs;
- }catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- if (rs != null) {
- rs.close();
- }
- if (stmt != null) {
- stmt.close();
- }
- if (conn != null) {
- conn.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement