0

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.

  • 2
    I would assume the answer is yes. If you have issues finding a starting point try this: https://en.wikipedia.org/wiki/Levenshtein_distance – pintxo Aug 13 '20 at 07:13
  • For misspellings, https://github.com/farzher/fuzzysort is a good option. – Trentium Aug 13 '20 at 19:01

1 Answers1

1

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()
Ievgen
  • 4,261
  • 7
  • 75
  • 124