Java: Membaca File CSV menggunakan OpenCSV

Berikut cara untuk membaca file CSV dengan menggunakan library OpenCSV.

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.List;
import com.opencsv.CSVReader;

public class ReaderCSV3  {
  public static void main(String[] args) throws Exception {
    String file = "src/data.csv";
    String[][] csv = readCSV(file);
    double[][] data = getDouble(csv);

    for (int i = 0; i < data.length; i++) {
      for (int j = 0; j < data[0].length; j++) {
        System.out.print(data[i][j] + "\t");
      }
      System.out.println("");
    }
  }

  public static String[][] readCSV(String fileName) throws Exception {
    try (CSVReader reader = new CSVReader(new BufferedReader(
      new FileReader(fileName)));) {

      List<String[]> lines = reader.readAll();
      String[][] data = new String[lines.size()][];
      data = lines.toArray(new String[lines.size()][]);

      return data;
    }
  }

  public static double[][] getDouble(String[][] data) {
    double[][] dataDouble = new double[data.length][data[0].length];

    for (int i = 0; i < data.length; i++) {
      for (int j = 0; j < data[0].length; j++) {
        dataDouble[i][j] = Double.parseDouble(data[i][j]);
      }
    }

    return dataDouble;
  }
}
Advertisements

Java: Menambahkan Font non-Latin menggunakan iText

Berikut cara untuk menambahkan font non-Latin menggunakan iText.

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorker;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.itextpdf.tool.xml.css.CssFile;
import com.itextpdf.tool.xml.css.StyleAttrCSSResolver;
import com.itextpdf.tool.xml.html.CssAppliers;
import com.itextpdf.tool.xml.html.CssAppliersImpl;
import com.itextpdf.tool.xml.html.Tags;
import com.itextpdf.tool.xml.parser.XMLParser;
import com.itextpdf.tool.xml.pipeline.css.CSSResolver;
import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline;
import com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;

public class D06_ParseHtmlCss {

    public static final String HTML = "src/NotaDinas.html";
    public static final String CSS = "src/notadinas.css";
    public static final String DEST = "src/xmlworker/hasiljp.pdf";

    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();

        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        writer.setInitialLeading(12.5f);

        // step 3
        document.open();

        // step 3.5
        XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
        fontProvider.register("src/NotoSansCJKjp-Light.otf", "Noto Sans CJK JP");
        FontFactory.setFontImp(fontProvider);
        CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);

        // step 4

        // CSS
        CSSResolver cssResolver = new StyleAttrCSSResolver();
        CssFile cssFile = XMLWorkerHelper.getCSS(new FileInputStream(CSS));
        cssResolver.addCss(cssFile);

        // HTML
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
        htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

        // Pipelines
        PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
        HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
        CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

        // XML Worker
        XMLWorker worker = new XMLWorker(css, true);
        XMLParser p = new XMLParser(worker);
        p.parse(new FileInputStream(HTML), Charset.forName("UTF-8"));

        // step 5
        document.close();
    }

    /**
     * Main method
     */
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new D06_ParseHtmlCss().createPdf(DEST);
    }
}

Java: Menambahkan Font Baru pada iText

Berikut merupakan teknik untuk menambahkan font pada iText jika font tersebut tidak ada di dalam sistem operasi.

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorker;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.itextpdf.tool.xml.css.CssFile;
import com.itextpdf.tool.xml.css.StyleAttrCSSResolver;
import com.itextpdf.tool.xml.html.CssAppliers;
import com.itextpdf.tool.xml.html.CssAppliersImpl;
import com.itextpdf.tool.xml.html.Tags;
import com.itextpdf.tool.xml.parser.XMLParser;
import com.itextpdf.tool.xml.pipeline.css.CSSResolver;
import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline;
import com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class D05_ParseHtmlCss {

    public static final String HTML = "src/walden.html";
    public static final String CSS = "src/walden.css";
    public static final String DEST = "src/xmlworker/hasil.pdf";

    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();

        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        writer.setInitialLeading(12.5f);

        // step 3
        document.open();

        // step 3.5
        XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
        fontProvider.register("src/Roboto-Regular.ttf");
        FontFactory.setFontImp(fontProvider);
        CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);

        // step 4

        // CSS
        CSSResolver cssResolver = new StyleAttrCSSResolver();
        CssFile cssFile = XMLWorkerHelper.getCSS(new FileInputStream(CSS));
        cssResolver.addCss(cssFile);

        // HTML
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
        htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

        // Pipelines
        PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
        HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
        CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

        // XML Worker
        XMLWorker worker = new XMLWorker(css, true);
        XMLParser p = new XMLParser(worker);
        p.parse(new FileInputStream(HTML));

        // step 5
        document.close();
    }

    /**
     * Main method
     */
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new D05_ParseHtmlCss().createPdf(DEST);
    }
}

