package dukeScrobbler; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; import java.util.Collection; import java.util.Iterator; import com.planetj.taste.common.TasteException; import com.planetj.taste.correlation.UserCorrelation; import com.planetj.taste.impl.correlation.AveragingPreferenceInferrer; import com.planetj.taste.impl.correlation.CosineMeasureCorrelation; import com.planetj.taste.impl.correlation.PearsonCorrelation; import com.planetj.taste.impl.correlation.SpearmanCorrelation; import com.planetj.taste.impl.neighborhood.NearestNUserNeighborhood; import com.planetj.taste.impl.neighborhood.ThresholdUserNeighborhood; import com.planetj.taste.model.DataModel; import com.planetj.taste.model.Item; import com.planetj.taste.model.Preference; import com.planetj.taste.model.User; import com.planetj.taste.neighborhood.UserNeighborhood; /** * gdfMaker takes a dataModel and writes a GUESS .gdf file * based on a nearest user neighborhood * using 3 neighbors and cosine correlation * * @author Ben Spain * */ public class gdfMaker { public static void makeGDF(DataModel model, File f) throws TasteException{ PrintStream output = null; try { output = new PrintStream(new FileOutputStream(f.toString(), true)); } catch (FileNotFoundException e) { System.out.println(e); } // GDF node header output.println("nodedef> name,id INT default 0"); Iterator it = model.getUsers().iterator(); String toPrint = ""; int id = 0; while(it.hasNext()){ toPrint = ""; User u = (User)it.next(); toPrint += u.getID(); toPrint += "," + id; id++; output.println(toPrint); } // GDF edge header output.println("edgedef> node1,node2,cosine DOUBLE default 0,pearson DOUBLE default 0,spearman DOUBLE default 0"); // Associate all 3 correlation values for each edge UserCorrelation cosineCorr = new CosineMeasureCorrelation(model); UserCorrelation pCorr = new PearsonCorrelation(model); UserCorrelation sCorr = new SpearmanCorrelation(model); cosineCorr.setPreferenceInferrer(new AveragingPreferenceInferrer()); pCorr.setPreferenceInferrer(new AveragingPreferenceInferrer()); sCorr.setPreferenceInferrer(new AveragingPreferenceInferrer()); UserNeighborhood neighborhood = new NearestNUserNeighborhood(3, cosineCorr, model); Iterator it3 = model.getUsers().iterator(); while(it3.hasNext()){ User u = (User)it3.next(); Collection c = neighborhood.getUserNeighborhood(u.getID()); Iterator it4 = c.iterator(); while(it4.hasNext()){ toPrint = ""; User n = (User)it4.next(); toPrint += u.getID() + "," +n.getID()+","; toPrint += cosineCorr.userCorrelation(u,n) + ","; toPrint += pCorr.userCorrelation(u,n) + ","; toPrint += sCorr.userCorrelation(u,n); System.out.println(toPrint); output.println(toPrint); } } } }