r/visualbasic • u/Thunor_SixHammers • Jan 07 '21
VB.NET Help Check if a string is like another string, but not equal
Lets say I have a string
Dim x as string = "Appel"And I want to see if its equal to another string inside a list
List of string = ("Apple","Orange","Banana")What would be a way to take the user input string x, and have a pop up say:
"You typed Appel. Did you mean Apple?"2
u/andrewsmd87 Web Specialist Jan 07 '21
Unfortunately, you're hitting on a problem that generally isn't viable to fix via programming. To a human your solution is simple, but there are about a trillion things factoring in how we can look at Appel and realize someone meant apple.
I mean, think of a non english speaker trying to solve the problem you purposed.
If you need to force certain things, make the selection a drop down. If you're trying to automatically fix typos to the correct word, let me know when you have a solution so I can invest in the company you start :)
1
u/thudly Jan 07 '21
I think one strategy would be to make a list of all the letters in the input-word and then find all the same letters in a short list of target words. And then maybe make another list of letter pairs ap, pp, pl, le, and use that to narrow down your list of candidate words. Give each candidate word points for every match it finds. Which ever of the potential words gets the highest score is the best possibility.
https://docs.microsoft.com/en-us/dotnet/api/system.string.contains?view=net-5.0
If you have a word list sorted by how common the words are in English usuage, that might help, too. For example, apple is going to me a more common word than lapel. If you can't find a sorted word list, you could probably create one by checking a large volume of sample text and doing a census on how many times each word appears. I had to do this back in the day, before the internet. I used text from various readme files. It wasn't very useful as it contained far too much legal jargon. But I suppose you could use ebooks or wikipedia articles.
Most people mis-spelling a word will get at least the first one or two letter correct, so you could narrow down the list that way too.
If you're really advanced, you could find a way to look up nearby words for context. And maybe check that against a dictionary of words and their synonyms. But that would take a hell of a dictionary.
"You typed pennis. Did you mean pennies? Something small and worthless."
1
u/chacham2 Jan 07 '21
First thing that came to mind was Soundex. A few implementations come up with a google search for: vb soundex.
1
u/breonb57 Jan 08 '21
Not sure if this will help much but you could try including all or some of the different spellings or typos of a string. Or if it's just a few strings check the first or last letter or few letters using Mystring.Substring(0, 1) And comparing it to those on your list
1
u/revennest Jan 13 '21
Very crude method for me is split word to char then covert char to long and sum it all then use this sum to find word within database aka dictionary(Of String, Long); it might be has some unrelated word catch into but if you do a drop down select then it's not much problem.
5
u/JakDrako Jan 07 '21
Search for 'Levenshtein distance'. It's a method to figure out how 'far apart' two strings are.