Next Word Predictor
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 - 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; }
});
Developer | Rohit Falor |
Username | rfalor |
Uploaded | November 05, 2022 |
Rating | 3 |
Size | 2,776 Kb |
Views | 50,600 |
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!
Name | Size |
Get Content from Wikipedia | 2,267 Kb |
IOS icon navbar | 1,775 Kb |
Color Predictor | 2,937 Kb |
Contact Cards | 5,184 Kb |
Various input styles | 1,681 Kb |
JS Resources | 2,479 Kb |
Avoiding obstacles | 2,566 Kb |
A Pen by Rohit Falor | 3,872 Kb |
A Pen by Rohit Falor | 3,014 Kb |
Dynamic Table UI | 3,918 Kb |
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!
Name | Username | Size |
Button shaking | SusanneLundblad | 2,227 Kb |
FreeCodeCamp - Simon Game | Ivhed | 8,481 Kb |
SVG Text Masking | JMChristensen | 2,141 Kb |
Disable JavaScript execution from console | Ludviglindblom | 2,534 Kb |
Flat buttons for Eliassen.com | Kdooley89 | 1,737 Kb |
Scroll to top button | DominicFrancois | 3,743 Kb |
Javascript Welcome | Peterlewicki | 1,573 Kb |
Highbrow Basic HTML Lesson 7 | Kimlarocca | 1,662 Kb |
Dragonball Dragon Radar | DouglasGlover | 2,157 Kb |
STIKHOI | Denmch | 7,122 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!