Пример #1
0
        public SVDRecommender(DataModel dataModel, Factorizer factorizer, CandidateItemsStrategy candidateItemsStrategy, PersistenceStrategy persistenceStrategy)
            : base(dataModel, candidateItemsStrategy)
        {
            Action refreshRunnable = null;

            this.factorizer          = factorizer;
            this.persistenceStrategy = persistenceStrategy;
            try
            {
                this.factorization = persistenceStrategy.load();
            }
            catch (IOException exception)
            {
                throw new TasteException("Error loading factorization", exception);
            }
            if (this.factorization == null)
            {
                this.train();
            }
            if (refreshRunnable == null)
            {
                refreshRunnable = () => this.train();
            }
            this.refreshHelper = new RefreshHelper(refreshRunnable);
            this.refreshHelper.addDependency(this.getDataModel());
            this.refreshHelper.addDependency(factorizer);
            this.refreshHelper.addDependency(candidateItemsStrategy);
        }
Пример #2
0
 public override bool Equals(object o)
 {
     if (o is Factorization)
     {
         Factorization factorization = (Factorization)o;
         return(((this.userIDMapping.Equals(factorization.userIDMapping) && this.itemIDMapping.Equals(factorization.itemIDMapping)) && Utils.ArrayDeepEquals(this.userFeatures, factorization.userFeatures)) && Utils.ArrayDeepEquals(this.itemFeatures, factorization.itemFeatures));
     }
     return(false);
 }
Пример #3
0
        protected static void writeBinary(Factorization factorization, Stream outFile)
        {
            int          num2;
            BinaryWriter writer = new BinaryWriter(outFile);

            writer.Write(factorization.numFeatures());
            writer.Write(factorization.numUsers());
            writer.Write(factorization.numItems());
            foreach (KeyValuePair <long, int?> pair in factorization.getUserIDMappings())
            {
                if (pair.Value.HasValue)
                {
                    long key = pair.Key;
                    writer.Write(pair.Value.Value);
                    writer.Write(key);
                    try
                    {
                        double[] numArray = factorization.getUserFeatures(key);
                        num2 = 0;
                        while (num2 < factorization.numFeatures())
                        {
                            writer.Write(numArray[num2]);
                            num2++;
                        }
                    }
                    catch (NoSuchUserException exception)
                    {
                        throw new IOException("Unable to persist factorization", exception);
                    }
                }
            }
            foreach (KeyValuePair <long, int?> pair2 in factorization.getItemIDMappings())
            {
                if (pair2.Value.HasValue)
                {
                    long num3 = pair2.Key;
                    writer.Write(pair2.Value.Value);
                    writer.Write(num3);
                    try
                    {
                        double[] numArray2 = factorization.getItemFeatures(num3);
                        for (num2 = 0; num2 < factorization.numFeatures(); num2++)
                        {
                            writer.Write(numArray2[num2]);
                        }
                    }
                    catch (NoSuchItemException exception2)
                    {
                        throw new IOException("Unable to persist factorization", exception2);
                    }
                }
            }
        }
Пример #4
0
 private void train()
 {
     this.factorization = this.factorizer.factorize();
     try
     {
         this.persistenceStrategy.maybePersist(this.factorization);
     }
     catch (IOException exception)
     {
         throw new TasteException("Error persisting factorization", exception);
     }
 }
Пример #5
0
        public void maybePersist(Factorization factorization)
        {
            Stream outFile = null;

            try
            {
                log.info("Writing factorization to {0}...", new object[] { this.file });
                outFile = new FileStream(this.file, FileMode.OpenOrCreate, FileAccess.Write);
                writeBinary(factorization, outFile);
            }
            finally
            {
                outFile.Close();
            }
        }