Java: Membuat PDF menggunakan Library iText

Berikut merupakan langkah-langkah yang diperlukan untuk membuat pdf file dari dokumen html.

  1. Siapkan library iText.
  2. Tambahkan library pada project yang ada.
  3. Tambahkan file html  dan css di folder di project.
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorker;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import com.itextpdf.tool.xml.css.CssFile;
import com.itextpdf.tool.xml.css.StyleAttrCSSResolver;
import com.itextpdf.tool.xml.html.Tags;
import com.itextpdf.tool.xml.parser.XMLParser;
import com.itextpdf.tool.xml.pipeline.css.CSSResolver;
import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline;
import com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class D04_ParseHtmlCss {

    public static final String HTML = "src/walden.html";
    public static final String CSS = "src/walden.css";
    public static final String DEST = "src/xmlworker/hasil.pdf";

    public void createPdf(String file) throws IOException, DocumentException {
        // step 1
        Document document = new Document();

        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
        writer.setInitialLeading(12.5f);

        // step 3
        document.open();

        // step 4

        // CSS
        CSSResolver cssResolver = new StyleAttrCSSResolver();
        CssFile cssFile = XMLWorkerHelper.getCSS(new FileInputStream(CSS));
        cssResolver.addCss(cssFile);

        // HTML
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
        htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());

        // Pipelines
        PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
        HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
        CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);

        // XML Worker
        XMLWorker worker = new XMLWorker(css, true);
        XMLParser p = new XMLParser(worker);
        p.parse(new FileInputStream(HTML));

        // step 5
        document.close();
    }

    /**
     * Main method
     */
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new D04_ParseHtmlCss().createPdf(DEST);
    }
}

Java: Data Normalization

Berikut merupakan uji coba data normalization menggunakan pemrograman Java.

public class DataNormalization {
	private static final double EXPONENTIAL = 2.718281828;
	private static final double PI = 3.14;

	public static void main(String[] agrs) {
		double[][] data = {
			{3, 5},
			{6, 8},
			{9, 10},
			{12, 14}
		};	

		double[][] result = getSigmoidal(data);

		for (int i = 0; i < result.length; i++) {
			for (int j = 0; j < result[0].length; j++) {
				System.out.print(result[i][j] + "\t");
			}
			System.out.println("");
		}
	}

	public static double[] getMean(double[][] data) {
		double[] mean = new double[data[0].length];

		for (int i = 0; i < data[0].length; i++) {
			for (int j = 0; j < data.length; j++) {
				mean[i] = mean[i] + data[j][i];
			}
			mean[i] = mean[i] / data.length;
		}

		return mean;
	}

	public static double[] getStandardDeviation(double[][] data) {
		double[] stdev = new double[data[0].length];
		double[] mean = getMean(data);

		for (int i = 0; i < data[0].length; i++) {
			for (int j = 0; j < data.length; j++) {
				stdev[i] = stdev[i] + Math.pow((data[j][i] - mean[i]), 2);
			}
			stdev[i] = Math.sqrt(stdev[i] / (data.length - 1));
		}

		return stdev;
	}

	public static double[][] getSigmoidal(double[][] data) {
		double[][] sigmoidal = new double[data.length][data[0].length];
		double[] mean = getMean(data);
		double[] stdev = getStandardDeviation(data);

		for (int i = 0; i < data[0].length; i++) {
			for (int j = 0; j < data.length; j++) {
				sigmoidal[j][i] = (data[j][i] - mean[i]) /  stdev[i];
				sigmoidal[j][i] = (1 - Math.pow(EXPONENTIAL, -sigmoidal[j][i])) / (1 + Math.pow(EXPONENTIAL, -sigmoidal[j][i]));
			}
		}

		return sigmoidal;
	}

	public static double[][] getZScore(double[][] data) {
		double[][] zScore = new double[data.length][data[0].length];
		double[] mean = getMean(data);
		double[] stdev = getStandardDeviation(data);

		for (int i = 0; i < data[0].length; i++) {
			for (int j = 0; j < data.length; j++) {
				zScore[j][i] = (data[j][i] - mean[i]) / stdev[i];
			}
		}

		return zScore;
	}

