Exemple #1
0
        /// <summary>Read in rating data from a file</summary>
        /// <param name="filename">the name of the file to read from</param>
        /// <param name="user_mapping">mapping object for user IDs</param>
        /// <param name="item_mapping">mapping object for item IDs</param>
        /// <param name="test_rating_format">whether there is a rating column in each line or not</param>
        /// <param name="ignore_first_line">if true, ignore the first line</param>
        /// <returns>the rating data</returns>
        static public ITimedRatings Read(
            string filename,
            IMapping user_mapping = null, IMapping item_mapping = null,
            TestRatingFileFormat test_rating_format = TestRatingFileFormat.WITH_RATINGS,
            bool ignore_first_line = false)
        {
            string binary_filename = filename + ".bin.TimedRatings";

            if (FileSerializer.Should(user_mapping, item_mapping) && File.Exists(binary_filename))
            {
                return((ITimedRatings)FileSerializer.Deserialize(binary_filename));
            }

            return(Wrap.FormatException <ITimedRatings>(filename, delegate() {
                using (var reader = new StreamReader(filename))
                {
                    var ratings = (TimedRatings)Read(reader, user_mapping, item_mapping);
                    if (FileSerializer.Should(user_mapping, item_mapping) && FileSerializer.CanWrite(binary_filename))
                    {
                        ratings.Serialize(binary_filename);
                    }
                    return ratings;
                }
            }));
        }
Exemple #2
0
        /// <summary>Read in implicit feedback data from a file</summary>
        /// <param name="filename">name of the file to be read from</param>
        /// <param name="user_mapping">user <see cref="IMapping"/> object</param>
        /// <param name="item_mapping">item <see cref="IMapping"/> object</param>
        /// <param name="ignore_first_line">if true, ignore the first line</param>
        /// <returns>a <see cref="IPosOnlyFeedback"/> object with the user-wise collaborative data</returns>
        static public IPosOnlyFeedback Read(string filename, IMapping user_mapping = null, IMapping item_mapping = null, bool ignore_first_line = false)
        {
            string binary_filename = filename + ".bin.PosOnlyFeedback";

            if (FileSerializer.Should(user_mapping, item_mapping) && File.Exists(binary_filename))
            {
                return((IPosOnlyFeedback)FileSerializer.Deserialize(binary_filename));
            }

            return(Wrap.FormatException <IPosOnlyFeedback>(filename, delegate() {
                using (var reader = new StreamReader(filename))
                {
                    var feedback_data = (ISerializable)Read(reader, user_mapping, item_mapping);
                    if (FileSerializer.Should(user_mapping, item_mapping) && FileSerializer.CanWrite(binary_filename))
                    {
                        feedback_data.Serialize(binary_filename);
                    }
                    return (IPosOnlyFeedback)feedback_data;
                }
            }));
        }
        /// <summary>Read in rating data which will be interpreted as implicit feedback data from a file</summary>
        /// <param name="filename">name of the file to be read from</param>
        /// <param name="rating_threshold">the minimum rating value needed to be accepted as positive feedback</param>
        /// <param name="user_mapping">user <see cref="IMapping"/> object</param>
        /// <param name="item_mapping">item <see cref="IMapping"/> object</param>
        /// <param name="ignore_first_line">if true, ignore the first line</param>
        /// <returns>a <see cref="IPosOnlyFeedback"/> object with the user-wise collaborative data</returns>
        static public IPosOnlyFeedback Read(string filename, float rating_threshold, IMapping user_mapping = null, IMapping item_mapping = null, bool ignore_first_line = false)
        {
            string binary_filename = string.Format(CultureInfo.InvariantCulture, "{0}.bin.PosOnlyFeedbackThreshold-{1}", filename, rating_threshold);

            if (FileSerializer.Should(user_mapping, item_mapping) && File.Exists(binary_filename))
            {
                return((IPosOnlyFeedback)FileSerializer.Deserialize(binary_filename));
            }

            return(Wrap.FormatException <IPosOnlyFeedback>(filename, delegate() {
                using (var reader = new StreamReader(filename))
                {
                    var feedback_data = (ISerializable)Read(reader, rating_threshold, user_mapping, item_mapping);
                    if (FileSerializer.Should(user_mapping, item_mapping) && FileSerializer.CanWrite(binary_filename))
                    {
                        feedback_data.Serialize(binary_filename);
                    }
                    return (IPosOnlyFeedback)feedback_data;
                }
            }));
        }
Exemple #4
0
        /// <summary>Read in static rating data from a file</summary>
        /// <param name="filename">the name of the file to read from</param>
        /// <param name="user_mapping">mapping object for user IDs</param>
        /// <param name="item_mapping">mapping object for item IDs</param>
        /// <param name="rating_type">the data type to be used for storing the ratings</param>
        /// <param name="test_rating_format">whether there is a rating column in each line or not</param>
        /// <param name="ignore_first_line">if true, ignore the first line</param>
        /// <returns>the rating data</returns>
        static public IRatings Read(
            string filename,
            IMapping user_mapping  = null, IMapping item_mapping = null,
            RatingType rating_type = RatingType.FLOAT,
            TestRatingFileFormat test_rating_format = TestRatingFileFormat.WITH_RATINGS,
            bool ignore_first_line = false)
        {
            string binary_filename = filename + ".bin.StaticRatings";

            if (FileSerializer.Should(user_mapping, item_mapping) && File.Exists(binary_filename))
            {
                return((IRatings)FileSerializer.Deserialize(binary_filename));
            }

            int size = 0;

            using (var reader = new StreamReader(filename))
                while (reader.ReadLine() != null)
                {
                    size++;
                }
            if (ignore_first_line)
            {
                size--;
            }

            return(Wrap.FormatException <IRatings>(filename, delegate() {
                using (var reader = new StreamReader(filename))
                {
                    var ratings = (StaticRatings)Read(reader, size, user_mapping, item_mapping, rating_type, test_rating_format);
                    if (FileSerializer.Should(user_mapping, item_mapping) && FileSerializer.CanWrite(binary_filename))
                    {
                        ratings.Serialize(binary_filename);
                    }
                    return ratings;
                }
            }));
        }