Example usage for weka.core Instances get

List of usage examples for weka.core Instances get

Introduction

In this page you can find the example usage for weka.core Instances get.

Prototype



@Override
publicInstance get(int index) 

Source Link

Document

Returns the instance at the given position.

Usage

From source file:edu.utexas.cs.tactex.utils.RegressionUtils.java

License:Open Source License

/**
 * adding y attributes with values//from  www.j  a  v  a  2s .  c  o  m
 */
public static Instances addYforWeka(Instances xInsts, Double[] y) {

    Instances xyInsts = addYforWeka(xInsts);

    if (y.length != xInsts.numInstances()) {
        log.error("cannot add y to instances since y.length != numInstances");
    }

    // initialize all y values
    int n = xInsts.numAttributes() - 1;
    for (int i = 0; i < y.length; ++i) {
        xInsts.get(i).setValue(n, y[i]);
    }

    return xyInsts;
}

From source file:elh.eus.absa.CLI.java

License:Open Source License

/**
 * Main access to the polarity tagging functionalities. Target based polarity. 
 *
 * @throws IOException/*w  w w . j ava 2 s.co m*/
 * input output exception if problems with corpora
 */
public final void evalATP(final InputStream inputStream) throws IOException, JDOMException {

    String paramFile = parsedArguments.getString("params");
    String corpusFormat = parsedArguments.getString("corpusFormat");
    String model = parsedArguments.getString("model");
    String lang = parsedArguments.getString("language");
    String classnum = parsedArguments.getString("classnum");
    boolean ruleBased = parsedArguments.getBoolean("ruleBasedClassifier");
    boolean printPreds = parsedArguments.getBoolean("outputPredictions");

    //Read corpus sentences
    CorpusReader reader = new CorpusReader(inputStream, corpusFormat, lang);

    //Rule-based Classifier.
    if (ruleBased) {
        Properties params = new Properties();
        params.load(new FileInputStream(new File(paramFile)));

        String posModelPath = params.getProperty("pos-model");
        String lemmaModelPath = params.getProperty("lemma-model");
        String kafDir = params.getProperty("kafDir");

        /* polarity lexicon. Domain specific polarity lexicon is given priority.
         * If no domain lexicon is found it reverts to general polarity lexicon.
         * If no general polarity lexicon is found program exits with error message.
        */
        String lex = params.getProperty("polarLexiconDomain", "none");
        if (lex.equalsIgnoreCase("none")) {
            lex = params.getProperty("polarLexiconGeneral", "none");
            if (lex.equalsIgnoreCase("none")) {
                System.err.println("Elixa Error :: Rule-based classifier is selected but no polarity"
                        + " lexicon has been specified. Either specify one or choose ML classifier");
                System.exit(1);
            }
        }
        File lexFile = new File(lex);
        Evaluator evalDoc = new Evaluator(lexFile, "lemma");

        for (String oId : reader.getOpinions().keySet()) {
            // sentence posTagging
            String taggedKaf = reader.tagSentenceTab(reader.getOpinion(oId).getsId(), kafDir, posModelPath,
                    lemmaModelPath);
            //process the postagged sentence with the word count based polarity tagger
            Map<String, String> results = evalDoc.polarityScoreTab(taggedKaf, lexFile.getName());
            String lblStr = results.get("polarity");
            String actual = "?";
            if (reader.getOpinion(oId).getPolarity() != null) {
                actual = reader.getOpinion(oId).getPolarity();
            }
            String rId = reader.getOpinion(oId).getsId().replaceFirst("_g$", "");
            System.out.println(rId + "\t" + actual + "\t" + lblStr + "\t" + reader.getOpinionSentence(oId));
            reader.getOpinion(oId).setPolarity(lblStr);
        }
    }
    //ML Classifier (default)
    else {
        Features atpTest = new Features(reader, paramFile, classnum, model);
        Instances testdata;
        if (corpusFormat.startsWith("tab") && !corpusFormat.equalsIgnoreCase("tabNotagged")) {
            testdata = atpTest.loadInstancesTAB(true, "atp");
        } else {
            testdata = atpTest.loadInstances(true, "atp");
        }
        //   setting class attribute (entCat|attCat|entAttCat|polarityCat)
        testdata.setClass(testdata.attribute("polarityCat"));

        WekaWrapper classify;
        try {
            classify = new WekaWrapper(model);

            System.err.println("evalAtp : going to test the model");
            //sort according to the instanceId
            //traindata.sort(atpTrain.getAttIndexes().get("instanceId"));
            //Instances testdata = new Instances(traindata);
            //testdata.deleteAttributeAt(0);
            //classify.setTestdata(testdata);
            classify.setTestdata(testdata);
            classify.testModel(model);

            if (printPreds) {
                for (String oId : reader.getOpinions().keySet()) {
                    int iId = atpTest.getOpinInst().get(oId);
                    Instance i = testdata.get(iId - 1);
                    double label = classify.getMLclass().classifyInstance(i);
                    String lblStr = i.classAttribute().value((int) label);
                    String actual = "?";
                    if (reader.getOpinion(oId).getPolarity() != null) {
                        actual = reader.getOpinion(oId).getPolarity();
                    }
                    String rId = reader.getOpinion(oId).getsId().replaceFirst("_g$", "");
                    String oSent = reader.getOpinionSentence(oId);
                    if (corpusFormat.startsWith("tab")) {
                        StringBuilder sb = new StringBuilder();
                        for (String kk : oSent.split("\n")) {
                            sb.append(kk.split("\\t")[0]);
                            sb.append(" ");
                        }
                        oSent = sb.toString();
                    }

                    System.out.println(rId + "\t" + actual + "\t" + lblStr + "\t" + oSent + "\t"
                            + reader.getOpinionSentence(oId).replaceAll("\n", " ").replaceAll("\\t", ":::"));
                    reader.getOpinion(oId).setPolarity(lblStr);
                }
            }
            //reader.print2Semeval2015format(model+"tagATP.xml");
            //reader.print2conll(model+"tagAtp.conll");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:elh.eus.absa.CLI.java

License:Open Source License

/**
 * Main access to the polarity tagging functionalities. Target based polarity. 
 *
 * @throws IOException/*from w  w  w .  j  a va  2  s. c om*/
 * input output exception if problems with corpora
 * @throws JDOMException 
 */
public final void tagATP(final InputStream inputStream) throws IOException, JDOMException {
    // load training parameters file
    String paramFile = parsedArguments.getString("params");
    String corpusFormat = parsedArguments.getString("corpusFormat");
    String model = parsedArguments.getString("model");
    String lang = parsedArguments.getString("language");
    String classnum = parsedArguments.getString("classnum");
    boolean ruleBased = parsedArguments.getBoolean("ruleBasedClassifier");

    //Read corpus sentences
    CorpusReader reader = new CorpusReader(inputStream, corpusFormat, lang);

    //Rule-based Classifier.
    if (ruleBased) {
        Properties params = new Properties();
        params.load(new FileInputStream(new File(paramFile)));

        String posModelPath = params.getProperty("pos-model");
        String lemmaModelPath = params.getProperty("lemma-model");
        String kafDir = params.getProperty("kafDir");

        /* polarity lexicon. Domain specific polarity lexicon is given priority.
         * If no domain lexicon is found it reverts to general polarity lexicon.
         * If no general polarity lexicon is found program exits with error message.
        */
        String lex = params.getProperty("polarLexiconDomain", "none");
        if (lex.equalsIgnoreCase("none")) {
            lex = params.getProperty("polarLexiconGeneral", "none");
            if (lex.equalsIgnoreCase("none")) {
                System.err.println("Elixa Error :: Rule-based classifier is selected but no polarity"
                        + " lexicon has been specified. Either specify one or choose ML classifier");
                System.exit(1);
            }
        }
        File lexFile = new File(lex);
        Evaluator evalDoc = new Evaluator(lexFile, "lemma");

        for (String oId : reader.getOpinions().keySet()) {
            // sentence posTagging
            String taggedKaf = reader.tagSentenceTab(reader.getOpinion(oId).getsId(), kafDir, posModelPath,
                    lemmaModelPath);
            //process the postagged sentence with the word count based polarity tagger
            Map<String, String> results = evalDoc.polarityScoreTab(taggedKaf, lexFile.getName());
            String lblStr = results.get("polarity");
            String actual = "?";
            if (reader.getOpinion(oId).getPolarity() != null) {
                actual = reader.getOpinion(oId).getPolarity();
            }
            String rId = reader.getOpinion(oId).getsId().replaceFirst("_g$", "");
            System.out.println(rId + "\t" + actual + "\t" + lblStr + "\t" + reader.getOpinionSentence(oId));
            reader.getOpinion(oId).setPolarity(lblStr);
        }
    } else {
        Features atpTrain = new Features(reader, paramFile, classnum, model);
        Instances traindata;
        if (corpusFormat.startsWith("tab") && !corpusFormat.equalsIgnoreCase("tabNotagged")) {
            traindata = atpTrain.loadInstancesTAB(true, "atp");
        } else if (lang.equalsIgnoreCase("eu")
                && (corpusFormat.equalsIgnoreCase("tabNotagged") || corpusFormat.equalsIgnoreCase("ireom"))) {
            traindata = atpTrain.loadInstancesConll(true, "atp");
        } else {
            traindata = atpTrain.loadInstances(true, "atp");
        }

        //   setting class attribute (entCat|attCat|entAttCat|polarityCat)
        traindata.setClass(traindata.attribute("polarityCat"));

        WekaWrapper classify;
        try {
            classify = new WekaWrapper(model);

            System.err.println();
            //sort according to the instanceId
            //traindata.sort(atpTrain.getAttIndexes().get("instanceId"));
            //Instances testdata = new Instances(traindata);
            //testdata.deleteAttributeAt(0);
            //classify.setTestdata(testdata);
            classify.setTestdata(traindata);
            classify.loadModel(model);

            for (String oId : reader.getOpinions().keySet()) {
                int iId = atpTrain.getOpinInst().get(oId);
                Instance i = traindata.get(iId - 1);
                double label = classify.getMLclass().classifyInstance(i);
                String lblStr = i.classAttribute().value((int) label);
                String actual = "?";
                if (reader.getOpinion(oId).getPolarity() != null) {
                    actual = reader.getOpinion(oId).getPolarity();
                }
                String rId = reader.getOpinion(oId).getsId().replaceFirst("_g$", "");
                String oSent = reader.getOpinionSentence(oId);
                if (corpusFormat.startsWith("tab")) {
                    StringBuilder sb = new StringBuilder();
                    for (String kk : oSent.split("\n")) {
                        sb.append(kk.split("\\t")[0]);
                        sb.append(" ");
                    }
                    oSent = sb.toString();
                }

                System.out.println(rId + "\t" + actual + "\t" + lblStr + "\t" + oSent + "\t"
                        + reader.getOpinionSentence(oId).replaceAll("\n", " ").replaceAll("\\t", ":::"));
                reader.getOpinion(oId).setPolarity(lblStr);
            }

            //reader.print2Semeval2015format(model+"tagATP.xml");
            //reader.print2conll(model+"tagAtp.conll");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:ergasia2pkg.LP_ROS.java

/**
 * Groups instances by their labels//from w w w . j av  a 2 s.com
 *
 * @param MultilabelInstances labeled instances
 * @return HashMap<String,List<Instance>> returned Hashmap with grouping
 */
public HashMap groupByLabelSet(MultiLabelInstances mlData) {

    Instances inst = mlData.getDataSet();

    Set<Attribute> atts = mlData.getLabelAttributes();
    HashMap LabelSetGroups = new HashMap<String, List<Instance>>();

    for (int i = 0; i < inst.numInstances(); i++) {
        Instance in = inst.get(i);
        String labelsetName = "";
        for (Attribute att : atts) {
            if (in.value(att) != 0) {
                labelsetName = labelsetName + att.name();
            }

        }
        if (LabelSetGroups.containsKey(labelsetName)) {
            List myList = (List) LabelSetGroups.get(labelsetName);
            myList.add(in);
            LabelSetGroups.put(labelsetName, myList);
        } else {
            List<Instance> myList = new ArrayList<Instance>();
            myList.add(in);
            LabelSetGroups.put(labelsetName, myList);
        }

    }

    return LabelSetGroups;
}

From source file:etc.aloe.filters.WordFeaturesExtractor.java

License:Open Source License

protected List<List<String>> tokenizeDocuments(Instances instances) {
    //Convert all instances into term lists
    List<List<String>> documents = new ArrayList<List<String>>();

    for (int i = 0; i < instances.size(); i++) {
        Instance instance = instances.get(i);

        if (instance.isMissing(selectedAttributeIndex) == false) {
            List<String> words = tokenizeDocument(instance);
            documents.add(words);// w ww  .ja v  a 2s .  c  o  m
        }
    }

    return documents;
}

From source file:eu.cassandra.appliance.IsolatedApplianceExtractor.java

License:Apache License

/**
 * This function is taking the instances coming out from clustering and put
 * each event to each respective cluster.
 * /*from www  .  j  av a2s  .  c o m*/
 * @param inst
 *          The clustered instances
 */
private void fillClusters(Instances inst) {
    // Initializing auxiliary variables
    ArrayList<Integer> temp;

    // For each instance check the cluster value and put it to the correct
    // cluster
    for (int i = 0; i < inst.size(); i++) {

        String cluster = inst.get(i).stringValue(inst.attribute(5));

        if (!clusters.containsKey(cluster))
            temp = new ArrayList<Integer>();
        else
            temp = clusters.get(cluster);

        temp.add(i);

        clusters.put(cluster, temp);

    }

}

From source file:eu.cassandra.appliance.IsolatedEventsExtractor.java

License:Apache License

/**
 * This function is taking the instances coming out from clustering and put
 * each event to each respective cluster.
 * /*from w  w  w .jav  a 2s .  com*/
 * @param inst
 *          The clustered instances
 */
private void fillClusters(Instances inst) {
    // Initializing auxiliary variables
    ArrayList<Integer> temp;

    // For each instance check the cluster value and put it to the correct
    // cluster
    for (int i = 0; i < inst.size(); i++) {

        String cluster = inst.get(i).stringValue(inst.attribute(6));

        if (!clusters.containsKey(cluster))
            temp = new ArrayList<Integer>();
        else
            temp = clusters.get(cluster);

        temp.add(i);

        clusters.put(cluster, temp);

    }

}

From source file:eu.cassandra.utils.Utils.java

License:Apache License

/**
 * This function is used in order to create clusters of points of interest
 * based on the active power difference they have.
 * //from   w  w w  . jav  a2  s.  co m
 * @param pois
 *          The list of points of interest that will be clustered.
 * @return The newly created clusters with the points that are comprising
 *         them.
 * @throws Exception
 */
public static ArrayList<ArrayList<PointOfInterest>> clusterPoints(ArrayList<PointOfInterest> pois, int bias)
        throws Exception {
    // Initialize the auxiliary variables
    ArrayList<ArrayList<PointOfInterest>> result = new ArrayList<ArrayList<PointOfInterest>>();

    // Estimating the number of clusters that will be created
    int numberOfClusters = (int) (Math.ceil((double) pois.size() / (double) Constants.MAX_POINTS_OF_INTEREST))
            + bias;

    log.info("Clusters: " + pois.size() + " / " + Constants.MAX_POINTS_OF_INTEREST + " + " + bias + " = "
            + numberOfClusters);

    // Create a new empty list of points for each cluster
    for (int i = 0; i < numberOfClusters; i++)
        result.add(new ArrayList<PointOfInterest>());

    // Initializing auxiliary variables namely the attributes of the data set
    Attribute id = new Attribute("id");
    Attribute pDiffRise = new Attribute("pDiff");

    ArrayList<Attribute> attr = new ArrayList<Attribute>();
    attr.add(id);
    attr.add(pDiffRise);

    Instances instances = new Instances("Points of Interest", attr, 0);

    // Each event is translated to an instance with the above attributes
    for (int i = 0; i < pois.size(); i++) {

        Instance inst = new DenseInstance(2);
        inst.setValue(id, i);
        inst.setValue(pDiffRise, Math.abs(pois.get(i).getPDiff()));

        instances.add(inst);

    }

    // System.out.println(instances.toString());

    Instances newInst = null;

    log.debug("Instances: " + instances.toSummaryString());

    // Create the addcluster filter of Weka and the set up the hierarchical
    // clusterer.
    AddCluster addcluster = new AddCluster();

    SimpleKMeans kmeans = new SimpleKMeans();

    kmeans.setSeed(numberOfClusters);

    // This is the important parameter to set
    kmeans.setPreserveInstancesOrder(true);
    kmeans.setNumClusters(numberOfClusters);
    kmeans.buildClusterer(instances);

    addcluster.setClusterer(kmeans);
    addcluster.setInputFormat(instances);
    addcluster.setIgnoredAttributeIndices("1");

    // Cluster data set
    newInst = Filter.useFilter(instances, addcluster);

    // System.out.println(newInst.toString());

    // Parse through the dataset to see where each point is placed in the
    // clusters.
    for (int i = 0; i < newInst.size(); i++) {

        String cluster = newInst.get(i).stringValue(newInst.attribute(2));

        cluster = cluster.replace("cluster", "");

        log.debug("Point of Interest: " + i + " Cluster: " + cluster);

        result.get(Integer.parseInt(cluster) - 1).add(pois.get(i));
    }

    // Sorting the each cluster points by their minutes.
    for (int i = result.size() - 1; i >= 0; i--) {
        if (result.get(i).size() == 0)
            result.remove(i);
        else
            Collections.sort(result.get(i), Constants.comp);
    }

    // Sorting the all clusters by their active power.

    Collections.sort(result, Constants.comp5);

    return result;
}

From source file:eu.linda.analytics.formats.ForecastingRDFGenerator.java

@Override
public Model generateRDFModel(Analytics analytics, AbstractList dataToExport) {

    helpfulFunctions.nicePrintMessage("Generate Forecasting RDFModel for weka algorithms ");

    Date date = new Date();
    DateFormat formatter = new SimpleDateFormat("ddMMyyyy");
    String today = formatter.format(date);
    String base = Configuration.lindaworkbenchURI
            + "openrdf-sesame/repositories/myRepository/statements?context=:_";
    String datasetContextToString = "analytics" + analytics.getId() + "V" + (analytics.getVersion() + 1)
            + "Date" + today;

    Instances triplets = (Instances) dataToExport;
    int tripletsAttibutesNum = triplets.numAttributes();

    // Create the model and define some prefixes (for nice serialization in RDF/XML and TTL)
    Model model = ModelFactory.createDefaultModel();
    //openrdf + analytic_process ID_version_date
    String NS = base + datasetContextToString + "#";

    String analytics_base = Configuration.lindaworkbenchURI
            + "openrdf-sesame/repositories/linda/rdf-graphs/analyticsontology";
    String analytics_NS = analytics_base + "#";

    model.setNsPrefix("ds", NS);
    model.setNsPrefix("rdf", RDF.getURI());
    model.setNsPrefix("xsd", XSD.getURI());
    model.setNsPrefix("foaf", FOAF.getURI());
    model.setNsPrefix("rdfs", RDFS.getURI());
    model.setNsPrefix("prov", "http://www.w3.org/ns/prov#");
    model.setNsPrefix("sio", "http://semanticscience.org/ontology/sio#");
    model.setNsPrefix("an", Configuration.lindaworkbenchURI
            + "openrdf-sesame/repositories/linda/rdf-graphs/analyticsontology#");

    // Define local properties
    Property analyzedField = model.createProperty(NS + "#analyzedField");
    Property predictedValue = model.createProperty(NS + "#predictedValue");
    Property wasDerivedFrom = model.createProperty("http://www.w3.org/ns/prov#wasDerivedFrom");
    Property wasGeneratedBy = model.createProperty("http://www.w3.org/ns/prov#wasGeneratedBy");
    Property actedOnBehalfOf = model.createProperty("http://www.w3.org/ns/prov#actedOnBehalfOf");
    Property wasAssociatedWith = model.createProperty("http://www.w3.org/ns/prov#wasAssociatedWith");
    Property hasTrainDataset = model.createProperty(NS + "hasTrainDataset");
    Property hasEvaluationDataset = model.createProperty(NS + "hasEvaluationDataset");
    Property algorithmProperty = model.createProperty(NS + "algorithm");

    Resource entity = model.createResource("http://www.w3.org/ns/prov#Entity");
    Resource activity = model.createResource("http://www.w3.org/ns/prov#Activity");
    Resource agent = model.createResource("http://www.w3.org/ns/prov#Agent");
    Resource onlineAccount = model.createResource(FOAF.OnlineAccount);

    Resource software_statement = model.createResource(analytics_NS + "Software/LinDa_analytics_software");
    Resource software = model.createResource(analytics_NS + "Software");
    Resource linda_user = model.createResource(analytics_NS + "User");

    Resource analytic_process = model.createResource(analytics_NS + "analytic_process");
    Resource analytic_process_statement = model.createResource(
            analytics_NS + "analytic_process/" + analytics.getId() + "/" + (analytics.getVersion() + 1));
    analytic_process_statement.addProperty(RDF.type, analytic_process);
    analytic_process_statement.addProperty(OWL.versionInfo, "1.0.0");
    analytic_process_statement.addLiteral(analyzedField, triplets.attribute(tripletsAttibutesNum - 1).name());
    analytic_process_statement.addProperty(RDFS.subClassOf, activity);
    analytic_process_statement.addProperty(wasAssociatedWith, software_statement);
    analytic_process_statement.addProperty(RDFS.label, "linda analytic process");
    analytic_process_statement.addProperty(RDFS.comment, analytics.getDescription());
    analytic_process_statement.addProperty(algorithmProperty, analytics.getAlgorithm_name());

    if (helpfulFunctions.isRDFInputFormat(analytics.getTrainQuery_id())) {

        Resource analytic_train_dataset_statement = model.createResource(
                Configuration.lindaworkbenchURI + "sparql/?q_id=" + analytics.getTrainQuery_id());
        analytic_process_statement.addProperty(hasTrainDataset, analytic_train_dataset_statement);

    }/*from   w ww. j a  v a 2  s .co  m*/

    if (helpfulFunctions.isRDFInputFormat(analytics.getEvaluationQuery_id())) {

        Resource analytic_evaluation_dataset_statement = model.createResource(
                Configuration.lindaworkbenchURI + "sparql/?q_id=" + analytics.getEvaluationQuery_id());
        analytic_process_statement.addProperty(hasEvaluationDataset, analytic_evaluation_dataset_statement);

    }

    Resource linda_user_statement = model.createResource(analytics_NS + "User/" + analytics.getUser_name());
    linda_user_statement.addProperty(RDF.type, linda_user);
    linda_user_statement.addProperty(RDFS.subClassOf, agent);
    linda_user_statement.addProperty(RDFS.label, "linda user");

    software_statement.addProperty(RDF.type, software);
    software_statement.addProperty(RDFS.subClassOf, agent);
    software_statement.addProperty(RDFS.label, "analytics software");
    software_statement.addProperty(actedOnBehalfOf, linda_user_statement);

    linda_user_statement.addProperty(OWL.equivalentClass, FOAF.Person);

    linda_user_statement.addProperty(FOAF.holdsAccount, onlineAccount);

    linda_user_statement.addProperty(FOAF.accountName, analytics.getUser_name());
    onlineAccount.addProperty(FOAF.homepage, Configuration.lindaworkbenchURI);

    Resource analytic_result_node = model.createResource(analytics_NS + "analytics_result_node");
    Resource analytic_input_node = model.createResource(analytics_NS + "analytic_input_node");

    // For each triplet, create a resource representing the sentence, as well as the subject, 
    // predicate, and object, and then add the triples to the model.
    for (int i = 1; i < triplets.size(); i++) {

        Resource analytic_result_node_statement = model.createResource(NS + "/" + i);

        Resource analytic_input_node_statement = model.createResource(triplets.get(i).toString(1));
        analytic_input_node_statement.addProperty(RDF.type, analytic_input_node);

        analytic_result_node_statement.addProperty(RDF.type, analytic_result_node);
        analytic_result_node_statement.addProperty(RDFS.subClassOf, entity);
        analytic_result_node_statement.addProperty(wasDerivedFrom, analytic_input_node_statement);
        analytic_result_node_statement.addProperty(wasGeneratedBy, analytic_process_statement);
        analytic_result_node_statement.addProperty(predictedValue,
                triplets.get(i).toString(tripletsAttibutesNum - 1));
    }
    return model;

}

From source file:eu.linda.analytics.formats.GeneralRDFGenerator.java

@Override
public Model generateRDFModel(Analytics analytics, AbstractList dataToExport) {

    helpfulFuncions.nicePrintMessage("Generate General RDFModel for weka algorithms ");

    Date date = new Date();
    DateFormat formatter = new SimpleDateFormat("ddMMyyyy");
    String today = formatter.format(date);
    String base = Configuration.lindaworkbenchURI + "openrdf-sesame/repositories/linda/statements?context=:_";
    String datasetContextToString = "analytics" + analytics.getId() + "V" + (analytics.getVersion() + 1)
            + "Date" + today;

    Instances triplets = (Instances) dataToExport;
    int tripletsAttibutesNum = triplets.numAttributes();

    // Create the model and define some prefixes (for nice serialization in RDF/XML and TTL)
    Model model = ModelFactory.createDefaultModel();
    //openrdf + analytic_process ID_version_date
    String NS = base + datasetContextToString + "#";

    String analytics_base = Configuration.lindaworkbenchURI
            + "openrdf-sesame/repositories/linda/rdf-graphs/analyticsontology";
    String analytics_NS = analytics_base + "#";

    model.setNsPrefix("ds", NS);
    model.setNsPrefix("rdf", RDF.getURI());
    model.setNsPrefix("xsd", XSD.getURI());
    model.setNsPrefix("foaf", FOAF.getURI());
    model.setNsPrefix("rdfs", RDFS.getURI());
    model.setNsPrefix("prov", "http://www.w3.org/ns/prov#");
    model.setNsPrefix("sio", "http://semanticscience.org/ontology/sio#");
    model.setNsPrefix("an", Configuration.lindaworkbenchURI
            + "openrdf-sesame/repositories/linda/rdf-graphs/analyticsontology#");

    // Define local properties
    Property analyzedField = model.createProperty(NS + "analyzedField");
    Property predictedValue = model.createProperty(NS + "predictedValue");
    Property wasDerivedFrom = model.createProperty("http://www.w3.org/ns/prov#wasDerivedFrom");
    Property wasGeneratedBy = model.createProperty("http://www.w3.org/ns/prov#wasGeneratedBy");
    Property actedOnBehalfOf = model.createProperty("http://www.w3.org/ns/prov#actedOnBehalfOf");
    Property wasAssociatedWith = model.createProperty("http://www.w3.org/ns/prov#wasAssociatedWith");
    Property hasTrainDataset = model.createProperty(NS + "hasTrainDataset");
    Property hasEvaluationDataset = model.createProperty(NS + "hasEvaluationDataset");
    Property algorithmProperty = model.createProperty(NS + "algorithm");
    Property dataSizeOfAnalyzedDataProperty = model.createProperty(NS + "dataSizeOfAnalyzedDatainBytes");
    Property timeToGetDataProperty = model.createProperty(NS + "timeToGetDataInSecs");
    Property timeToRunAnalyticsProcessProperty = model.createProperty(NS + "timeToRunAnalyticsProcessInSecs");
    Property timeToCreateRDFOutPutProperty = model.createProperty(NS + "timeToCreateRDFOutPutInSecs");
    Property performanceProperty = model.createProperty(NS + "hasPerformance");
    Property atTime = model.createProperty("http://www.w3.org/ns/prov#atTime");

    Resource entity = model.createResource("http://www.w3.org/ns/prov#Entity");
    Resource activity = model.createResource("http://www.w3.org/ns/prov#Activity");
    Resource agent = model.createResource("http://www.w3.org/ns/prov#Agent");
    Resource onlineAccount = model.createResource(FOAF.OnlineAccount);
    Resource linda_user = model.createResource(analytics_NS + "User");
    Resource software_statement = model.createResource(analytics_NS + "Software/LinDa_analytics_software");
    Resource software = model.createResource(analytics_NS + "Software");
    Resource performance = model.createResource(analytics_NS + "performance");
    Resource performance_statement = model
            .createResource(analytics_NS + "performance/" + analytics.getId() + "/" + analytics.getVersion());

    Resource analytic_process = model.createResource(analytics_NS + "analytic_process");
    Resource analytic_process_statement = model.createResource(
            analytics_NS + "analytic_process/" + analytics.getId() + "/" + analytics.getVersion());
    analytic_process_statement.addProperty(RDF.type, analytic_process);
    analytic_process_statement.addProperty(OWL.versionInfo, "1.0.0");
    analytic_process_statement.addLiteral(analyzedField, triplets.attribute(tripletsAttibutesNum - 1).name());
    analytic_process_statement.addProperty(RDFS.subClassOf, activity);
    analytic_process_statement.addProperty(wasAssociatedWith, software_statement);
    analytic_process_statement.addProperty(RDFS.label, "Linda Analytic process");
    analytic_process_statement.addProperty(RDFS.comment, analytics.getDescription());
    analytic_process_statement.addProperty(algorithmProperty, analytics.getAlgorithm_name());

    Calendar cal = GregorianCalendar.getInstance();
    Literal value = model.createTypedLiteral(cal);
    analytic_process_statement.addProperty(atTime, value);

    performance_statement.addProperty(RDF.type, performance);
    performance_statement.addProperty(dataSizeOfAnalyzedDataProperty, Float.toString(analytics.getData_size()));
    performance_statement.addProperty(timeToGetDataProperty, Float.toString(analytics.getTimeToGet_data()));
    performance_statement.addProperty(timeToRunAnalyticsProcessProperty,
            Float.toString(analytics.getTimeToRun_analytics()));
    performance_statement.addProperty(timeToCreateRDFOutPutProperty,
            Float.toString(analytics.getTimeToCreate_RDF()));
    analytic_process_statement.addProperty(performanceProperty, performance_statement);

    if (helpfulFuncions.isRDFInputFormat(analytics.getTrainQuery_id())) {

        Resource analytic_train_dataset_statement = model.createResource(
                Configuration.lindaworkbenchURI + "sparql/?q_id=" + analytics.getTrainQuery_id());
        analytic_process_statement.addProperty(hasTrainDataset, analytic_train_dataset_statement);

    }//from w  w  w  .  ja  va  2 s  .  c  om

    if (helpfulFuncions.isRDFInputFormat(analytics.getEvaluationQuery_id())) {

        Resource analytic_evaluation_dataset_statement = model.createResource(
                Configuration.lindaworkbenchURI + "sparql/?q_id=" + analytics.getEvaluationQuery_id());
        analytic_process_statement.addProperty(hasEvaluationDataset, analytic_evaluation_dataset_statement);

    }

    Resource linda_user_statement = model.createResource(analytics_NS + "User/" + analytics.getUser_name());
    linda_user_statement.addProperty(RDF.type, linda_user);
    linda_user_statement.addProperty(RDFS.subClassOf, agent);
    linda_user_statement.addProperty(RDFS.label, "linda user");

    software_statement.addProperty(RDF.type, software);
    software_statement.addProperty(RDFS.subClassOf, agent);
    software_statement.addProperty(RDFS.label, "analytics software");
    software_statement.addProperty(actedOnBehalfOf, linda_user_statement);

    linda_user_statement.addProperty(OWL.equivalentClass, FOAF.Person);

    linda_user_statement.addProperty(FOAF.holdsAccount, onlineAccount);

    linda_user_statement.addProperty(FOAF.accountName, analytics.getUser_name());
    onlineAccount.addProperty(FOAF.homepage, Configuration.lindaworkbenchURI);

    Resource analytic_result_node = model.createResource(analytics_NS + "analytics_result_node");
    Resource analytic_input_node = model.createResource(analytics_NS + "analytic_input_node");

    // For each triplet, create a resource representing the sentence, as well as the subject, 
    // predicate, and object, and then add the triples to the model.
    for (int i = 1; i < triplets.size(); i++) {
        //for (Instance triplet : triplets) {
        Resource analytic_input_node_statement = model.createResource(triplets.get(i).toString(0));
        analytic_input_node_statement.addProperty(RDF.type, analytic_input_node);

        Resource analytic_result_node_statement = model.createResource(NS + "/" + i);
        analytic_result_node_statement.addProperty(RDF.type, analytic_result_node);
        analytic_result_node_statement.addProperty(RDFS.subClassOf, entity);
        analytic_result_node_statement.addProperty(wasDerivedFrom, analytic_input_node_statement);
        analytic_result_node_statement.addProperty(wasGeneratedBy, analytic_process_statement);
        analytic_result_node_statement.addProperty(predictedValue,
                triplets.get(i).toString(tripletsAttibutesNum - 1));

    }

    return model;

}