Skip to content
Advertisement

Is there anyway to simulate a “Did you mean” in Java Script?

So I’m creating a bot with an API, and the list is pretty case sensitive and only allowing exact matches. For example, there I have the word “ENCHANTED_GLISTERING_MELON”. Its all-caps have underscores and complicated spelling, and the site does not accept if it is not an exact match. It is not so user-friendly. Is there any way to so that when a user inputs something, it will auto-capitalize, replace spaces with underscores, and most importantly, check for misspellings, then consider the closest word? I have a dictionary of what the site accepts.

Advertisement

Answer

It not a a simple task to disallow some words with typos.

To avoid reinventing the wheel I would recommend you to use the one of the Open Source engines like RASA to enable neural language processing with your chat.

https://rasa.com/

However, it’s not so easy to use if you having troubles with parsing the string in JavaScript.

For a words similarities you check Levenshtein Distance algorithm:

https://www.npmjs.com/package/autocorrect

https://www.npmjs.com/package/string-similarity

Getting the closest string match

For a simple solution you can just replace your disallowed words:

How to replace several words in javascript

Also, if it’s just a filter for a bad words in your chat you can use some existing libraries like bad-words:

https://www.npmjs.com/package/bad-words

And you can capitalize everything for your particular strange case:

'enchanted glistering melon'.trim().replace(/ /g,'_').toLocaleUpperCase()
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement