Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.58 KB | None | 0 0
  1. package com.be.music.blast.musicBlast;
  2.  
  3. import org.springframework.stereotype.Service;
  4.  
  5. import java.sql.*;
  6. import java.util.ArrayList;
  7. import java.util.Iterator;
  8. import java.util.List;
  9. import java.sql.Date;
  10.  
  11. import javax.sql.DataSource;
  12.  
  13. import org.apache.mahout.cf.taste.common.TasteException;
  14. import org.apache.mahout.cf.taste.impl.common.FastByIDMap;
  15. import org.apache.mahout.cf.taste.impl.model.GenericDataModel;
  16. import org.apache.mahout.cf.taste.impl.model.GenericPreference;
  17. import org.apache.mahout.cf.taste.impl.model.GenericUserPreferenceArray;
  18. import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
  19. import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood;
  20. import org.apache.mahout.cf.taste.impl.recommender.CachingRecommender;
  21. import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
  22. import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
  23. import org.apache.mahout.cf.taste.model.DataModel;
  24. import org.apache.mahout.cf.taste.model.Preference;
  25. import org.apache.mahout.cf.taste.model.PreferenceArray;
  26. import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
  27. import org.apache.mahout.cf.taste.recommender.RecommendedItem;
  28. import org.apache.mahout.cf.taste.recommender.Recommender;
  29. import org.apache.mahout.cf.taste.recommender.UserBasedRecommender;
  30. import org.apache.mahout.cf.taste.similarity.UserSimilarity;
  31.  
  32. import com.mysql.jdbc.Connection;
  33. import com.mysql.jdbc.Statement;
  34. import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
  35.  
  36. @Service
  37. public class RecommendersService {
  38.    
  39.    
  40.     public RecommendersService()
  41.     {
  42.  
  43.     }
  44.    
  45.     public void generateStartingData(ResultSet rs, Connection con) throws SQLException {
  46.         int id_product;
  47.         int id_rate;
  48.         int id_customer;
  49.         float rateValue;
  50.         long miliSeconds;
  51.         String query2 = "INSERT INTO ps_product_comment "
  52.                     + "(id_product_comment, id_product, id_customer, grade, content, title, customer_name, validate, deleted, date_add) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
  53.         PreparedStatement prepStmt = con.prepareStatement(query2);
  54.         try {
  55.             while(rs.next()){
  56.                  id_product  = rs.getInt("id_product");
  57.                  id_customer = rs.getInt("id_customer");
  58.                  rateValue =  rs.getFloat("rate");
  59.                  id_rate = rs.getInt("id_rate");
  60.                 prepStmt.setInt(1, id_rate);
  61.                 prepStmt.setInt(2, id_product);
  62.                 prepStmt.setInt(3, id_customer);
  63.                 prepStmt.setFloat(4, rateValue);
  64.                 prepStmt.setString(5, "Random content");
  65.                 prepStmt.setString(6, "Random title");
  66.                 prepStmt.setString(7, "Random name");
  67.                 prepStmt.setBoolean(8, true);
  68.                 prepStmt.setBoolean(9, false);
  69.                 miliSeconds = (long)46* (long)365* (long)24* (long)60* (long)60* (long)1000;
  70.                 prepStmt.setDate(10, new Date(miliSeconds));     
  71.                 prepStmt.execute();  
  72.                
  73.                  System.out.print(id_product + " ");
  74.                  System.out.print(id_customer + " ");
  75.                  System.out.print(rateValue + " ");
  76.                  System.out.println(id_rate + " ");
  77.               }
  78.         } catch (SQLException e) {
  79.             // TODO Auto-generated catch block
  80.             e.printStackTrace();
  81.         }
  82.     }
  83.    
  84.     public CachingRecommender getRecommender(Connection con, ResultSet rs, Statement stmt) throws SQLException, TasteException {
  85.         String comment_stmt = "SELECT * FROM ps_product_comment";      
  86.         List<Preference> liOfRatePrefs = new ArrayList<Preference>();
  87.         GenericUserPreferenceArray genUserRatePrefs;
  88.         FastByIDMap<PreferenceArray> fastRatePrefsMap;
  89.         GenericDataModel genericRatesModel;  
  90.         UserSimilarity userSimilarity;
  91.         UserNeighborhood neighborhood;
  92.         Recommender recommender, cachingRecommender;
  93.        
  94.         //Creating list of lists of rates for one customer
  95.         ArrayList<ArrayList<Preference>> liOfliOfRatePref= new ArrayList<ArrayList<Preference>>();
  96.         for (int i=0 ;i<getNumOfCustomers(con, rs, stmt) ;i++) {
  97.             liOfliOfRatePref.add(new ArrayList<Preference>());
  98.         }
  99.        
  100.        
  101.        
  102.         writePrefsToList(con, rs, stmt, liOfliOfRatePref);
  103.        
  104.         for (int i=0 ;i<getNumOfCustomers(con, rs, stmt) ;i++) {
  105.              for (Preference arr : liOfliOfRatePref.get(i)) {
  106.                 // System.out.println(arr.getUserID() + " " + arr.getValue() + " " + arr.getItemID());
  107.              }
  108.         }
  109.        
  110.         fastRatePrefsMap = new FastByIDMap<PreferenceArray>();
  111.         for (int i=0; i<liOfliOfRatePref.size(); i++){
  112.             fastRatePrefsMap.put((long) i, new GenericUserPreferenceArray(liOfliOfRatePref.get(i)));
  113.             GenericUserPreferenceArray gen = (GenericUserPreferenceArray) fastRatePrefsMap.get(i);
  114.             for (int j = 0; j < gen.length(); j ++) {
  115.             //  System.out.println(gen.get(j).getItemID() + " " + gen.get(j).getUserID() + " " + gen.get(j).getValue());
  116.             }
  117.            
  118.         }
  119.        
  120.         genericRatesModel = new GenericDataModel(fastRatePrefsMap);
  121.         userSimilarity = new PearsonCorrelationSimilarity(genericRatesModel);
  122.         neighborhood = new ThresholdUserNeighborhood(0.1, userSimilarity, genericRatesModel);
  123.         recommender = new GenericUserBasedRecommender(genericRatesModel, neighborhood, userSimilarity);
  124.  
  125.         return new CachingRecommender(recommender);
  126.     }
  127.    
  128.     public void writePrefsToList(Connection con, ResultSet rs, Statement stmt, ArrayList<ArrayList<Preference>> liOfliOfRatePref) throws SQLException {
  129.         int id_product = 0;
  130.         int id_customer = 0;
  131.         int id_rate = 0;
  132.         float rateValue = (float) 0.0;
  133.         String comment_stmt = "SELECT * FROM ps_product_comment";
  134.         stmt = (Statement) con.createStatement();
  135.         rs = stmt.executeQuery(comment_stmt);
  136.         while(rs.next()){
  137.              id_product  = rs.getInt("id_product");
  138.              id_customer = rs.getInt("id_customer");
  139.              rateValue =  rs.getFloat("grade");
  140.              id_rate = rs.getInt("id_product_comment");
  141.              
  142.             // Adding preference to the list by correct id_customer (id_customer - 1, because in DB there are customers counted from 1)
  143.             liOfliOfRatePref.get(id_customer - 1).add(new GenericPreference(id_customer,  id_product,  rateValue));
  144.         }
  145.     }
  146.    
  147.     public int getNumOfCustomers(Connection con, ResultSet rs, Statement stmt) throws SQLException  {
  148.         String customers_stmt    = "SELECT * FROM ps_customer";
  149.         stmt = (Statement) con.createStatement();
  150.         rs = stmt.executeQuery(customers_stmt);
  151.         int numOfCustomers = 0;
  152.         while(rs.next()) {
  153.              numOfCustomers++;
  154.              System.out.println(numOfCustomers);
  155.         }
  156.        
  157.         return numOfCustomers;
  158.     }
  159.    
  160.     public String getItemIds(int idOfUser) throws TasteException
  161.     {
  162.         Connection conn = null;
  163.         Connection con = null;
  164.         Statement stmt = null;
  165.         ResultSet rs = null;
  166.         try {
  167.           // Get connection from DataSource
  168.             con = (Connection) DriverManager.getConnection("jdbc:mysql://172.22.0.2:3306/zabawa","root","szachownica");
  169.             stmt = (Statement) con.createStatement();
  170.        
  171.            
  172.             List<RecommendedItem> recommendations = getRecommender(con, rs, stmt).recommend(20, 3);
  173.             String itemIDs = null;
  174.             for(int i=0; i<recommendations.size(); i++) {
  175.                 itemIDs += recommendations.get(i).getItemID();
  176.                 if(i != recommendations.size()-1) itemIDs += ",";;
  177.             }
  178.            
  179.             return itemIDs;
  180.        
  181.        
  182.     }catch (SQLException e) {
  183.         e.printStackTrace();
  184.       } finally {
  185.         try {
  186.           if (rs != null) {
  187.             rs.close();
  188.           }
  189.           if (stmt != null) {
  190.             stmt.close();
  191.           }
  192.           if (conn != null) {
  193.             conn.close();
  194.           }
  195.         } catch (Exception e) {
  196.           e.printStackTrace();
  197.         }
  198.       }
  199.         return null;
  200.     }
  201.    
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement