Exemplo n.º 1
0
        /// <summary>Read binary relation data from file</summary>
        /// <remarks>
        /// The expected (sparse) line format is:
        /// ENTITY_ID space/tab/comma ENTITY_ID
        /// for the relations that hold.
        /// </remarks>
        /// <param name="reader">a StreamReader to be read from</param>
        /// <param name="mapping">the mapping object for the given entity type</param>
        /// <returns>the relation data</returns>
        static public IBooleanMatrix Read(StreamReader reader, IMapping mapping)
        {
            var matrix = new SparseBooleanMatrix();

            string line;

            while ((line = reader.ReadLine()) != null)
            {
                // ignore empty lines
                if (line.Length == 0)
                {
                    continue;
                }

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

                if (tokens.Length != 2)
                {
                    throw new FormatException("Expected exactly 2 columns: " + line);
                }

                int entity1_id = mapping.ToInternalID(tokens[0]);
                int entity2_id = mapping.ToInternalID(tokens[1]);

                matrix[entity1_id, entity2_id] = true;
            }

            return(matrix);
        }
        /// <summary>Read in rating data which will be interpreted as implicit feedback data from an IDataReader, e.g. a database via DbDataReader</summary>
        /// <param name="reader">the IDataReader 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>
        /// <returns>a <see cref="IPosOnlyFeedback"/> object with the user-wise collaborative data</returns>
        static public IPosOnlyFeedback Read(IDataReader reader, float rating_threshold, IMapping user_mapping, IMapping item_mapping)
        {
            var feedback = new PosOnlyFeedback <SparseBooleanMatrix>();

            if (reader.FieldCount < 3)
            {
                throw new FormatException("Expected at least 3 columns.");
            }
            Func <string> get_user_id = reader.GetStringGetter(0);
            Func <string> get_item_id = reader.GetStringGetter(1);
            Func <float>  get_rating  = reader.GetFloatGetter(2);

            while (reader.Read())
            {
                int   user_id = user_mapping.ToInternalID(get_user_id());
                int   item_id = item_mapping.ToInternalID(get_item_id());
                float rating  = get_rating();

                if (rating >= rating_threshold)
                {
                    feedback.Add(user_id, item_id);
                }
            }

            return(feedback);
        }
Exemplo n.º 3
0
        /// <summary>Read binary relation data from an IDataReader, e.g. a database via DbDataReader</summary>
        /// <param name="reader">an IDataReader to be read from</param>
        /// <param name="mapping">the mapping object for the given entity type</param>
        /// <returns>the relation data</returns>
        static public IBooleanMatrix Read(IDataReader reader, IMapping mapping)
        {
            if (reader.FieldCount < 2)
            {
                throw new FormatException("Expected at least 2 columns.");
            }

            var matrix = new SparseBooleanMatrix();

            Func <string> get_e1_id = reader.GetStringGetter(0);
            Func <string> get_e2_id = reader.GetStringGetter(1);

            while (!reader.Read())
            {
                int entity1_id = mapping.ToInternalID(get_e1_id());
                int entity2_id = mapping.ToInternalID(get_e2_id());

                matrix[entity1_id, entity2_id] = true;
            }

            return(matrix);
        }
Exemplo n.º 4
0
        private IRatings ReadData(string path, IMapping usersMap, IMapping itemsMap)
        {
            var ratings = new MyMediaLite.Data.Ratings();

            File.ReadAllLines(path).Select(l =>
            {
                var parts = l.Split(new string[] { "::" }, StringSplitOptions.RemoveEmptyEntries);
                return(new { User = parts[0], Item = parts[1], Rating = float.Parse(parts[2]) });
            }).ToList()
            .ForEach(r =>
                     ratings.Add(usersMap.ToInternalID(r.User), itemsMap.ToInternalID(r.Item), r.Rating));

            return(ratings);
        }
Exemplo n.º 5
0
		/// <summary>Read binary relation data from file</summary>
		/// <remarks>
		/// The expected (sparse) line format is:
		/// ENTITY_ID space/tab/comma ENTITY_ID
		/// for the relations that hold.
		/// </remarks>
		/// <param name="reader">a StreamReader to be read from</param>
		/// <param name="mapping">the mapping object for the given entity type</param>
		/// <returns>the relation data</returns>
		static public IBooleanMatrix Read(StreamReader reader, IMapping mapping)
		{
			var matrix = new SparseBooleanMatrix();

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

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

				if (tokens.Length != 2)
					throw new FormatException("Expected exactly 2 columns: " + line);

				int entity1_id = mapping.ToInternalID(tokens[0]);
				int entity2_id = mapping.ToInternalID(tokens[1]);

				matrix[entity1_id, entity2_id] = true;
			}

			return matrix;
		}
