Created
November 16, 2020 00:50
-
-
Save MeetMartin/7797470e1d926df09b7d49e53bc36c32 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <title>NLP in a browser</title> | |
| <script src='./dist/bundle.js'></script> | |
| <script> | |
| const {containerBootstrap, Nlp, LangEn, fs} = window.nlpjs; | |
| const setupNLP = async corpus => { | |
| const container = containerBootstrap(); | |
| container.register('fs', fs); | |
| container.use(Nlp); | |
| container.use(LangEn); | |
| const nlp = container.get('nlp'); | |
| nlp.settings.autoSave = false; | |
| await nlp.addCorpus(corpus); | |
| nlp.train(); | |
| return nlp; | |
| }; | |
| const onChatSubmit = nlp => async event => { | |
| event.preventDefault(); | |
| const chat = document.getElementById('chat'); | |
| const chatInput = document.getElementById('chatInput'); | |
| chat.innerHTML = chat.innerHTML + `<p>you: ${chatInput.value}</p>`; | |
| const response = await nlp.process('en', chatInput.value); | |
| chat.innerHTML = chat.innerHTML + `<p>chatbot: ${response.answer}</p>`; | |
| chatInput.value = ''; | |
| }; | |
| (async () => { | |
| const nlp = await setupNLP('https://raw.githubusercontent.com/jesus-seijas-sp/nlpjs-examples/master/01.quickstart/02.filecorpus/corpus-en.json'); | |
| const chatForm = document.getElementById('chatbotForm'); | |
| chatForm.addEventListener('submit', onChatSubmit(nlp)); | |
| })(); | |
| </script> | |
| </head> | |
| <body> | |
| <h1>NLP in a browser</h1> | |
| <div id="chat"></div> | |
| <form id="chatbotForm"> | |
| <input type="text" id="chatInput" /> | |
| <input type="submit" id="chatSubmit" value="send" /> | |
| </form> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment