Array-based storage for rating data.
Very memory-efficient. This data structure does NOT support incremental updates.
Inheritance: StaticRatings
Exemplo n.º 1
0
        /// <summary>Read in static rating data from a TextReader</summary>
        /// <param name="reader">the <see cref="TextReader"/> to read from</param>
        /// <param name="size">the number of ratings in the file</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>
        /// <returns>the rating data</returns>
        public static IRatings Read(TextReader reader, int size,
            IEntityMapping user_mapping, IEntityMapping item_mapping,
            RatingType rating_type)
        {
            IRatings ratings;
            if (rating_type == RatingType.BYTE)
                ratings = new StaticByteRatings(size);
            else if (rating_type == RatingType.FLOAT)
                ratings = new StaticFloatRatings(size);
            else
                ratings = new StaticRatings(size);

            var split_chars = new char[]{ '\t', ' ', ',' };
            string line;

            while ( (line = reader.ReadLine()) != null )
            {
                if (line.Length == 0)
                    continue;

                string[] tokens = line.Split(split_chars);

                if (tokens.Length < 3)
                    throw new IOException("Expected at least three columns: " + line);

                int user_id = user_mapping.ToInternalID(int.Parse(tokens[0]));
                int item_id = item_mapping.ToInternalID(int.Parse(tokens[1]));
                double rating = double.Parse(tokens[2], CultureInfo.InvariantCulture);

                ratings.Add(user_id, item_id, rating);
            }
            return ratings;
        }
Exemplo n.º 2
0
		/// <summary>Read in rating test data (Track 1) from a TextReader</summary>
		/// <param name="reader">the <see cref="StreamReader"/> to read from</param>
		/// <returns>the rating data</returns>
		static public IRatings ReadTest(StreamReader reader)
		{
			IRatings ratings = new StaticByteRatings(GetNumberOfRatings(reader));

			string line;

			while ( (line = reader.ReadLine()) != null )
			{
				string[] tokens = line.Split('|');

				int user_id          = int.Parse(tokens[0]);
				int num_user_ratings = int.Parse(tokens[1]); // number of ratings for this user

				for (int i = 0; i < num_user_ratings; i++)
				{
					line = reader.ReadLine();

					tokens = line.Split('\t');

					int item_id = int.Parse(tokens[0]);

					ratings.Add(user_id, item_id, 0);
				}
			}
			return ratings;
		}
		public void TestFull()
		{
			var ratings = new StaticByteRatings(2);
			Assert.AreEqual(0, ratings.Count);
			ratings.Add(1, 4, 3);
			Assert.AreEqual(1, ratings.Count);
			ratings.Add(1, 8, 2);
			Assert.AreEqual(2, ratings.Count);
			ratings.Add(2, 4, 2);
		}
		[Test()] public void TestMaxUserIDMaxItemID()
		{
			var ratings = new StaticByteRatings(7);
			ratings.Add(1, 4, 3);
			ratings.Add(1, 8, 2);
			ratings.Add(2, 4, 2);
			ratings.Add(2, 2, 6);
			ratings.Add(2, 5, 4);
			ratings.Add(3, 7, 2);
			ratings.Add(6, 3, 3);

			Assert.AreEqual(6, ratings.MaxUserID);
			Assert.AreEqual(8, ratings.MaxItemID);
		}
		[Test()] public void TestMinRatingMaxRating()
		{
			var ratings = new StaticByteRatings(7);
			ratings.Add(1, 4, 3);
			ratings.Add(1, 8, 2);
			ratings.Add(2, 4, 2);
			ratings.Add(2, 2, 6);
			ratings.Add(2, 5, 4);
			ratings.Add(3, 7, 2);
			ratings.Add(6, 3, 3);

			Assert.AreEqual(6, ratings.Scale.Max);
			Assert.AreEqual(2, ratings.Scale.Min);
		}
		[Test()] public void TestAdd()
		{
			var ratings = new StaticByteRatings(7);
			ratings.Add(1, 4, 3);
			Assert.AreEqual(1, ratings.Count);
			ratings.Add(1, 8, 2);
			Assert.AreEqual(2, ratings.Count);
			ratings.Add(2, 4, 2);
			ratings.Add(2, 2, 6);
			ratings.Add(2, 5, 4);
			ratings.Add(3, 7, 2);
			ratings.Add(6, 3, 3);

			Assert.AreEqual(4, ratings[2, 5]);
			Assert.AreEqual(3, ratings[1, 4]);
			Assert.AreEqual(3, ratings[6, 3]);
			Assert.AreEqual(7, ratings.Count);
		}
