<commit_before>// Copyright (C) 2014 <NAME>
//
// This file is part of LibRec.
//
// LibRec is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// LibRec is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with LibRec. If not, see <http://www.gnu.org/licenses/>.
//

package i5.las2peer.services.recommender.librec.rating;

import java.util.List;

import i5.las2peer.services.recommender.communities.CommunityDetector;
import i5.las2peer.services.recommender.communities.CommunityDetector.CommunityDetectionAlgorithm;
import i5.las2peer.services.recommender.graphs.GraphBuilder;
import i5.las2peer.services.recommender.graphs.GraphBuilder.GraphConstructionMethod;
import i5.las2peer.services.recommender.graphs.GraphBuilder.SimilarityMeasure;
import i5.las2peer.services.recommender.librec.data.Configuration;
import i5.las2peer.services.recommender.librec.data.DenseMatrix;
import i5.las2peer.services.recommender.librec.data.DenseVector;
import i5.las2peer.services.recommender.librec.data.MatrixEntry;
import i5.las2peer.services.recommender.librec.data.SparseMatrix;
import i5.las2peer.services.recommender.librec.util.Logs;
import i5.las2peer.services.recommender.librec.util.Strings;

/**
 * Community-aware model based on <NAME>, Factorization Meets the Neighborhood: a Multifaceted Collaborative Filtering Model., KDD 2008.
 * 
 * @author guoguibing, martin
 * 
 */
@Configuration("factors, lRateC, lRateCN, lRateCF, maxLRate, regC, regCN, regCF, iters, boldDriver")
public class ComNeighSVDPlusPlusFast extends BiasedMF {

	protected DenseMatrix Y, Ocu, Oci;
	protected DenseMatrix W, C; // weighting factors for neighborhood model
	
	// ---Community-related algorithm parameters
	// graph construction method
	private GraphConstructionMethod graphMethod;
	// k parameter for the k-nn graph construction
	private int knn;
	// Similarity measure for the k-nn graph construction
	private SimilarityMeasure sim;
	// Community detection algorithm
	private CommunityDetectionAlgorithm cdAlgo;
	// Steps parameter for the Walktrap algorithm
	private int wtSteps;
	// Maximum number of (overlapping) communities per user/item

	protected int numUserCommunities;
	protected int numItemCommunities;
	
	protected DenseVector BCu, BCi;
	
	// Community membership matrices for users and items
//	protected SparseMatrix userMembershipsMatrix, itemMembershipsMatrix;
	
	// User/item community membership map
	private DenseVector userMembershipsVector, itemMembershipsVector;
	
	public ComNeighSVDPlusPlusFast(SparseMatrix trainMatrix, SparseMatrix testMatrix, int fold) {
		super(trainMatrix, testMatrix, fold);

		setAlgoName("ComNeighSVD++Fast");
		
		knn = cf.getInt("graph.knn.k", 10);
		switch (cf.getString("graph.method", "ratings").toLowerCase()){
		case "tags":
			graphMethod = GraphConstructionMethod.TAGS;
			break;
		default:
		case "ratings":
			graphMethod = GraphConstructionMethod.RATINGS;
			break;
		}
		switch (cf.getString("graph.knn.sim", "cosine").toLowerCase()){
		case "pearson":
			sim = SimilarityMeasure.PEARSON_CORRELATION;
			break;
		default:
		case "cosine":
			sim = SimilarityMeasure.COSINE_SIMILARITY;
			break;
		}
		switch (cf.getString("cd.algo", "wt").toLowerCase()){
		case "dmid":
			cdAlgo = CommunityDetectionAlgorithm.DMID;
			break;
		case "slpa":
			cdAlgo = CommunityDetectionAlgorithm.SLPA;
			break;
		default:
		case "wt":
			cdAlgo = CommunityDetectionAlgorithm.WALKTRAP;
			break;
		}
		wtSteps = cf.getInt("cd.walktrap.steps", 2);
	}

