英文:
Mahout - How to read a custom input file?
问题
我关注了这个话题,并使用MemoryIDMigrator为我的产品获取了一个长ID:
https://stackoverflow.com/questions/7209624/mahout-to-read-a-custom-input-file
但是我无法像这样初始化IDMigrator,因为父构造函数需要首先调用:
public MemoryIDMigrator memoryIDMigrator;
public CustomFileDataModel(File dataFile) throws IOException {
this.memoryIDMigrator = new MemoryIDMigrator();
super(dataFile);
}
@Override
protected long readItemIDFromString(String stringID) {
long result = this.memoryIDMigrator.toLongID(stringID);
this.memoryIDMigrator.storeMapping(result, stringID);
return result;
}
我应该如何传递这个参数以获取我的String ID?我需要类似于这样的内容:
List<RecommendedItem> recommendations = recommender.recommend(2, 5);
for (RecommendedItem recommendation : recommendations) {
System.out.println(memoryIDMigrator.toStringID(recommendation.getItemID()));
}
英文:
I follow this topic and use MemoryIDMigrator to get a long ID for my products :
https://stackoverflow.com/questions/7209624/mahout-to-read-a-custom-input-file
But I can't initialize IDMigrator like this because the parent constructor need to come first :
public MemoryIDMigrator memoryIDMigrator;
public CustomFileDataModel(File dataFile) throws IOException {
this.memoryIDMigrator = new MemoryIDMigrator();
super(dataFile);
}
@Override
protected long readItemIDFromString(String stringID) {
long result = this.memoryIDMigrator.toLongID(stringID);
this.memoryIDMigrator.storeMapping(result, stringID);
return result;
}
How could I give this parameter to get back my String ID ? I need something like this :
List<RecommendedItem> recommendations = recommender.recommend(2, 5);
for (RecommendedItem recommendation : recommendations) {
System.out.println(memoryIDMigrator.toStringID(recommendation.getItemID());
}
答案1
得分: 0
我刚找到了一个解决方案!在我看来,这并不是最佳的方法,但它能够工作。
当我的覆盖函数尝试读取一个项目时,我阻止了如果已经创建了新映射的情况:
public class CustomFileDataModel extends FileDataModel {
public MemoryIDMigrator memoryIDMigrator;
private boolean loaded;
public CustomFileDataModel(File dataFile) throws IOException {
super(dataFile);
}
@Override
protected long readItemIDFromString(String stringID) {
if (!this.loaded) {
this.memoryIDMigrator = new MemoryIDMigrator();
this.loaded = true;
}
long result = this.memoryIDMigrator.toLongID(stringID);
this.memoryIDMigrator.storeMapping(result, stringID);
return result;
}
}
类似地,如果您想使用自定义的用户ID,您可以覆盖 readUserIDFromString()
函数。
然后,要恢复我的字符串ID,我像这样进行操作:
public static void main( String[] args ) throws IOException, TasteException {
CustomFileDataModel model = new CustomFileDataModel(new File("path/to/data.csv"));
UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model);
UserBasedRecommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
List<RecommendedItem> recommendations = recommender.recommend(2, 5);
for (RecommendedItem recommendation : recommendations) {
System.out.println(model.memoryIDMigrator.toStringID(recommendation.getItemID()));
}
}
英文:
I just found a solution ! In my opinion it's not the best way to do it but it works.
When my override function try to read an item, I prevent the creation of a new map if it had already been created :
public class CustomFileDataModel extends FileDataModel {
public MemoryIDMigrator memoryIDMigrator;
private boolean loaded;
public CustomFileDataModel(File dataFile) throws IOException {
super(dataFile);
}
@Override
protected long readItemIDFromString(String stringID) {
if (!this.loaded) {
this.memoryIDMigrator = new MemoryIDMigrator();
this.loaded = true;
}
long result = this.memoryIDMigrator.toLongID(stringID);
this.memoryIDMigrator.storeMapping(result, stringID);
return result;
}
}
Similarly, if you want to use a custom user ID, you can override the readUserIDFromString()
function.
And then to recover my string ID, I proceed like this :
public static void main( String[] args ) throws IOException, TasteException {
CustomFileDataModel model = new CustomFileDataModel(new File("path/to/data.csv"));
UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model);
UserBasedRecommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
List<RecommendedItem> recommendations = recommender.recommend(2, 5);
for (RecommendedItem recommendation : recommendations) {
System.out.println(model.memoryIDMigrator.toStringID(recommendation.getItemID()));
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论