Permalink
Browse files

further code sharing between command line programs

  • Loading branch information...
1 parent b852c11 commit 7139dbb614cc8a6fe2b3c1dd325304157032419b @zenogantner committed Jul 8, 2012
@@ -18,11 +18,13 @@
using System;
using System.Collections.Generic;
using System.Globalization;
+using System.IO;
using System.Linq;
using Mono.Options;
using MyMediaLite;
using MyMediaLite.Data;
using MyMediaLite.DataType;
+using MyMediaLite.IO;
using MyMediaLite.Util;
public abstract class CommandLineProgram<T> where T:IRecommender
@@ -191,6 +193,37 @@ protected virtual void Run(string[] args)
CheckParameters(extra_args);
}
+ protected virtual void LoadData()
+ {
+ training_file = Path.Combine(data_dir, training_file);
+
+ // user attributes
+ if (user_attributes_file != null)
+ user_attributes = AttributeData.Read(Path.Combine(data_dir, user_attributes_file), user_mapping);
+ if (recommender is IUserAttributeAwareRecommender)
+ ((IUserAttributeAwareRecommender)recommender).UserAttributes = user_attributes;
+
+ // item attributes
+ if (item_attributes_file != null)
+ item_attributes = AttributeData.Read(Path.Combine(data_dir, item_attributes_file), item_mapping);
+ if (recommender is IItemAttributeAwareRecommender)
+ ((IItemAttributeAwareRecommender)recommender).ItemAttributes = item_attributes;
+
+ // user relation
+ if (recommender is IUserRelationAwareRecommender)
+ {
+ ((IUserRelationAwareRecommender)recommender).UserRelation = RelationData.Read(Path.Combine(data_dir, user_relations_file), user_mapping);
+ Console.WriteLine("relation over {0} users", ((IUserRelationAwareRecommender)recommender).NumUsers);
+ }
+
+ // item relation
+ if (recommender is IItemRelationAwareRecommender)
+ {
+ ((IItemRelationAwareRecommender)recommender).ItemRelation = RelationData.Read(Path.Combine(data_dir, item_relations_file), item_mapping);
+ Console.WriteLine("relation over {0} items", ((IItemRelationAwareRecommender)recommender).NumItems);
+ }
+ }
+
protected void Abort(string message)
{
Console.Error.WriteLine(message);
@@ -215,7 +215,7 @@ protected override void Run(string[] args)
if (load_item_mapping_file != null)
item_mapping = EntityMappingExtensions.LoadMapping(load_item_mapping_file);
- // load all the data -- TODO generalize
+ // load all the data
LoadData();
Console.Write(training_data.Statistics(test_data, user_attributes, item_attributes));
@@ -393,41 +393,17 @@ protected override void CheckParameters(IList<string> extra_args)
}
}
- void LoadData()
+ protected override void LoadData()
{
TimeSpan loading_time = Wrap.MeasureTime(delegate() {
+ base.LoadData();
+
// training data
training_file = Path.Combine(data_dir, training_file);
training_data = double.IsNaN(rating_threshold)
? ItemData.Read(training_file, user_mapping, item_mapping, file_format == ItemDataFileFormat.IGNORE_FIRST_LINE)
: ItemDataRatingThreshold.Read(training_file, rating_threshold, user_mapping, item_mapping, file_format == ItemDataFileFormat.IGNORE_FIRST_LINE);
- // user attributes
- if (user_attributes_file != null)
- user_attributes = AttributeData.Read(Path.Combine(data_dir, user_attributes_file), user_mapping);
- if (recommender is IUserAttributeAwareRecommender)
- ((IUserAttributeAwareRecommender)recommender).UserAttributes = user_attributes;
-
- // item attributes
- if (item_attributes_file != null)
- item_attributes = AttributeData.Read(Path.Combine(data_dir, item_attributes_file), item_mapping);
- if (recommender is IItemAttributeAwareRecommender)
- ((IItemAttributeAwareRecommender)recommender).ItemAttributes = item_attributes;
-
- // user relation
- if (recommender is IUserRelationAwareRecommender)
- {
- ((IUserRelationAwareRecommender)recommender).UserRelation = RelationData.Read(Path.Combine(data_dir, user_relations_file), user_mapping);
- Console.WriteLine("relation over {0} users", ((IUserRelationAwareRecommender)recommender).NumUsers);
- }
-
- // item relation
- if (recommender is IItemRelationAwareRecommender)
- {
- ((IItemRelationAwareRecommender)recommender).ItemRelation = RelationData.Read(Path.Combine(data_dir, item_relations_file), item_mapping);
- Console.WriteLine("relation over {0} items", ((IItemRelationAwareRecommender)recommender).NumItems);
- }
-
// user groups
if (user_groups_file != null)
{
@@ -49,7 +49,7 @@ public class RatingPrediction : CommandLineProgram<RatingPredictor>
bool in_training_items;
bool in_test_items;
bool all_items;
-
+
bool search_hp = false;
string prediction_line = "{0}\t{1}\t{2}";
string prediction_header = null;
@@ -211,7 +211,7 @@ protected override void Run(string[] args)
item_mapping = EntityMappingExtensions.LoadMapping(load_item_mapping_file);
// load all the data
- LoadData(!online_eval);
+ LoadData();
// if requested, save ID mappings
if (save_user_mapping_file != null)
@@ -413,11 +413,13 @@ protected override void CheckParameters(IList<string> extra_args)
}
}
- void LoadData(bool static_data)
+ protected override void LoadData()
{
- training_file = Path.Combine(data_dir, training_file);
-
+ bool static_data = !online_eval;
+
TimeSpan loading_time = Wrap.MeasureTime(delegate() {
+ base.LoadData();
+
// read training data
if ((recommender is TimeAwareRatingPredictor || chronological_split != null) && file_format != RatingFileFormat.MOVIELENS_1M)
{
@@ -440,32 +442,6 @@ void LoadData(bool static_data)
}
recommender.Ratings = training_data;
- // user attributes
- if (user_attributes_file != null)
- user_attributes = AttributeData.Read(Path.Combine(data_dir, user_attributes_file), user_mapping);
- if (recommender is IUserAttributeAwareRecommender)
- ((IUserAttributeAwareRecommender)recommender).UserAttributes = user_attributes;
-
- // item attributes
- if (item_attributes_file != null)
- item_attributes = AttributeData.Read(Path.Combine(data_dir, item_attributes_file), item_mapping);
- if (recommender is IItemAttributeAwareRecommender)
- ((IItemAttributeAwareRecommender)recommender).ItemAttributes = item_attributes;
-
- // user relation
- if (recommender is IUserRelationAwareRecommender)
- {
- ((IUserRelationAwareRecommender)recommender).UserRelation = RelationData.Read(Path.Combine(data_dir, user_relations_file), user_mapping);
- Console.WriteLine("relation over {0} users", ((IUserRelationAwareRecommender)recommender).NumUsers);
- }
-
- // item relation
- if (recommender is IItemRelationAwareRecommender)
- {
- ((IItemRelationAwareRecommender)recommender).ItemRelation = RelationData.Read(Path.Combine(data_dir, item_relations_file), item_mapping);
- Console.WriteLine("relation over {0} items", ((IItemRelationAwareRecommender)recommender).NumItems);
- }
-
// read test data
if (test_file != null)
{

0 comments on commit 7139dbb

Please sign in to comment.