impl.Clusterer Maven / Gradle / Ivy
package impl;
import org.opencompare.api.java.Cell;
import org.opencompare.api.java.PCM;
import org.opencompare.api.java.Product;
import java.io.*;
import java.util.TreeSet;
import java.util.Set;
/**
* Created by jbferrei on 4/10/15.
*/
public class Clusterer {
public int[][] calculateProductSimilarityMatrix(PCM pcm){
int[][] simMatrix = new int[pcm.getProducts().size()][pcm.getProducts().size()];
for(int i = 0; i < pcm.getProducts().size(); i++){
for(int j = 0; j < pcm.getProducts().size(); j++){
simMatrix[i][j] = calculateProductSimilarityByIntersection(pcm.getProducts().get(i), pcm.getProducts().get(j));
}
}
return simMatrix;
}
private int calculateProductSimilarityByIntersection(Product currentProduct, Product toCompareProduct){
Set intersection = new TreeSet(getFeatureNamesWithoutEmptyValuesOfAProduct(currentProduct));
//Set intersection = new TreeSet(currentProduct.getCells().);
intersection.retainAll(getFeatureNamesWithoutEmptyValuesOfAProduct(toCompareProduct));
return intersection.size()*(-1);
}
private Set getFeatureNamesWithoutEmptyValuesOfAProduct(Product product){
Set setOfFeaturesWithContent = new TreeSet();
for(Cell cell : product.getCells())
if(cell.getContent()!="N/A")
setOfFeaturesWithContent.add(cell.getFeature().getName());
return setOfFeaturesWithContent;
}
public void printSimilarityMatrixOfProjects(int[][] simMatrix, String outputFolder, PCM pcm){
StringBuffer sb = new StringBuffer();
String[][] stringMatrix = new String[simMatrix.length+1][simMatrix.length+1];
//get product names in an array of String
String [] productNames = new String[pcm.getProducts().size()];
int counter = 0;
for(Product product : pcm.getProducts()) {
productNames[counter] = product.getName();
counter++;
}
//initialize
for(int i = 1; i