com.civprod.writerstoolbox.OpenNLP.training.ThoughtAndSpeechTrainer.java Source code

Java tutorial

Introduction

Here is the source code for com.civprod.writerstoolbox.OpenNLP.training.ThoughtAndSpeechTrainer.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 com.civprod.writerstoolbox.OpenNLP.training;

import com.civprod.swing.CollectionListModel;
import com.civprod.writerstoolbox.thoughtAndSpeech.ThoughtAndSpeechEvaluator;
import com.civprod.writerstoolbox.thoughtAndSpeech.ThoughtAndSpeechModel;
import com.civprod.writerstoolbox.thoughtAndSpeech.ThoughtAndSpeechParserFactory;
import com.civprod.writerstoolbox.thoughtAndSpeech.ThoughtAndSpeechParserME;
import com.civprod.writerstoolbox.thoughtAndSpeech.ThoughtAndSpeechSample;
import com.civprod.writerstoolbox.thoughtAndSpeech.ThoughtAndSpeechSampleStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import opennlp.tools.dictionary.Dictionary;
import opennlp.tools.util.MarkableFileInputStreamFactory;
import opennlp.tools.util.ObjectStream;
import opennlp.tools.util.PlainTextByLineStream;
import opennlp.tools.util.TrainingParameters;
import opennlp.tools.util.eval.FMeasure;
import org.apache.commons.math3.stat.descriptive.SummaryStatistics;

/**
 *
 * @author Steven Owens
 */
public class ThoughtAndSpeechTrainer extends EditorWindow<ThoughtAndSpeechModel> {
    private static final long serialVersionUID = 3672L;

    private final CollectionListModel<File> mFileCollectionListModel;
    private Dictionary saidWordsDictionary;
    private Dictionary thoughtWordsDictionary;

    /**
     * Creates new form ThoughtAndSpeechTrainer
     */
    public ThoughtAndSpeechTrainer() {
        initComponents();
        mFileCollectionListModel = new CollectionListModel<>();
        listFiles.setModel(mFileCollectionListModel);
    }

