All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.reco4j.util.Utility Maven / Gradle / Ivy

Go to download

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!
/*
 * Utility.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.util;

import java.util.ArrayList;
import java.util.Iterator;
import org.reco4j.graph.INode;
import org.reco4j.model.Rating;

/**
 *
 * @author Alessandro Negro 
 */
public class Utility
{
  public static ArrayList cutList(ArrayList recommendations, int recoNumber)
  {
    ArrayList recommendationsRes = new ArrayList();

    int step = 0;
    for (Rating rate : recommendations)
    {
      if (step > recoNumber)
        break;
      step++;
      recommendationsRes.add(step++, rate);
    }
    return recommendationsRes;
  }

//  public static HashMap getKNNRow(ArrayList recommendations, String itemIdentifierName, int kvalue)
//  {
//    HashMap knnRow = new HashMap();
//
//    int step = 0;
//    for (Rating rate : recommendations)
//    {
//      if (step > kvalue)
//        break;
//      step++;
//      knnRow.put(rate.getItem().getProperty(itemIdentifierName), rate);
//    }
//    return knnRow;
//  }
//  
//  public static FastByIDMap getFastKNNRow(ArrayList recommendations, String itemIdentifierName, int kvalue)
//  {
//    FastByIDMap knnRow = new FastByIDMap();
//
//    int step = 0;
//    for (Rating rate : recommendations)
//    {
//      if (step > kvalue)
//        break;
//      step++;
//      String itemId = rate.getItem().getProperty(itemIdentifierName);
//      knnRow.put(Long.parseLong(itemId), rate);
//    }
//    return knnRow;
//  }

  public static void orderedInsert(ArrayList recommendations, double estimatedRating, INode item)
  {
    //Da sistemare per prevedere il caso in cui ci siano più edge type che stiamo considerando
    //e quindi lo stesso nodo potrebbe comparire più volte
    int index = 0;
    Iterator recIterator = recommendations.iterator();
    while (recIterator.hasNext() && recIterator.next().getRate() > estimatedRating)
    {
      index++;
      continue;
    }
    Rating rate = new Rating(item, estimatedRating);
    recommendations.add(index, rate);
  }
  public static void orderedInsert(ArrayList recommendations, double estimatedRating, INode item, int size)
  {
    int index = 0;
    Iterator recIterator = recommendations.iterator();
    while (recIterator.hasNext() && recIterator.next().getRate() > estimatedRating)
    {
      index++;
      if (index > size)
        return;
      else
        continue;
    }
    Rating rate = new Rating(item, estimatedRating);
    recommendations.add(index, rate);
    if (recommendations.size() > size)
      recommendations.remove(size);
  }
  public static void orderedInsert(ArrayList recommendations, double estimatedRating, INode item, INode user, int size)
  {
    int index = 0;
    Iterator recIterator = recommendations.iterator();
    while (recIterator.hasNext() && recIterator.next().getRate() > estimatedRating)
    {
      index++;
      if (index > size)
        return;
      else
        continue;
    }
    Rating rate = new Rating(user, item, estimatedRating, null);
    recommendations.add(index, rate);
    if (recommendations.size() > size)
      recommendations.remove(size);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy