Creating Your First AI (Artificial Intelligence) Program

Incomplete: Building a Neural Network that judges whether or not a sentence is complete or incomplete (fragment)..

By now you’ve undoubtedly heard of the terms artificial intelligence, machine learning, or neural network.  So, I’m not going to waste your time trying to convince you how important this topic is. You’re here reading this, so you already know.  

I was inspired by an article written by Daniel Simmons entitled “You can build a neural network in JavaScript even if you don’t really understand neural networks.”  He does a really great job of explaining how to set up your files and training data. The only problem is that his example isn’t as useful as I would like it to be. First off, his network takes a tweet and then tells you if it is a tweet from Donald Trump or Kim Kardashian.  And second, his output is sent to what is called a console.log, and if you don’t know what that is, there is the problem. I wanted to be able to open a web page, type in a sentence, click submit, and have the machine tell me if it is a complete or incomplete sentence right on the page, so that is what I built.  

Let’s get started.

First, you'll want to go to Then, in Google Chrome, right click on the page and from the menu select View page source.  This will open another tab in your browser that lets you see the code underneath the web page.  Here is the underlying code for the sentence checker neural network I built. This is the index.html file.  If you click on the file names that are linked at the bottom, the browser will open them up as well.

You will also need the other three files:  brain.js, training-data.js, and script.js. So, you’ll need a total of four files: index.html, brain.js, training-data.js, and script.js.  You don’t really need to do anything with index.html and brain.js. Just download them, or copy and paste their contents into your own files.  I have also made a zip file of them that you can download from here:

The two files you will need to change to suit your needs are training-data.js and script.js.  Here is what training-data.js looks like.

const trainingData = [                


       input: "I am great.",            

       output: { compsent: 1 }


       input: "Inside Chi's nursery.",

       output: { incompsent: 1 }


       input: "Why my hair pink?",

       output: { incompsent: 1 }


       input: "Feeling blue.",

       output: { incompsent: 1 }


       input: "I love my mom.",

       output: { compsent: 1 }


       input: "I don’t really do wigs.",

       output: { compsent: 1 }


       input: "Wait the thought of going back.",

       output: { incompsent: 1 }


       input: "Live on instagram!",

       output: { incompsent: 1 }



For you to use the training-date.js file, all you need to do is open up the document and change whatever is within the quotation marks that are to the right of input.  For example, you could put examples of email subject lines in quotations marks.  Then, you would need to identify whether the title is an example of spam or not spam.  So, for spam identification your training-data.js file would look something like this.

const trainingData = [                


       input: "Earn $1000s working from home.",            

       output: { spam: 1 }


       input: "Your brother’s birthday party.",

       output: { notspam: 1 }


       input: "Get your tax refund immediately.",

       output: { spam: 1 }



As you can see, the input line provides the example, and the output line classifies the example.  For this example, I am only classifying them under two different categories for the sake of time and computing power.  The larger your training set, the better. Currently, I am working on a javascript file that can simply collect sentences from large sets of text.  The hard part will be trying to find incomplete sentences that have been written by novice writers.

The next step is the script file.  This is what it looks like. For this file, you'll want to change the words "incompsent" and "compsent" to whatever terms you'll be using.

I made comments in the code below.  Anything after a double backslash is a comment and is not processed by the browser eg //.

let trainedNet; // This is the variable to create a computational worker.

//Below are the four functions that make up this javascript file.

//Each function represents a program that will be run.  

//The function below converts the text into a numeric character code.

function encode(arg) {  

   return arg.split('').map(x => (x.charCodeAt(0) / 256));


// This function processes the training data into its separate arrays.

function processTrainingData(data)


   return => {

       return {

           input: encode(d.input),

           output: d.output




// This function trains the neural network

function train(data) {

   let net = new brain.NeuralNetwork();

   net.train(processTrainingData(data, {

    errorThresh: 0.005,  // error threshold to reach

 iterations: 20000,   // maximum training iterations

 log: true,           // console.log() progress periodically

 logPeriod: 10,       // number of iterations between logging

 learningRate: 0.1    // learning rate


   trainedNet = net.toFunction();

document.getElementById("rslts").innerHTML = "Training complete.";


//This function begins the process and then outputs the results to the web page.

function execute() {

var sentence = document.getElementById("textfield").value;

var input = sentence;

   let results = trainedNet(encode(input));  


   let output;

   let certainty;

//The if-then statement below picks the winner based on the greater certainty

   if (results.compsent > results.incompsent) { //This determines the results

       output = 'a complete sentence.' //Tells you that the sentence is complete.  

       certainty = Math.floor(results.compsent * 100) //Provides certainty of choice.

       document.getElementById("rslts").innerHTML = "I'm " + certainty + "% sure

       that '" + sentence + "' is " + output; // Outputs to the web page.

   } else {

       output = 'an incomplete sentence.'

       certainty = Math.floor(results.incompsent * 100)

        document.getElementById("rslts").innerHTML = "I'm " + certainty +     

       "% sure that '" + sentence + "' is " + output; // Outputs to the web page.


   return "I'm " + certainty + "% sure that this is " + output;


train(trainingData); // This runs function train(data).

The most important part of this is the training data.  The larger the training set and the more examples you have the better.  

So, now that you have a glimpse at the files.  You need to upload them to a website or you can download a program like XAMPP and run it on your personal computer.  I am currently using portable XAMPP I downloaded from It is really easy to use. Look at the image below.  When you install XAMPP, it creates a folder called htdocs. You create a folder in the htdocs folder, and then copy your files to it.  I created a folder called brain. Once you’ve done that, open XAMPP, and click the Start button across from Apache. Open your browser and type in http://localhost/brain/   

Remember to create a large data set.  


Simmons, Daniel. (2018). You can build a neural network in JavaScript even if you don’t really understand neural networks [article]. Retrieved                 from

Simmons, Daniel. (2018). lordpoint/neural-network-author-classifier [github repository]. Retrieved from                        network-author-classifier