	public static double[][] getSoftmax(double[][] data) {
		double[][] softmax = new double[data.length][data[0].length];
		double[] mean = getMean(data);
		double[] stdev = getStandardDeviation(data);

		for (int i = 0; i < data[0].length; i++) {
			for (int j = 0; j < data.length; j++) {
				softmax[j][i] = (data[j][i] - mean[i]) / (1 * (stdev[i] / (2 * PI)));
				softmax[j][i] = 1 / (1 + Math.pow(EXPONENTIAL, -softmax[j][i]));
			}
		}

		return softmax;
	}

	public static double[][] getDecimalScaling(double[][] data, int n) {
		double[][] decimalScaling = new double[data.length][data[0].length];

		for (int i = 0; i < data[0].length; i++) {
			for (int j = 0; j < data.length; j++) {
				decimalScaling[j][i] = data[j][i] / Math.pow(10, n);
			}
		}

		return decimalScaling;
	}

	public static double[] getMin(double[][] data) {
		double[] min = new double[data[0].length];

		for (int i = 0; i < min.length; i++) {
			min[i] = Integer.MAX_VALUE;
		}

		for (int i = 0; i < data[0].length; i++) {
			for (int j = 0; j < data.length; j++) {
				if (data[j][i] < min[i]) {
					min[i] = data[j][i];
				}
			}
		}

		return min;
	}

	public static double[] getMax(double[][] data) {
		double[] max = new double[data[0].length];

		for (int i = 0; i < max.length; i++) {
			max[i] = Integer.MIN_VALUE;
		}

		for (int i = 0; i < data[0].length; i++) {
			for (int j = 0; j < data.length; j++) { 				if (data[j][i] > max[i]) {
					max[i] = data[j][i];
				}
			}
		}

		return max;
	}

	public static double[][] getMinMax(double[][] data) {
		double[][] minMax = new double[data.length][data[0].length];
		double[] min = getMin(data);
		double[] max = getMax(data);

		for (int i = 0; i < data[0].length; i++) {
			for (int j = 0; j < data.length; j++) {
				minMax[j][i] = (data[j][i] - min[i]) / (max[i] - min[i]);
			}
		}

		return minMax;
	}
}

Java: K-means Clustering

Proses klaster merupakan sebuah metode yang digunakan untuk menempatkan data ke dalam kelompok dalam jumlah tertentu.

import java.util.Random;

