id.co.nlp.MachineTranslationAPP.FormTraining.java Source code

Java tutorial

Introduction

Here is the source code for id.co.nlp.MachineTranslationAPP.FormTraining.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package id.co.nlp.MachineTranslationAPP;

import id.co.nlp.MachineTranslation.MachineTranslation;
import id.co.nlp.MachineTranslation.Utils.Util;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import org.json.JSONObject;

/**
 *
 * @author Andika
 */
public class FormTraining extends javax.swing.JFrame {

    /**
     * Creates new form LATIH
     */
    protected FormTesting formTesting;
    protected JFileChooser fileChooser;
    protected MachineTranslation machineTranslation;
    protected String sourceFile = "";
    protected String targetFile = "";
    protected String dictFile = "";

    public FormTraining(FormTesting formTesting) {
        this.formTesting = formTesting;
        this.fileChooser = new JFileChooser();
        this.machineTranslation = new MachineTranslation();
        initComponents();
        this.jMenu1.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        englishCorpusTextField = new javax.swing.JTextField();
        indCorpusTextField = new javax.swing.JTextField();
        dictTextField = new javax.swing.JTextField();
        englishCorpusButton = new javax.swing.JButton();
        IndButtonCorpus = new javax.swing.JButton();
        dictButtonCorpus = new javax.swing.JButton();
        trainingButton = new javax.swing.JButton();
        resultLabel = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        testingMenu = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("English Corpus");

        jLabel2.setText("Indonesi Cospus");

        jLabel3.setText("DIctionary");

        englishCorpusTextField.setEditable(false);
        englishCorpusTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                englishCorpusTextFieldActionPerformed(evt);
            }
        });

        indCorpusTextField.setEditable(false);
        indCorpusTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                indCorpusTextFieldActionPerformed(evt);
            }
        });

        dictTextField.setEditable(false);
        dictTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dictTextFieldActionPerformed(evt);
            }
        });

        englishCorpusButton.setText("search");
        englishCorpusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                englishCorpusButtonActionPerformed(evt);
            }
        });

        IndButtonCorpus.setText("search");
        IndButtonCorpus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                IndButtonCorpusActionPerformed(evt);
            }
        });

        dictButtonCorpus.setText("search");
        dictButtonCorpus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dictButtonCorpusActionPerformed(evt);
            }
        });

        trainingButton.setText("training");
        trainingButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                trainingButtonActionPerformed(evt);
            }
        });

        jButton1.setText("buka form testing");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jMenu1.setText("File");

        testingMenu.setText("Buka Halaman Testing ");
        testingMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                testingMenuActionPerformed(evt);
            }
        });
        jMenu1.add(testingMenu);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1).addComponent(jLabel3).addComponent(jLabel2)
                                .addComponent(trainingButton))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup().addGap(46, 46, 46).addGroup(layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(resultLabel)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(indCorpusTextField,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 456,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(
                                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(IndButtonCorpus))
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(dictTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        456, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(
                                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(dictButtonCorpus))
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(englishCorpusTextField,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 456,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(
                                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(englishCorpusButton))))
                                .addGroup(layout.createSequentialGroup().addGap(18, 18, 18).addComponent(jButton1)))
                        .addContainerGap(48, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(jLabel1)
                        .addComponent(englishCorpusTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(englishCorpusButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2)
                                .addComponent(indCorpusTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(IndButtonCorpus))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3)
                                .addComponent(dictTextField, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(dictButtonCorpus))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(trainingButton).addComponent(resultLabel).addComponent(jButton1))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void testingMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testingMenuActionPerformed
        this.formTesting.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_testingMenuActionPerformed

    private void englishCorpusTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_englishCorpusTextFieldActionPerformed
        if (this.fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            try {
                this.sourceFile = this.fileChooser.getSelectedFile().getCanonicalPath();
                this.englishCorpusTextField.setText(this.sourceFile);
            } catch (IOException ex) {
                Logger.getLogger(FormTraining.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_englishCorpusTextFieldActionPerformed

    public class FileOpenFilter extends FileFilter {

        private final String extention;
        private final String description;

        public FileOpenFilter(String extenstion, String description) {
            this.extention = extenstion;
            this.description = description;
        }

        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return false;
            } else {
                return file.getName().endsWith(this.extention);
            }
        }

        @Override
        public String getDescription() {
            return this.description + String.format(" (*%s)", this.extention);
        }

    }

    private void englishCorpusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_englishCorpusButtonActionPerformed
        this.fileChooser.setFileFilter(new FileOpenFilter("txt", "Data Latih BP"));
        if (this.fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            try {
                this.sourceFile = this.fileChooser.getSelectedFile().getCanonicalPath();
                this.englishCorpusTextField.setText(this.sourceFile);
            } catch (IOException ex) {
                Logger.getLogger(FormTraining.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_englishCorpusButtonActionPerformed

    private void indCorpusTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_indCorpusTextFieldActionPerformed
        this.fileChooser.setFileFilter(new FileOpenFilter("txt", "Data Latih BP"));
        if (this.fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            try {
                this.targetFile = this.fileChooser.getSelectedFile().getCanonicalPath();
                this.indCorpusTextField.setText(this.targetFile);
            } catch (IOException ex) {
                Logger.getLogger(FormTraining.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_indCorpusTextFieldActionPerformed

    private void dictTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dictTextFieldActionPerformed
        this.fileChooser.setFileFilter(new FileOpenFilter("xml", "Data Latih BP"));
        if (this.fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            try {
                this.dictFile = this.fileChooser.getSelectedFile().getCanonicalPath();
                this.dictTextField.setText(this.dictFile);
            } catch (IOException ex) {
                Logger.getLogger(FormTraining.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_dictTextFieldActionPerformed

    private void dictButtonCorpusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dictButtonCorpusActionPerformed
        if (this.fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            try {
                this.dictFile = this.fileChooser.getSelectedFile().getCanonicalPath();
                this.dictTextField.setText(this.dictFile);
            } catch (IOException ex) {
                Logger.getLogger(FormTraining.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_dictButtonCorpusActionPerformed

    private void IndButtonCorpusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_IndButtonCorpusActionPerformed
        if (this.fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            try {
                this.targetFile = this.fileChooser.getSelectedFile().getCanonicalPath();
                this.indCorpusTextField.setText(this.targetFile);
            } catch (IOException ex) {
                Logger.getLogger(FormTraining.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_IndButtonCorpusActionPerformed

    private void trainingButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_trainingButtonActionPerformed
        if (this.sourceFile.isEmpty() || this.targetFile.isEmpty() || this.dictFile.isEmpty()) {
            JOptionPane.showMessageDialog(this, "data yang anda masukan kurang lengkap");
        } else {
            try {
                long startTime = System.currentTimeMillis();
                this.machineTranslation.training(Util.read(this.sourceFile).toLowerCase(),
                        Util.read(this.targetFile).toLowerCase(), this.dictFile, 2, true);
                new File(System.getProperty("user.home") + File.separator + ".machine_translation").mkdirs();
                this.machineTranslation
                        .save(System.getProperty("user.home") + File.separator + ".machine_translation");
                long endTime = System.currentTimeMillis();
                JSONObject timeProcess = this.getTimeProcess(endTime - startTime);
                this.resultLabel.setText("Pelatihan Selesai dengan waktu pelatihan " + timeProcess.get("hours")
                        + " jam, " + timeProcess.get("minutes") + " menit, " + timeProcess.get("second")
                        + " detik, " + timeProcess.get("milisecond") + " milisecon");
                JOptionPane.showMessageDialog(this, "training selesai");
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage());
                Logger.getLogger(FormTraining.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_trainingButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.formTesting.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private JSONObject getTimeProcess(Long time) {
        JSONObject timeString = new JSONObject();
        timeString.put("days", 0);
        timeString.put("hours", 0);
        timeString.put("menutes", 0);
        timeString.put("second", 0);
        timeString.put("milisecond", 0);
        long temp = 0;
        timeString.put("days", (int) (time / (1000 * 60 * 60 * 24)));
        temp = (time - ((1000 * 60 * 60 * 24) * timeString.getInt("days")));
        timeString.put("hours", (int) (temp / (1000 * 60 * 60)));
        temp = (temp - (timeString.getInt("hours") * (1000 * 60 * 60)));
        timeString.put("minutes", (int) (temp / (1000 * 60)));
        temp = (temp - (timeString.getInt("minutes") * (1000 * 60)));
        timeString.put("second", (temp / 1000));
        temp = temp - (timeString.getInt("second") / 1000);
        timeString.put("milisecond", (int) temp);
        return timeString;
    }

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton IndButtonCorpus;
    private javax.swing.JButton dictButtonCorpus;
    private javax.swing.JTextField dictTextField;
    private javax.swing.JButton englishCorpusButton;
    private javax.swing.JTextField englishCorpusTextField;
    private javax.swing.JTextField indCorpusTextField;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JLabel resultLabel;
    private javax.swing.JMenuItem testingMenu;
    private javax.swing.JButton trainingButton;
    // End of variables declaration//GEN-END:variables
}