org.carrot2.examples.core.SavingResultsToJson.java Source code

Java tutorial

Introduction

Here is the source code for org.carrot2.examples.core.SavingResultsToJson.java

Source

/*
 * Carrot2 project.
 *
 * Copyright (C) 2002-2014, Dawid Weiss, Stanisaw Osiski.
 * All rights reserved.
 *
 * Refer to the full license file "carrot2.LICENSE"
 * in the root folder of the repository checkout or at:
 * http://www.carrot2.org/carrot2.LICENSE
 */

package org.carrot2.examples.core;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Map;

import org.carrot2.clustering.lingo.LingoClusteringAlgorithm;
import org.carrot2.core.Controller;
import org.carrot2.core.ControllerFactory;
import org.carrot2.core.Document;
import org.carrot2.core.ProcessingResult;
import org.carrot2.core.attribute.CommonAttributesDescriptor;
import org.carrot2.examples.SampleDocumentData;
import org.carrot2.examples.clustering.ClusteringDataFromDocumentSources;

import com.google.common.collect.Maps;

/**
 * This example shows how to save clustering results as JSON.
 * <p>
 * It is assumed that you are familiar with {@link ClusteringDataFromDocumentSources}
 * example.
 * </p>
 */
public class SavingResultsToJson {
    public static void main(String[] args) throws Exception {
        // Let's fetch some results from MSN first
        final Controller controller = ControllerFactory.createSimple();
        final Map<String, Object> attributes = Maps.newHashMap();
        CommonAttributesDescriptor.attributeBuilder(attributes)
                .documents(new ArrayList<Document>(SampleDocumentData.DOCUMENTS_DATA_MINING)).query("data mining");

        final ProcessingResult result = controller.process(attributes, LingoClusteringAlgorithm.class);

        // Now, we can serialize the entire result to XML like this
        result.serializeJson(new PrintWriter(System.out));
        System.out.println();

        // Optionally, we can provide a callback for JSON-P-style calls
        result.serializeJson(new PrintWriter(System.out), "loadResults", true /* indent */,
                false /* save documents */, true /* save clusters */);

    }
}