0

Is it possible for c# to look past typos in user input and execute the proceeding code given that the user input is very similar to the condition. Lets say for example, we ask the user to pick an option from a switch consisting of colours."blue" is the desired colour and this has some code following it but the user inputs "bluu" or "glue", would c# be able to pick the "blue" option given that it can recognise the input is closer to blue then other colours such as "red". Is there some sort of method we could apply to this.

hameedmd
  • 3
  • 2
  • The code would be very simple hence I didnt write any! This was more of a theoretical question to see if it is possible and HOW it could be implemented. – hameedmd Jul 30 '19 at 11:22
  • 3
    You will need to write the code that accepts these inputs, so you need to figure out how to do it. You could go for something like levenshtein distance and pick the closest option, but C# or .NET by itself does not have anything like this built in. – Lasse V. Karlsen Jul 30 '19 at 11:22
  • 4
    However, if you want the user to pick from a list of options, why not do just that? Why do you want to allow the user to type anything at all? Use a dropdown or similar. – Lasse V. Karlsen Jul 30 '19 at 11:23
  • This question will probably get closed, as it's way to broad - However to entertain the thought experiment: I guess you would need some kind of machine learning script, that learns possible similar inputs and accepts them. However in this case, actual checkboxes, comboboxes,... with fixed values to select from are probably a better option. – Roland Deschain Jul 30 '19 at 11:24
  • Here's C# implementation of Levenshtein distance https://blogs.msdn.microsoft.com/toub/2006/05/05/generic-levenshtein-edit-distance-with-c/ – Lokki Jul 30 '19 at 11:24
  • @LasseVågsætherKarlsen I asked myself the same question. In the simplest possible answer, I have a very long complex code for a mathematical calculator which heavily relies on user input, I myself have made tiny 1 letter errors which have messed up my whole equation calculation leading me to start again. I want to give the user some safety so the code isnt as tedious due to one little mistake. – hameedmd Jul 30 '19 at 11:25
  • Or an auto-complete, if there are a lot of choices. Use the right UI for the job. If you don't want the user to be able to enter anything they like, then don't provide that ability. Go and look at some well known websites and apps and look at how they provide choices. – ADyson Jul 30 '19 at 11:26
  • @RolandDeschain _"you would need some kind of machine learning script"_ - Not at all. You can do pretty alright with a dictionary and Levenshtein. – Fildor Jul 30 '19 at 11:28
  • P.s. if someone is asking you to implement it in this particular way, then part of your job as a programmer sometimes is to explain to them why that's a bad idea. The only reason this wouldn't be a bad idea that I can think of is either as an academic exercise (to see if you can make it work) or because it's a machine learning or similar dictionary building system – ADyson Jul 30 '19 at 11:28
  • @LasseVågsætherKarlsen I misread what you said :), How would a dropdown be implemented as I havent come across this as I have only been coding a few weeks. Thanks. – hameedmd Jul 30 '19 at 11:28
  • @ADyson The code is heavily reliant on user input so I dont think auto-complete would give much use to the user. – hameedmd Jul 30 '19 at 11:31
  • @RolandDeschain a machine learning script sounds like a very cool idea but wouldnt this be hard to create and implement. – hameedmd Jul 30 '19 at 11:33
  • Yes, this would be like using a V8 engine to run a blender. However as @Fildor pointed out, there are easier approaches like the Levenshtein distance. – Roland Deschain Jul 30 '19 at 11:36
  • Furthermore would a drop down list be implementable in a console app as I wasnt aware this was possible. @LasseVågsætherKarlsen – hameedmd Jul 30 '19 at 11:36
  • It would be very hard to do that in a console app. Not impossible, but harder. Input is probably your best option in this case. – Lasse V. Karlsen Jul 30 '19 at 11:59
  • @hameedmd ah you didn't mention it was a console app....that would have saved you a lot of this discussion. Although you might want to consider if a console app is really the best medium for delivering this functionality. – ADyson Jul 31 '19 at 08:31
  • @ADyson Yes that was my bad ;) however the tips from all have been helpful and will only help broaden my understanding so thanks! – hameedmd Jul 31 '19 at 11:50

1 Answers1

0

You could look into using the Levenshtein Distance Algorithm to check how similar the strings are, maybe if the algorithm finds the strings are only one character in difference you could then execute whatever code. It would probably be easier to use some conditional statements as opposed to a switch in this case.

see this answer for an imlementation of this algorithm

Sanogoals
  • 31
  • 9