    /**
     * 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() {

        myFileChooser = new javax.swing.JFileChooser();
        jScrollPane1 = new javax.swing.JScrollPane();
        listFiles = new javax.swing.JList<File>();
        cmdLoadTrainingFile = new javax.swing.JButton();
        cmdDeleteTrainingFile = new javax.swing.JButton();
        cmdTrain = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        textTestResults = new javax.swing.JTextArea();
        cmdCreateSaidDictionary = new javax.swing.JButton();
        cmdLoadSaidDictionary = new javax.swing.JButton();
        cmdCreateThoughtWordDictionary = new javax.swing.JButton();
        cmdLoadThoughtWordDictionary = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        listFiles.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };

            public int getSize() {
                return strings.length;
            }

            public Object getElementAt(int i) {
                return strings[i];
            }
        });
        jScrollPane1.setViewportView(listFiles);

        cmdLoadTrainingFile.setText("load training file");
        cmdLoadTrainingFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdLoadTrainingFileActionPerformed(evt);
            }
        });

        cmdDeleteTrainingFile.setText("delete training file");
        cmdDeleteTrainingFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdDeleteTrainingFileActionPerformed(evt);
            }
        });

        cmdTrain.setText("train model");
        cmdTrain.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdTrainActionPerformed(evt);
            }
        });

        textTestResults.setColumns(20);
        textTestResults.setRows(5);
        jScrollPane2.setViewportView(textTestResults);

        cmdCreateSaidDictionary.setText("create said word Dictionary");
        cmdCreateSaidDictionary.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCreateSaidDictionaryActionPerformed(evt);
            }
        });

        cmdLoadSaidDictionary.setText("load said word Dictionary");
        cmdLoadSaidDictionary.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdLoadSaidDictionaryActionPerformed(evt);
            }
        });

        cmdCreateThoughtWordDictionary.setText("create thought word Dictionary");
        cmdCreateThoughtWordDictionary.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCreateThoughtWordDictionaryActionPerformed(evt);
            }
        });

        cmdLoadThoughtWordDictionary.setText("load thought word Dictionary");
        cmdLoadThoughtWordDictionary.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdLoadThoughtWordDictionaryActionPerformed(evt);
            }
        });

        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)
                        .addGroup(layout.createSequentialGroup().addComponent(cmdCreateThoughtWordDictionary)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmdLoadThoughtWordDictionary).addGap(0, 151, Short.MAX_VALUE))
                        .addComponent(jScrollPane2)
                        .addGroup(layout.createSequentialGroup().addGroup(layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup().addComponent(cmdCreateSaidDictionary)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cmdLoadSaidDictionary))
                                .addGroup(layout.createSequentialGroup()
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(cmdLoadTrainingFile)
                                                .addComponent(cmdDeleteTrainingFile)))
                                .addComponent(cmdTrain))
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup().addComponent(cmdLoadTrainingFile)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmdDeleteTrainingFile)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(cmdCreateSaidDictionary).addComponent(cmdLoadSaidDictionary))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(cmdCreateThoughtWordDictionary)
                                .addComponent(cmdLoadThoughtWordDictionary))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(cmdTrain)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 212,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

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

    private void cmdLoadTrainingFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdLoadTrainingFileActionPerformed
        final ThoughtAndSpeechTrainer tempThis = this;
        new Thread(() -> {
            tempThis.setVisible(false);
            int returnval = myFileChooser.showOpenDialog(tempThis);
            if (returnval == JFileChooser.APPROVE_OPTION) {
                final File selectedFile = myFileChooser.getSelectedFile();
                mFileCollectionListModel.add(selectedFile);
            }
            tempThis.setVisible(true);
        }).start();
    }//GEN-LAST:event_cmdLoadTrainingFileActionPerformed

    private void cmdDeleteTrainingFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdDeleteTrainingFileActionPerformed
        mFileCollectionListModel.remove(listFiles.getSelectedValue());
    }//GEN-LAST:event_cmdDeleteTrainingFileActionPerformed

    private void cmdTrainActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdTrainActionPerformed
        final ThoughtAndSpeechTrainer tempThis = this;
        new Thread(() -> {
            textTestResults.setText("");
            Charset charset = Charset.forName("UTF-8");
            //create TokenizerFactory part of the training context
            ThoughtAndSpeechParserFactory myTokenizerFactory = new ThoughtAndSpeechParserFactory("EN",
                    this.saidWordsDictionary, this.thoughtWordsDictionary);

            /*ThoughtAndSpeechParser stdTokenizer = null;
            try {
            stdTokenizer = OpenNLPUtils.createTokenizer();
            } catch (IOException ex) {
            Logger.getLogger(TokenizerTrainer.class.getName()).log(Level.SEVERE, null, ex);
            }*/
            List<FileSplit> FileSplits = FileSplit.generateFileSplitsLOO(mFileCollectionListModel);
            File trainingFile = new File("en-ThoughtAndSpeech.train");
            File testFile = new File("en-ThoughtAndSpeech.test");
            SummaryStatistics curFStats = new SummaryStatistics();
            SummaryStatistics curRecallStats = new SummaryStatistics();
            SummaryStatistics curPrecisionStats = new SummaryStatistics();
            SummaryStatistics stdFStats = new SummaryStatistics();
            SummaryStatistics stdRecallStats = new SummaryStatistics();
            SummaryStatistics stdPrecisionStats = new SummaryStatistics();
            java.io.BufferedOutputStream trainingFileWriter = null;
            for (FileSplit curFileSplit : FileSplits) {
                try {
                    //create training file
                    trainingFileWriter = new java.io.BufferedOutputStream(
                            new java.io.FileOutputStream(trainingFile));
                    for (File curTrainingFile : curFileSplit.getTrainingFiles()) {
                        java.io.BufferedInputStream curTrainingFileReader = null;
                        try {
                            curTrainingFileReader = new java.io.BufferedInputStream(
                                    new java.io.FileInputStream(curTrainingFile));
                            while (curTrainingFileReader.available() > 0) {
                                trainingFileWriter.write(curTrainingFileReader.read());
                            }
                        } catch (IOException ex) {
                            Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE, null, ex);
                        } finally {
                            if (curTrainingFileReader != null) {
                                curTrainingFileReader.close();
                            }
                        }
                    }
                    trainingFileWriter.write('\n');
                } catch (IOException ex) {
                    Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    if (trainingFileWriter != null) {
                        try {
                            trainingFileWriter.close();
                        } catch (IOException ex) {
                            Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
                //create test file
                java.io.BufferedOutputStream testFileWriter = null;
                try {
                    //create training file
                    testFileWriter = new java.io.BufferedOutputStream(new java.io.FileOutputStream(testFile));
                    for (File curTrainingFile : curFileSplit.getTestFiles()) {
                        String testingFileName = curTrainingFile.getCanonicalPath();
                        textTestResults
                                .setText(textTestResults.getText() + "testing with " + testingFileName + "\n");
                        java.io.BufferedInputStream curTrainingFileReader = null;
                        try {
                            curTrainingFileReader = new java.io.BufferedInputStream(
                                    new java.io.FileInputStream(curTrainingFile));
                            while (curTrainingFileReader.available() > 0) {
                                int read = curTrainingFileReader.read();
                                testFileWriter.write(read);
                            }
                        } catch (IOException ex) {
                            Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE, null, ex);
                        } finally {
                            if (curTrainingFileReader != null) {
                                curTrainingFileReader.close();
                            }
                        }
                    }
                    testFileWriter.write('\n');
                } catch (IOException ex) {
                    Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    if (testFileWriter != null) {
                        try {
                            testFileWriter.close();
                        } catch (IOException ex) {
                            Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
                //create and train model
                ObjectStream<String> trainingLineStream = null;
                ThoughtAndSpeechModel train = null;
                try {
                    trainingLineStream = new PlainTextByLineStream(new MarkableFileInputStreamFactory(trainingFile),
                            charset);
                    ObjectStream<ThoughtAndSpeechSample> sampleStream = null;
                    try {
                        sampleStream = new ThoughtAndSpeechSampleStream(trainingLineStream);
                        train = ThoughtAndSpeechParserME.train("en", sampleStream, myTokenizerFactory,
                                TrainingParameters.defaultParams());
                    } catch (IOException ex) {
                        Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE, null, ex);
                    } finally {
                        if (sampleStream != null) {
                            try {
                                sampleStream.close();
                            } catch (IOException ex) {
                                Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE, null,
                                        ex);
                            }
                        }
                    }
                } catch (IOException ex) {
                    Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    if (trainingLineStream != null) {
                        try {
                            trainingLineStream.close();
                        } catch (IOException ex) {
                            Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
                if (train != null) {
                    ObjectStream<String> testingLineStream = null;
                    try {
                        testingLineStream = new PlainTextByLineStream(new MarkableFileInputStreamFactory(testFile),
                                charset);
                        ObjectStream<ThoughtAndSpeechSample> sampleStream = null;
                        try {
                            sampleStream = new ThoughtAndSpeechSampleStream(testingLineStream);
                            ThoughtAndSpeechParserME testDetector = new ThoughtAndSpeechParserME(train);
                            ThoughtAndSpeechEvaluator evaluator = new ThoughtAndSpeechEvaluator(testDetector);
                            evaluator.evaluate(sampleStream);
                            FMeasure testFMeasure = evaluator.getFMeasure();
                            curFStats.addValue(testFMeasure.getFMeasure());
                            curRecallStats.addValue(testFMeasure.getRecallScore());
                            curPrecisionStats.addValue(testFMeasure.getPrecisionScore());
                            textTestResults.setText(textTestResults.getText() + testFMeasure.getFMeasure() + " "
                                    + testFMeasure.getPrecisionScore() + " " + testFMeasure.getRecallScore()
                                    + "\n");
                            /*if (stdTokenizer != null) {
                            testingLineStream = new PlainTextByLineStream(new FileInputStream(testFile), charset);
                            sampleStream = new TokenSampleStream(testingLineStream);
                            TokenizerEvaluator stdEvaluator = new TokenizerEvaluator(stdTokenizer);
                            stdEvaluator.evaluate(sampleStream);
                            FMeasure stdFMeasure = stdEvaluator.getFMeasure();
                            stdFStats.addValue(stdFMeasure.getFMeasure());
                            stdRecallStats.addValue(stdFMeasure.getRecallScore());
                            stdPrecisionStats.addValue(stdFMeasure.getPrecisionScore());
                            textTestResults.setText(textTestResults.getText() + " " + stdFMeasure.getFMeasure() + " " + stdFMeasure.getPrecisionScore() + " " + stdFMeasure.getRecallScore()  + "\n");
                            }*/
                            textTestResults.setText(textTestResults.getText() + "\n");
                        } catch (IOException ex) {
                            Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE, null, ex);
                        } finally {
                            if (sampleStream != null) {
                                try {
                                    sampleStream.close();
                                } catch (IOException ex) {
                                    Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE,
                                            null, ex);
                                }
                            }
                        }
                    } catch (IOException ex) {
                        Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE, null, ex);
                    } finally {
                        if (testingLineStream != null) {
                            try {
                                testingLineStream.close();
                            } catch (IOException ex) {
                                Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE, null,
                                        ex);
                            }
                        }
                    }
                }
            }
            textTestResults.setText(textTestResults.getText() + "\n");
            textTestResults.setText(textTestResults.getText() + "test model\n");
            textTestResults.setText(textTestResults.getText() + "f score mean " + curFStats.getMean() + " stdDev "
                    + curFStats.getStandardDeviation() + "\n");
            textTestResults.setText(textTestResults.getText() + "recall mean " + curRecallStats.getMean()
                    + " stdDev " + curRecallStats.getStandardDeviation() + "\n");
            textTestResults.setText(textTestResults.getText() + "precision score mean "
                    + curPrecisionStats.getMean() + " stdDev " + curPrecisionStats.getStandardDeviation() + "\n");
            textTestResults.setText(textTestResults.getText() + "std model\n");
            textTestResults.setText(textTestResults.getText() + "f score mean " + stdFStats.getMean() + " stdDev "
                    + stdFStats.getStandardDeviation() + "\n");
            textTestResults.setText(textTestResults.getText() + "recall mean " + stdRecallStats.getMean()
                    + " stdDev " + stdRecallStats.getStandardDeviation() + "\n");
            textTestResults.setText(textTestResults.getText() + "precision score mean "
                    + stdPrecisionStats.getMean() + " stdDev " + stdPrecisionStats.getStandardDeviation() + "\n");
            //create combinded training file
            trainingFileWriter = null;
            try {
                trainingFileWriter = new java.io.BufferedOutputStream(new java.io.FileOutputStream(trainingFile));
                for (File curTrainingFile : mFileCollectionListModel) {
                    java.io.BufferedInputStream curTrainingFileReader = null;
                    try {
                        curTrainingFileReader = new java.io.BufferedInputStream(
                                new java.io.FileInputStream(curTrainingFile));
                        while (curTrainingFileReader.available() > 0) {
                            trainingFileWriter.write(curTrainingFileReader.read());
                        }
                    } catch (IOException ex) {
                        Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE, null, ex);
                    } finally {
                        if (curTrainingFileReader != null) {
                            curTrainingFileReader.close();
                        }
                    }
                }
                trainingFileWriter.write('\n');
            } catch (IOException ex) {
                Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                if (trainingFileWriter != null) {
                    try {
                        trainingFileWriter.close();
                    } catch (IOException ex) {
                        Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
            //create and train model
            ObjectStream<String> lineStream = null;
            this.createdObject = null;
            try {
                lineStream = new PlainTextByLineStream(new MarkableFileInputStreamFactory(trainingFile), charset);
                ObjectStream<ThoughtAndSpeechSample> sampleStream = null;
                try {
                    sampleStream = new ThoughtAndSpeechSampleStream(lineStream);
                    this.createdObject = ThoughtAndSpeechParserME.train("en", sampleStream, myTokenizerFactory,
                            TrainingParameters.defaultParams());
                } catch (IOException ex) {
                    Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    if (sampleStream != null) {
                        try {
                            sampleStream.close();
                        } catch (IOException ex) {
                            Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            } catch (IOException ex) {
                Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                if (lineStream != null) {
                    try {
                        lineStream.close();
                    } catch (IOException ex) {
                        Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
            if (createdObject != null) {
                OutputStream modelOut = null;
                File modelFile = new File("en-ThoughtAndSpeech-token.bin");
                try {
                    modelOut = new BufferedOutputStream(new FileOutputStream(modelFile));
                    createdObject.serialize(modelOut);
                } catch (IOException ex) {
                    Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    if (modelOut != null) {
                        try {
                            modelOut.close();
                        } catch (IOException ex) {
                            Logger.getLogger(SentenceDetectorTrainer.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }
            textTestResults.setText(textTestResults.getText() + "done");
        }).start();
    }//GEN-LAST:event_cmdTrainActionPerformed

    private void cmdCreateSaidDictionaryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCreateSaidDictionaryActionPerformed
        final ThoughtAndSpeechTrainer tempThis = this;
        new Thread(() -> {
            final DictionaryEditor newDictionaryEditor = new DictionaryEditor();
            newDictionaryEditor.runOnClose(() -> {
                this.saidWordsDictionary = newDictionaryEditor.getCreatedObject();
                tempThis.setVisible(true);
            });
            tempThis.setVisible(false);
            newDictionaryEditor.setVisible(true);
        }).start();
    }//GEN-LAST:event_cmdCreateSaidDictionaryActionPerformed

    private void cmdLoadSaidDictionaryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdLoadSaidDictionaryActionPerformed
        final ThoughtAndSpeechTrainer tempThis = this;
        new Thread(() -> {
            tempThis.setVisible(false);
            int returnval = myFileChooser.showOpenDialog(tempThis);
            if (returnval == JFileChooser.APPROVE_OPTION) {
                final File selectedFile = myFileChooser.getSelectedFile();
                InputStream DictionaryInputStream = null;
                try {
                    DictionaryInputStream = new java.io.BufferedInputStream(new FileInputStream(selectedFile));
                    this.saidWordsDictionary = new Dictionary(DictionaryInputStream);
                } catch (IOException ex) {
                    Logger.getLogger(DictionaryEditor.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    if (DictionaryInputStream != null) {
                        try {
                            DictionaryInputStream.close();
                        } catch (IOException ex) {
                            Logger.getLogger(DictionaryEditor.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }
            tempThis.setVisible(true);
        }).start();
    }//GEN-LAST:event_cmdLoadSaidDictionaryActionPerformed

    private void cmdCreateThoughtWordDictionaryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCreateThoughtWordDictionaryActionPerformed
        final ThoughtAndSpeechTrainer tempThis = this;
        new Thread(() -> {
            final DictionaryEditor newDictionaryEditor = new DictionaryEditor();
            newDictionaryEditor.runOnClose(() -> {
                this.thoughtWordsDictionary = newDictionaryEditor.getCreatedObject();
                tempThis.setVisible(true);
            });
            tempThis.setVisible(false);
            newDictionaryEditor.setVisible(true);
        }).start();
    }//GEN-LAST:event_cmdCreateThoughtWordDictionaryActionPerformed

    private void cmdLoadThoughtWordDictionaryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdLoadThoughtWordDictionaryActionPerformed
        final ThoughtAndSpeechTrainer tempThis = this;
        new Thread(() -> {
            tempThis.setVisible(false);
            int returnval = myFileChooser.showOpenDialog(tempThis);
            if (returnval == JFileChooser.APPROVE_OPTION) {
                final File selectedFile = myFileChooser.getSelectedFile();
                InputStream DictionaryInputStream = null;
                try {
                    DictionaryInputStream = new java.io.BufferedInputStream(new FileInputStream(selectedFile));
                    this.thoughtWordsDictionary = new Dictionary(DictionaryInputStream);
                } catch (IOException ex) {
                    Logger.getLogger(DictionaryEditor.class.getName()).log(Level.SEVERE, null, ex);
                } finally {
                    if (DictionaryInputStream != null) {
                        try {
                            DictionaryInputStream.close();
                        } catch (IOException ex) {
                            Logger.getLogger(DictionaryEditor.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }
            tempThis.setVisible(true);
        }).start();
    }//GEN-LAST:event_cmdLoadThoughtWordDictionaryActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ThoughtAndSpeechTrainer.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(() -> {
            new ThoughtAndSpeechTrainer().setVisible(true);
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdCreateAbbreviationDictionary;
    private javax.swing.JButton cmdCreateSaidDictionary;
    private javax.swing.JButton cmdCreateThoughtWordDictionary;
    private javax.swing.JButton cmdDeleteTrainingFile;
    private javax.swing.JButton cmdLoadSaidDictionary;
    private javax.swing.JButton cmdLoadThoughtWordDictionary;
    private javax.swing.JButton cmdLoadTrainingFile;
    private javax.swing.JButton cmdTrain;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList<File> listFiles;
    private javax.swing.JFileChooser myFileChooser;
    private javax.swing.JTextArea textTestResults;
    // End of variables declaration//GEN-END:variables
}