Exemplo n.º 6
0
        /// <summary>Read binary attribute data from an IDataReader, e.g. a database via DbDataReader</summary>
        /// <param name="reader">an IDataReader to be read from</param>
        /// <param name="mapping">the mapping object for the given entity type</param>
        /// <returns>the attribute data</returns>
        static public IBooleanMatrix Read(IDataReader reader, IMapping mapping)
        {
            if (reader.FieldCount < 2)
            {
                throw new Exception("Expected at least 2 columns.");
            }

            var matrix = new SparseBooleanMatrix();

            while (!reader.Read())
            {
                int entity_id = mapping.ToInternalID(reader.GetString(0));
                int attr_id   = reader.GetInt32(1);

                matrix[entity_id, attr_id] = true;
            }

            return(matrix);
        }
        Read(IDataReader reader, IMapping user_mapping, IMapping item_mapping)
        {
            var ratings = new Ratings();

            if (reader.FieldCount < 3)
            {
                throw new FormatException("Expected at least 3 columns.");
            }

            Func <string> get_user_id = reader.GetStringGetter(0);
            Func <string> get_item_id = reader.GetStringGetter(1);
            Func <float>  get_rating  = reader.GetFloatGetter(2);

            while (reader.Read())
            {
                int   user_id = user_mapping.ToInternalID(get_user_id());
                int   item_id = item_mapping.ToInternalID(get_item_id());
                float rating  = get_rating();

                ratings.Add(user_id, item_id, rating);
            }
            ratings.InitScale();
            return(ratings);
        }
Exemplo n.º 8
0
        /// <summary>Read in implicit feedback data from an IDataReader, e.g. a database via DbDataReader</summary>
        /// <param name="reader">the IDataReader 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>
        /// <returns>a <see cref="IPosOnlyFeedback"/> object with the user-wise collaborative data</returns>
        public static IPosOnlyFeedback Read(IDataReader reader, IMapping user_mapping, IMapping item_mapping)
        {
            var feedback = new PosOnlyFeedback<SparseBooleanMatrix>();

            if (reader.FieldCount < 2)
                throw new FormatException("Expected at least 2 columns.");

            Func<string> get_user_id = reader.GetStringGetter(0);
            Func<string> get_item_id = reader.GetStringGetter(1);

            while (reader.Read())
            {
                int user_id = user_mapping.ToInternalID(get_user_id());
                int item_id = item_mapping.ToInternalID(get_item_id());

                feedback.Add(user_id, item_id);
            }

            return feedback;
        }
Exemplo n.º 9
0
        /// <summary>Read in rating data from an IDataReader, e.g. a database via DbDataReader</summary>
        /// <param name="reader">the <see cref="IDataReader"/> to read from</param>
        /// <param name="user_mapping">mapping object for user IDs</param>
        /// <param name="item_mapping">mapping object for item IDs</param>
        /// <returns>the rating data</returns>
        public static IRatings Read(IDataReader reader, IMapping user_mapping, IMapping item_mapping)
        {
            var ratings = new Ratings();

            if (reader.FieldCount < 3)
                throw new FormatException("Expected at least 3 columns.");

            Func<string> get_user_id = reader.GetStringGetter(0);
            Func<string> get_item_id = reader.GetStringGetter(1);
            Func<float>  get_rating  = reader.GetFloatGetter(2);

            while (reader.Read())
            {
                int user_id = user_mapping.ToInternalID(get_user_id());
                int item_id = item_mapping.ToInternalID(get_item_id());
                float rating = get_rating();

                ratings.Add(user_id, item_id, rating);
            }
            ratings.InitScale();
            return ratings;
        }
Exemplo n.º 10
0
		/// <summary>Read binary attribute data from an IDataReader, e.g. a database via DbDataReader</summary>
		/// <param name="reader">an IDataReader to be read from</param>
		/// <param name="mapping">the mapping object for the given entity type</param>
		/// <returns>the attribute data</returns>
		static public IBooleanMatrix Read(IDataReader reader, IMapping mapping)
		{
			if (reader.FieldCount < 2)
				throw new Exception("Expected at least 2 columns.");

			var matrix = new SparseBooleanMatrix();

			while (!reader.Read())
			{
				int entity_id = mapping.ToInternalID(reader.GetString(0));
				int attr_id   = reader.GetInt32(1);

				matrix[entity_id, attr_id] = true;
			}

			return matrix;
		}
Exemplo n.º 11
0
		/// <summary>Read binary relation data from an IDataReader, e.g. a database via DbDataReader</summary>
		/// <param name="reader">an IDataReader to be read from</param>
		/// <param name="mapping">the mapping object for the given entity type</param>
		/// <returns>the relation data</returns>
		static public IBooleanMatrix Read(IDataReader reader, IMapping mapping)
		{
			if (reader.FieldCount < 2)
				throw new FormatException("Expected at least 2 columns.");

			var matrix = new SparseBooleanMatrix();

			Func<string> get_e1_id = reader.GetStringGetter(0);
			Func<string> get_e2_id = reader.GetStringGetter(1);

			while (!reader.Read())
			{
				int entity1_id = mapping.ToInternalID(get_e1_id());
				int entity2_id = mapping.ToInternalID(get_e2_id());

				matrix[entity1_id, entity2_id] = true;
			}

			return matrix;
		}