Exemplo n.º 7
0
        public void TestAddRating()
        {
            var ratings = new StaticByteRatings(7);
            ratings.Add(1, 4, 3);
            Assert.AreEqual(1, ratings.Count);
            ratings.Add(1, 8, 2);
            Assert.AreEqual(2, ratings.Count);
            ratings.Add(2, 4, 2);
            ratings.Add(2, 2, 6);
            ratings.Add(2, 5, 4);
            ratings.Add(3, 7, 2);
            ratings.Add(6, 3, 3);

            Assert.AreEqual(4, ratings.Get(2, 5));
            Assert.AreEqual(3, ratings.Get(1, 4));
            Assert.AreEqual(3, ratings.Get(6, 3));
            Assert.AreEqual(7, ratings.Count);
        }
Exemplo n.º 8
0
        /// <summary>Read in static rating data from a TextReader</summary>
        /// <param name="reader">the <see cref="TextReader"/> to read from</param>
        /// <param name="size">the number of ratings in the file</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="ignore_first_line">if true, ignore the first line</param>
        /// <returns>the rating data</returns>
        public static IRatings Read(
            TextReader reader, int size,
			IEntityMapping user_mapping = null, IEntityMapping item_mapping = null,
			RatingType rating_type = RatingType.FLOAT,
			bool ignore_first_line = false)
        {
            if (user_mapping == null)
                user_mapping = new IdentityMapping();
            if (item_mapping == null)
                item_mapping = new IdentityMapping();
            if (ignore_first_line)
                reader.ReadLine();

            IRatings ratings;
            if (rating_type == RatingType.BYTE)
                ratings = new StaticByteRatings(size);
            else if (rating_type == RatingType.FLOAT)
                ratings = new StaticRatings(size);
            else
                throw new FormatException(string.Format("Unknown rating type: {0}", rating_type));

            string line;
            while ((line = reader.ReadLine()) != null)
            {
                if (line.Length == 0)
                    continue;

                string[] tokens = line.Split(Constants.SPLIT_CHARS);

                if (tokens.Length < 3)
                    throw new FormatException("Expected at least 3 columns: " + line);

                int user_id = user_mapping.ToInternalID(tokens[0]);
                int item_id = item_mapping.ToInternalID(tokens[1]);
                float rating = float.Parse(tokens[2], CultureInfo.InvariantCulture);

                ratings.Add(user_id, item_id, rating);
            }
            return ratings;
        }
Exemplo n.º 9
0
		/// <summary>Read in rating data from a TextReader</summary>
		/// <param name="reader">the <see cref="StreamReader"/> to read from</param>
		/// <returns>the rating data</returns>
		static public IRatings Read80Plus(StreamReader reader)
		{
			// create ratings data structure
			IRatings ratings = new StaticByteRatings(GetNumberOfRatings(reader));

			// read in ratings
			string line;
			while ( (line = reader.ReadLine()) != null )
			{
				string[] tokens = line.Split('|');

				int user_id          = int.Parse(tokens[0]);
				int num_user_ratings = int.Parse(tokens[1]); // number of ratings for this user

				for (int i = 0; i < num_user_ratings; i++)
				{
					line = reader.ReadLine();

					tokens = line.Split('\t');

					int item_id = int.Parse(tokens[0]);
					byte rating = byte.Parse(tokens[1]);

					ratings.Add(user_id, item_id, rating >= 80 ? 1 : 0);
				}
			}
			return ratings;
		}		
Exemplo n.º 10
0
		public void TestRemoveAt()
		{
			var ratings = new StaticByteRatings(8);
			ratings.Add(1, 4, 3);
			ratings.Add(1, 8, 2);
			ratings.Add(2, 4, 2);
			ratings.Add(2, 2, 6);
			ratings.Add(2, 5, 4);
			ratings.Add(3, 7, 2);
			ratings.Add(3, 3, 3);
			ratings.Add(6, 3, 3);

			Assert.AreEqual(8, ratings.Count);
			ratings.RemoveAt(ratings.GetIndex(2, 5));
		}
Exemplo n.º 11
0
		[Test()] public void TestIndex()
		{
			var ratings = new StaticByteRatings(8);
			ratings.Add(1, 4, 3);
			ratings.Add(1, 8, 2);
			ratings.Add(2, 4, 2);
			ratings.Add(2, 2, 6);
			ratings.Add(2, 5, 4);
			ratings.Add(3, 4, 2);
			ratings.Add(3, 3, 3);
			ratings.Add(6, 3, 3);

			// test index[,]
			Assert.AreEqual(3, ratings[1, 4]);
			Assert.AreEqual(2, ratings[1, 8]);
			Assert.AreEqual(6, ratings[2, 2]);
		}
Exemplo n.º 12
0
		public void TestRemoveItem()
		{
			var ratings = new StaticByteRatings(7);
			ratings.Add(1, 4, 3);
			ratings.Add(1, 8, 2);
			ratings.Add(2, 4, 2);
			ratings.Add(2, 2, 6);
			ratings.Add(2, 5, 4);
			ratings.Add(3, 4, 2);
			ratings.Add(3, 3, 3);

			Assert.AreEqual(7, ratings.Count);
			ratings.RemoveItem(4);
		}