    public static File writeARFF(TrainingCorpus corpus, Lexicon lexicon,
            boolean b, String vector_location,
            Map<Integer, Integer> attributeMapping) throws IOException {
        File vectorFile = new File(vector_location);
        PrintWriter out = null;
        out = new PrintWriter(new FileWriter(vectorFile));

        out.print("%\n% Dataset generated by the TextClassifiation API\n%\n\n");

        // generate the arff headers
        out.print("@relation dataset\n\n");

        // label values
        out.print("@attribute label {");
        boolean isFirst = true;
        for (String l : lexicon.getLabels()) {
            if (!isFirst)
                out.print(",");
            out.print(l);
            isFirst = false;
        }
        out.print("}\n");

        // dump the content of the lexicon file
        int attributeNum = lexicon.getAttributesNum();
        Map<Integer, String> iindex = lexicon.getInvertedIndex();
        for (int c = 1; c <= attributeNum; c++) {
            String attrib = iindex.get(c);
            if (attrib == null)
                attrib = "NULL";
            else
                attrib = attrib.replaceAll("\\s+", " ");
            attrib = attrib.replaceAll("\"", "&quote;");
            if (attrib.endsWith("\\"))
                attrib = attrib.substring(0, attrib.length() - 1);
            attrib = "\"i_" + c + "_" + attrib + "\"";
            out.print("@attribute " + attrib + "  NUMERIC\n");
        }

        out.print("\n");

        out.print("@data\n");

        // get an iterator on the Corpus
        // and retrieve the documents one by one
        Iterator<Document> docIterator = corpus.iterator();
        while (docIterator.hasNext()) {
            Document doc = docIterator.next();
            int label = doc.getLabel();
            // get a vector from the document
            // need a metric (e.g. relative frequency / binary)
            // and a lexicon
            // the vector is represented as a string directly
            Vector vector = null;
            if (attributeMapping == null)
                vector = doc.getFeatureVector(lexicon);
            else
                vector = doc.getFeatureVector(lexicon, attributeMapping);

            StringBuffer buffer = new StringBuffer("{");

            buffer.append("0 ").append(lexicon.getLabel(label));

            // {1 X, 3 Y, 4 "class A"}
            // index space value
            int[] indices = vector.getIndices();
            double[] values = vector.getValues();
            for (int i = 0; i < indices.length; i++) {
                if (buffer.length() > 1)
                    buffer.append(", ");
                if (indices[i] > attributeNum)
                    continue;
                buffer.append(indices[i]).append(" ").append(values[i]);
            }
            buffer.append("}\n");
            out.print(buffer.toString());
        }
        out.close();
        return vectorFile;
    }