	@Override
	protected void initModel() throws Exception {
		super.initModel();
		
		// build the user and item graphs
		Logs.info("{}{} build user and item graphs ...", new Object[] { algoName, foldInfo });
		GraphBuilder gb = new GraphBuilder();
		gb.setMethod(graphMethod);
		gb.setRatingData(trainMatrix);
		gb.setTaggingData(userTagTable, itemTagTable);
		gb.setK(knn);
		gb.setSimilarityMeasure(sim);
		gb.buildGraphs();
		SparseMatrix userMatrix = gb.getUserAdjacencyMatrix();
		SparseMatrix itemMatrix = gb.getItemAdjacencyMatrix();
		
		// detect communities
		Logs.info("{}{} detect communities ...", new Object[] { algoName, foldInfo });
		CommunityDetector cd = new CommunityDetector();
		cd.setAlgorithm(cdAlgo);
		if (cdAlgo == CommunityDetectionAlgorithm.WALKTRAP)
			cd.setWalktrapParameters(wtSteps);
		
		cd.setGraph(userMatrix);
		cd.detectCommunities();
		userMembershipsVector = cd.getMembershipsVector();
		numUserCommunities = cd.getNumCommunities();
		
		cd.setGraph(itemMatrix);
		cd.detectCommunities();
		itemMembershipsVector = cd.getMembershipsVector();
		numItemCommunities = cd.getNumCommunities();
		
		logCommunityInfo();
		
		Y = new DenseMatrix(numItemCommunities, numFactors);
		Y.init(initMean, initStd);
<commit_msg>clean up unused objects<commit_after>// Copyright (C) 2014 <NAME>
//
// This file is part of LibRec.
//
// LibRec is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// LibRec is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with LibRec. If not, see <http://www.gnu.org/licenses/>.
//

package i5.las2peer.services.recommender.librec.rating;

import java.util.List;

import i5.las2peer.services.recommender.communities.CommunityDetector;
import i5.las2peer.services.recommender.communities.CommunityDetector.CommunityDetectionAlgorithm;
import i5.las2peer.services.recommender.graphs.GraphBuilder;
import i5.las2peer.services.recommender.graphs.GraphBuilder.GraphConstructionMethod;
import i5.las2peer.services.recommender.graphs.GraphBuilder.SimilarityMeasure;
import i5.las2peer.services.recommender.librec.data.Configuration;
import i5.las2peer.services.recommender.librec.data.DenseMatrix;
import i5.las2peer.services.recommender.librec.data.DenseVector;
import i5.las2peer.services.recommender.librec.data.MatrixEntry;
import i5.las2peer.services.recommender.librec.data.SparseMatrix;
import i5.las2peer.services.recommender.librec.util.Logs;
import i5.las2peer.services.recommender.librec.util.Strings;

/**
 * Community-aware model based on <NAME>, Factorization Meets the Neighborhood: a Multifaceted Collaborative Filtering Model., KDD 2008.
 * 
 * @author guoguibing, martin
 * 
 */
@Configuration("factors, lRateC, lRateCN, lRateCF, maxLRate, regC, regCN, regCF, iters, boldDriver")
public class ComNeighSVDPlusPlusFast extends BiasedMF {

	protected DenseMatrix Y, Ocu, Oci;
	protected DenseMatrix W, C; // weighting factors for neighborhood model
	
	// ---Community-related algorithm parameters
	// graph construction method
	private GraphConstructionMethod graphMethod;
	// k parameter for the k-nn graph construction
	private int knn;
	// Similarity measure for the k-nn graph construction
	private SimilarityMeasure sim;
	// Community detection algorithm
	private CommunityDetectionAlgorithm cdAlgo;
	// Steps parameter for the Walktrap algorithm
	private int wtSteps;
	// Maximum number of (overlapping) communities per user/item

	protected int numUserCommunities;
	protected int numItemCommunities;
	
	protected DenseVector BCu, BCi;
	
	// Community membership matrices for users and items
//	protected SparseMatrix userMembershipsMatrix, itemMembershipsMatrix;
	
	// User/item community membership map
	private DenseVector userMembershipsVector, itemMembershipsVector;
	
	public ComNeighSVDPlusPlusFast(SparseMatrix trainMatrix, SparseMatrix testMatrix, int fold) {
		super(trainMatrix, testMatrix, fold);

		setAlgoName("ComNeighSVD++Fast");
		
		knn = cf.getInt("graph.knn.k", 10);
		switch (cf.getString("graph.method", "ratings").toLowerCase()){
		case "tags":
			graphMethod = GraphConstructionMethod.TAGS;
			break;
		default:
		case "ratings":
			graphMethod = GraphConstructionMethod.RATINGS;
			break;
		}
		switch (cf.getString("graph.knn.sim", "cosine").toLowerCase()){
		case "pearson":
			sim = SimilarityMeasure.PEARSON_CORRELATION;
			break;
		default:
		case "cosine":
			sim = SimilarityMeasure.COSINE_SIMILARITY;
			break;
		}
		switch (cf.getString("cd.algo", "wt").toLowerCase()){
		case "dmid":
			cdAlgo = CommunityDetectionAlgorithm.DMID;
			break;
		case "slpa":
			cdAlgo = CommunityDetectionAlgorithm.SLPA;
			break;
		default:
		case "wt":
			cdAlgo = CommunityDetectionAlgorithm.WALKTRAP;
			break;
		}
		wtSteps = cf.getInt("cd.walktrap.steps", 2);
	}

	@Override
	protected void initModel() throws Exception {
		super.initModel();
		
		// build the user and item graphs
		Logs.info("{}{} build user and item graphs ...", new Object[] { algoName, foldInfo });
		GraphBuilder gb = new GraphBuilder();
		gb.setMethod(graphMethod);
		gb.setRatingData(trainMatrix);
		gb.setTaggingData(userTagTable, itemTagTable);
		gb.setK(knn);
		gb.setSimilarityMeasure(sim);
		gb.buildGraphs();
		SparseMatrix userMatrix = gb.getUserAdjacencyMatrix();
		SparseMatrix itemMatrix = gb.getItemAdjacencyMatrix();
		gb = null;
		
		// detect communities
		Logs.info("{}{} detect communities ...", new Object[] { algoName, foldInfo });
		CommunityDetector cd = new CommunityDetector();
		cd.setAlgorithm(cdAlgo);
		if (cdAlgo == CommunityDetectionAlgorithm.WALKTRAP)
			cd.setWalktrapParameters(wtSteps);
		
		cd.setGraph(userMatrix);
		cd.detectCommunities();
		userMembershipsVector = cd.getMembershipsVector();
		numUserCommunities = cd.getNumCommunities();
		
		cd.setGraph(itemMatrix);
		cd.detectCommunities();
		itemMembershipsVector = cd.getMembershipsVector();
		numItemCommunities = cd.getNumCommunities();
		
		userMatrix = null;
		itemMatrix = null;
		cd = null;

		logCommunityInfo();
		
		Y = new DenseMatrix(numItemCommunities, numFactors);
		Y.init(initMean, initStd);

{'fraction_non_alphanumeric': 0.10305526290585193, 'fraction_numerical': 0.007278996264725601, 'mean_word_length': 4.740516767454645, 'pattern_counts': {'":': 14, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5034816', 'n_tokens_mistral': 3511, 'n_tokens_neox': 3169, 'n_words': 819}