public class KMeans {
	public static void main(String args[]) {
		double[][] data = {
			{-7.59, 128.66},
			{-8.8, 112.53},
			{1.45, 128.83},
			{-5.69, 128.85},
			{-9.42, 107.02},
			{-6.18, 132.68},
			{2.12, 96.53},
			{-8.03, 110.39},
			{-8.41, 120.69},
			{-5.61, 133.38},
			{-0.98, 124.68},
			{-6.61, 130.28},
			{-5.34, 102.94},
			{-0.3, 132.94},
			{-3.32, 127.41},
			{2.49, 95.95},
			{-8.43, 121.83},
			{-0.21, 124.69},
			{-3.76, 101.81},
			{-2.61, 138.42},
			{-2.32, 138.52},
			{3.68, 126.06},
			{-0.59, 127.47},
			{-4.34, 103.44},
			{-7, 103.33},
			{-6.08, 125.88},
			{-1.06, 129.2},
			{-1.11, 129.24},
			{-8.1, 110.37},
			{-6.83, 129.6},
			{-0.48, 132.87},
			{3.76, 126.79},
			{-5.34, 133.95},
			{-9.08, 118.76},
			{1.88, 96.8},
			{5.07, 94.35},
			{-4.94, 133.9},
			{-4.66, 134.18},
			{-4.48, 133.86},
			{-7.42, 129.22},
			{-7.27, 123.15},
			{-4.83, 133.74},
			{-5.13, 133.92},
			{-4.93, 133.93},
			{-0.46, 133.46},
			{-4.81, 133.72},
			{4.93, 94.86},
			{-11.23, 113.66},
			{-4.05, 128.75},
			{-6.74, 128.82},
			{-0.86, 120.42},
			{-6.83, 104.86},
			{-5.76, 127.49},
			{2.7, 128.03},
			{-4.85, 133.83},
			{-5.67, 101.76},
			{-7.88, 106.41},
			{3.75, 94.91},
			{-7.29, 125.67},
			{4.66, 95.14},
			{-5.26, 131.53},
			{-3.43, 100.2},
			{-3.42, 99.39},
			{-3.26, 100.26},
			{-3.7, 99.78},
			{-2.59, 99.65},
			{-3.74, 99.62},
			{-3.88, 99.31},
			{-2.51, 99.62},
			{-3.74, 99.32},
			{-3.69, 99.3},
			{-3.45, 99.93},
			{-3.71, 99.34},
			{-6.48, 130.13},
			{2.82, 128.32},
			{-3.45, 99.42},
			{-3.77, 99.15},
			{-3.59, 99.74},
			{-6.19, 130.64},
			{-6.68, 103.78},
			{-4.57, 134.09},
			{2.49, 126.35},
			{-7.67, 128.53},
			{-7.97, 104.67},
			{-3.74, 99.37},
			{-3.85, 99.39},
			{-8.98, 110.08},
			{-9.37, 119.32},
			{-1.71, 99.31},
			{-8.15, 107.15},
			{0.55, 126.18},
			{1.54, 126.5},
			{-6.66, 130.18},
			{-6.67, 130.64},
			{-3.06, 101.26},
			{-1.9, 139.08},
			{-5.92, 130.87},
			{-8.06, 129.7},
			{1.03, 99.96},
			{-3.42, 100.01},
			{-7.47, 106.04},
			{-5.08, 102.79},
			{-6.21, 130.8},
			{-2.07, 136.48},
			{-3.28, 131.06},
			{-2.53, 129.19},
			{-6.54, 131.51},
			{3.79, 128.04},
			{3.8, 127.84},
			{-6.6, 130.73},
			{-8.41, 106.82},
			{2.62, 98.92},
			{-0.69, 133.07},
			{-6.37, 130.64},
			{-9.21, 127.07},
			{-3.88, 100.02},
			{-11.04, 107.35},
			{-0.21, 123.89},
			{-6.78, 130.04},
			{-7.38, 125.08},
			{-1.48, 132.06},
			{-9.92, 109.54},
			{-6.11, 117.55},
			{-6.6, 103.74},
			{-8.39, 119.74},
			{3.73, 95.95},
			{-7.64, 129.1},
			{-6.54, 130.17},
			{5.7, 93.56},
			{-9.08, 111.19},
			{0.3, 122.22},
			{2.6, 95.78},
			{-0.83, 121.74},
			{3.77, 95.94},
			{-8.19, 119.95},
			{-2.27, 137.99},
			{-4.68, 101.16},
			{-10.47, 113.33},
			{-2.61, 99.95},
			{4, 90.37},
			{0.05, 125},
			{0.05, 125},
			{-0.85, 119.78},
			{0.79, 123.32},
			{-5.21, 130.13},
			{3.63, 126.84},
			{-7.1, 105.15},
			{-5.23, 130.25},
			{-0.94, 124.19},
			{-7.16, 129.31}
		};

		// Jumlah Klaster
		int k = 3;

		double[][] random = getRandom(data, k);
		double[] cluster = getEuclideanDistance(data, random, k);

		for (int i = 0; i < data.length; i++) {
			for (int j = 0; j < data[0].length; j++) {
				System.out.print(data[i][j] + "\t");
			}
			System.out.print(cluster[i]);
			System.out.print("\n");
		}
	}

	public static double[] getEuclideanDistance(double[][] data, double[][] random, int k) {
		double[][] cluster = new double[data.length][k];
		double[] min = new double[data.length];
		double[] clusterClass = new double[data.length];
		double dist = 0;

		for (int a = 0; a < k; a++) {
			for (int b = 0; b < data.length; b++) {
				for (int c = 0; c < data[0].length; c++) {
					dist += Math.pow((data[b][c] - random[a][c]), 2);
				}
				cluster[b][a] = Math.sqrt(dist);
				dist = 0;
			}
		}

		for (int i = 0; i < cluster.length; i++) {
			min[i] = cluster[i][0];
			for (int j = 0; j < cluster[0].length; j++) {
				if (cluster[i][j] < min[i]) {
					min[i] = cluster[i][j];
					clusterClass[i] = j;
				}
			}
		}
		return clusterClass;
	}

	public static double[][] getRandom(double[][] data, int k) {
		double[][] random = new double[k][data[0].length];

		for (int a = 0; a < k; a++) {
			int rand = new Random().nextInt(data.length);
			for (int b = 0; b < data[0].length; b++) {
				random[a][b] = data[rand][b];
			}
		}
		return random;
	}
}