Next Word Predictor

Developer
Size
2,776 Kb
Views
50,600

How do I make an next word predictor?

Pen which uses naive word prediction (trigrams, grams, tokenization) to predict the next word to be typed.. What is a next word predictor? How do you make a next word predictor? This script and codes were developed by Rohit Falor on 05 November 2022, Saturday.

Next Word Predictor Previews

Next Word Predictor - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Next Word Predictor</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <textarea id="text"></textarea>
<button id="predict" hidden>Predict</button>
<div id="info">This is a pen which uses naive methods of word prediction (trigrams, bigrams, tokenization) to predict the next word to be typed in the box. Start typing and give it a try!</div>
<span id="msg">Next word is:</span>
<span id="word"></span> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Next Word Predictor - Script Codes CSS Codes

* { margin: 5px;
}
#text, #info { width: 400px; height: 200px; font-size: 1.3em; font-family: calibri;
}
#msg, #word { font-size: 1.3em; font-family: calibri; margin-top: 10px; display: block; color: #333;
}
#word { font-size: 1.5em; color: #000;
}
#info { position: absolute; margin-left: 450px; top: 5px; font-size: 1.2em;
}

Next Word Predictor - Script Codes JS Codes

var count = 0;
var hasBigram = false;
var trigram = [{ firstTwo: "The chat", third: ["was", "the", "wanted"]
}];
$('#text').keyup(function (event) { if (event.keyCode === 32) $('#predict').trigger('click');
});
$('#predict').click(function (event) { var words = $('#text').val().toLowerCase().split(" "); words.pop(); for (var i = 0; i < words.length - 2; i++) { var firstTwoWords = words[count] + " " + words[count + 1]; var thirdWord = words[count + 2]; hasBigram = false; trigram.forEach(function (arrayItem) { if (arrayItem.firstTwo === firstTwoWords) { hasBigram = true; arrayItem.third.push(thirdWord); } }); if (!hasBigram) { thirdWord = thirdWord.split(); trigram.push({ firstTwo: firstTwoWords, third: thirdWord }); } count++; } var lastWords = words[words.length - 2] + " " + words[words.length - 1]; trigram.forEach(function (arrayItem) { if (arrayItem.firstTwo === lastWords) $('#word').text(getMaxOccurrence(arrayItem.third)); }); count = 0; function getMaxOccurrence(arr) { var o = {}, maxCount = 0, maxValue, m; for (var i = 0, iLen = arr.length; i < iLen; i++) { m = arr[i]; if (!o.hasOwnProperty(m)) { o[m] = 0; }++o[m]; if (o[m] > maxCount) { maxCount = o[m]; maxValue = m; } } return maxValue; }
});
Next Word Predictor - Script Codes
Next Word Predictor - Script Codes
Home Page Home
Developer Rohit Falor
Username rfalor
Uploaded November 05, 2022
Rating 3
Size 2,776 Kb
Views 50,600
Do you need developer help for Next Word Predictor?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Rohit Falor (rfalor) Script Codes
Create amazing captions with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!