
org.reco4j.similarity.CosineSimilarity Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of reco4j-core Show documentation
Show all versions of reco4j-core Show documentation
Reco4j is an open source project aims at developing a recommendation framework based on graph data sources. We choose graph databases for several reasons. They are NoSQL databases, so "schemaless". This means that it is possible to extend the basic data structure with intermediate information, i.e. similarity value between item and so on. Moreover, since every information is expressed with properties, nodes and relations, the recommendation process can be customized to work on every graph.
Reco4j can be used on every graph where "user" and "item" is represented by node and the preferences are modelled as relationship between them.
Current implementation leverage on Neo4j as first graph database integrated in our framework.
The newest version!
/*
* CosineSimilarity.java
*
* Copyright (C) 2013 Alessandro Negro
*
* This program 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.
*
* This program 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 this program. If not, see .
*/
package org.reco4j.similarity;
import java.util.HashMap;
import java.util.List;
import org.reco4j.graph.IEdge;
import org.reco4j.graph.IEdgeType;
import org.reco4j.graph.INode;
/**
*
* @author Alessandro Negro
*/
public class CosineSimilarity
extends BasicSimilarity
{
public CosineSimilarity(ICosineSimilarityConfig config)
{
super(config);
}
@Override
public double getSimilarity(INode x, INode y, IEdgeType edgeType)
{
HashMap totalUserMap = new HashMap();
HashMap totalXUserMap = new HashMap();
HashMap totalYUserMap = new HashMap();
List xInEdge = x.getInEdge(edgeType);
for (IEdge edge : xInEdge)
{
insertUserOnce(totalUserMap, edge.getSource());
insertUserOnce(totalXUserMap, edge.getSource());
}
List yInEdge = y.getInEdge(edgeType);
for (IEdge edge : yInEdge)
{
insertUserOnce(totalUserMap, edge.getSource());
insertUserOnce(totalYUserMap, edge.getSource());
}
int userTotalNumber = totalUserMap.size();
int[] xVector = new int[userTotalNumber];
int[] yVector = new int[userTotalNumber];
int pos = 0;
for (INode user : totalUserMap.values())
{
if (totalXUserMap.containsKey(user.getProperty(getConfig().getUserIdentifierName())))
xVector[pos] = 1;
else
xVector[pos] = 0;
if (totalYUserMap.containsKey(user.getProperty(getConfig().getUserIdentifierName())))
yVector[pos] = 1;
else
yVector[pos] = 0;
pos++;
}
return calculateCosineValue(xVector, yVector);
}
private double calculateCosineValue(int[] xVector, int[] yVector)
{
double a = (double) getDotProduct(xVector, yVector);
double b = getNorm(xVector) * getNorm(yVector);
if (b > 0)
return a / b;
else
return 0;
}
private int getDotProduct(int[] xVector, int[] yVector)
{
int sum = 0;
for (int i = 0, n = xVector.length; i < n; i++)
{
sum += xVector[i] * yVector[i];
}
return sum;
}
private double getNorm(int[] xVector)
{
int sum = 0;
for (int i = 0, n = xVector.length; i < n; i++)
{
sum += xVector[i] * xVector[i];
}
return Math.sqrt(sum);
}
private void insertUserOnce(HashMap totalUserMap, INode user)
{
String userId = user.getProperty(getConfig().getUserIdentifierName());
if (totalUserMap.get(userId) == null)
totalUserMap.put(userId, user);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy