Mahout – 如何读取自定义输入文件?

huangapple 未分类评论50阅读模式
英文:

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&lt;RecommendedItem&gt; 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(&quot;path/to/data.csv&quot;));

    UserSimilarity similarity = new PearsonCorrelationSimilarity(model);

    UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model);

    UserBasedRecommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);

    List&lt;RecommendedItem&gt; recommendations = recommender.recommend(2, 5);
    for (RecommendedItem recommendation : recommendations) {
        System.out.println(model.memoryIDMigrator.toStringID(recommendation.getItemID()));
    }
}

huangapple
  • 本文由 发表于 2020年4月4日 02:38:22
  • 转载请务必保留本文链接:https://java.coder-hub.com/61018401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定