<commit_before>/*
 * LensKit, an open source recommender systems toolkit.
 * Copyright 2010-2013 Regents of the University of Minnesota and contributors
 * Work on LensKit has been funded by the National Science Foundation under
 * grants IIS 05-34939, 08-08692, 08-12148, and 10-17697.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.grouplens.lenskit.eval.graph;

import com.google.common.io.Closer;
import org.grouplens.grapht.graph.Edge;
import org.grouplens.grapht.graph.Graph;
import org.grouplens.grapht.graph.Node;
import org.grouplens.grapht.spi.AbstractSatisfactionVisitor;
import org.grouplens.grapht.spi.CachedSatisfaction;
import org.grouplens.grapht.spi.Satisfaction;
import org.grouplens.lenskit.core.LenskitRecommenderEngineFactory;
import org.grouplens.lenskit.data.dao.DataAccessObject;
import org.grouplens.lenskit.data.pref.PreferenceDomain;
import org.grouplens.lenskit.eval.AbstractCommand;
import org.grouplens.lenskit.eval.CommandException;
import org.grouplens.lenskit.eval.algorithm.LenskitAlgorithmInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class DumpGraphCommand extends AbstractCommand<File> {
    private static final Logger logger = LoggerFactory.getLogger(DumpGraphCommand.class);

    private LenskitAlgorithmInstance algorithm;
    private File output;
    private PreferenceDomain domain = null;
    private Class<? extends DataAccessObject> daoType;


    public DumpGraphCommand() {
        this("dumpGraph");
    }

    public DumpGraphCommand(String name) {
        super(name);
    }

    public DumpGraphCommand setAlgorithm(LenskitAlgorithmInstance algorithm) {
        this.algorithm = algorithm;
        return this;
    }

    public DumpGraphCommand setOutput(File f) {
        output = f;
        return this;
    }

    public DumpGraphCommand setDomain(PreferenceDomain dom) {
        domain = dom;
        return this;
    }

    public DumpGraphCommand setDaoType(Class<? extends DataAccessObject> daoType) {
        this.daoType = daoType;
        return this;
    }

    @Override
    public File call() throws CommandException {
        if (output == null) {
            logger.error("no output file specified");
            throw new IllegalStateException("no graph output file specified");
        }
        LenskitRecommenderEngineFactory factory = algorithm.getFactory().clone();
        if (domain != null) {
            factory.bind(PreferenceDomain.class).to(domain);
        }
        Graph initial = factory.getInitialGraph(daoType);
        try {
            writeGraph(factory.getInitialGraph(daoType), output);
        } catch (IOException e) {
            throw new CommandException("error writing graph", e);
        }
        // TODO Support dumping the instantiated graph again
        return output;
    }

    private void writeGraph(Graph g, File file) throws IOException, CommandException {
        Closer close = Closer.create();
        try {
            FileWriter writer = close.register(new FileWriter(file));
            GraphWriter gw = close.register(new GraphWriter(writer));
            renderGraph(g, gw);
        } catch (Throwable th) {
            throw close.rethrow(th, CommandException.class);
        } finally {
            close.close();
        }
    }

    private void renderGraph(final Graph g, final GraphWriter gw) throws CommandException {
        // Handle the root node
        Node root = g.getNode(null);
        if (root == null) {
            throw new CommandException("no root node for graph");
        }
        GraphDumper dumper = new GraphDumper(g, gw);<commit_msg>Create parent directories for output file<commit_after>/*
 * LensKit, an open source recommender systems toolkit.
 * Copyright 2010-2013 Regents of the University of Minnesota and contributors
 * Work on LensKit has been funded by the National Science Foundation under
 * grants IIS 05-34939, 08-08692, 08-12148, and 10-17697.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.grouplens.lenskit.eval.graph;

import com.google.common.io.Closer;
import com.google.common.io.Files;
import org.grouplens.grapht.graph.Edge;
import org.grouplens.grapht.graph.Graph;
import org.grouplens.grapht.graph.Node;
import org.grouplens.grapht.spi.AbstractSatisfactionVisitor;
import org.grouplens.grapht.spi.CachedSatisfaction;
import org.grouplens.grapht.spi.Satisfaction;
import org.grouplens.lenskit.core.LenskitRecommenderEngineFactory;
import org.grouplens.lenskit.data.dao.DataAccessObject;
import org.grouplens.lenskit.data.pref.PreferenceDomain;
import org.grouplens.lenskit.eval.AbstractCommand;
import org.grouplens.lenskit.eval.CommandException;
import org.grouplens.lenskit.eval.algorithm.LenskitAlgorithmInstance;
import org.grouplens.lenskit.util.io.LKFileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class DumpGraphCommand extends AbstractCommand<File> {
    private static final Logger logger = LoggerFactory.getLogger(DumpGraphCommand.class);

    private LenskitAlgorithmInstance algorithm;
    private File output;
    private PreferenceDomain domain = null;
    private Class<? extends DataAccessObject> daoType;


    public DumpGraphCommand() {
        this("dumpGraph");
    }

    public DumpGraphCommand(String name) {
        super(name);
    }

    public DumpGraphCommand setAlgorithm(LenskitAlgorithmInstance algorithm) {
        this.algorithm = algorithm;
        return this;
    }

    public DumpGraphCommand setOutput(File f) {
        output = f;
        return this;
    }

    public DumpGraphCommand setDomain(PreferenceDomain dom) {
        domain = dom;
        return this;
    }

    public DumpGraphCommand setDaoType(Class<? extends DataAccessObject> daoType) {
        this.daoType = daoType;
        return this;
    }

    @Override
    public File call() throws CommandException {
        if (output == null) {
            logger.error("no output file specified");
            throw new IllegalStateException("no graph output file specified");
        }
        LenskitRecommenderEngineFactory factory = algorithm.getFactory().clone();
        if (domain != null) {
            factory.bind(PreferenceDomain.class).to(domain);
        }
        Graph initial = factory.getInitialGraph(daoType);
        try {
            writeGraph(factory.getInitialGraph(daoType), output);
        } catch (IOException e) {
            throw new CommandException("error writing graph", e);
        }
        // TODO Support dumping the instantiated graph again
        return output;
    }

    private void writeGraph(Graph g, File file) throws IOException, CommandException {
        Files.createParentDirs(output);
        Closer close = Closer.create();
        try {
            FileWriter writer = close.register(new FileWriter(file));
            GraphWriter gw = close.register(new GraphWriter(writer));
            renderGraph(g, gw);
        } catch (Throwable th) {
            throw close.rethrow(th, CommandException.class);
        } finally {
            close.close();
        }
    }

    private void renderGraph(final Graph g, final GraphWriter gw) throws CommandException {
        // Handle the root node
        Node root = g.getNode(null);
        if (root == null) {
            throw new CommandException("no root node for graph");
        }
        GraphDumper dumper = new GraphDumper(g, gw);
{'fraction_non_alphanumeric': 0.08194148638045062, 'fraction_numerical': 0.011433695774016366, 'mean_word_length': 3.1750116986429573, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1467455', 'n_tokens_mistral': 2542, 'n_tokens_neox': 2429, 'n_words': 749}