17

I'm making a chat responder for a game and i want know if there is a way you can compare two strings and see if they are approximatley equal to each other for example:

if someone typed: "Strength level?" it would do a function.. then if someone else typed: "Str level?" it would do that same function, but i want it so that if someone made a typo or something like that it would automatically detect what they're trying to type for example: "Strength tlevel?" would also make the function get called.

is what I'm asking here something simple or will it require me to make a big giant irritating function to check the Strings?

if you've been baffled by my explanation (Not really one of my strong points) then this is basically what I'm asking.

How can I check if two strings are similar to each other?

Shaun Wild
  • 1,237
  • 3
  • 17
  • 34

6 Answers6

18

See this question and answer: Getting the closest string match

Using some heuristics and the Levenshtein distance algorithm, you can compute the similarity of two strings and take a guess at whether they're equal.

enter image description here

Your only option other than that would be a dictionary of accepted words similar to the one you're looking for.

Community
  • 1
  • 1
Alain
  • 26,663
  • 20
  • 114
  • 184
6

You can use Levenshtein distance.

maxlath
  • 1,804
  • 15
  • 24
Sandro
  • 1,266
  • 2
  • 13
  • 25
2

I believe you should use one of Edit distance algorithms to solve your problem. Here is for example Levenstein distance algorithm implementation in java. You may use it to compare words in the sentences and if sum of their edit distances would be less than for example 10% of sentence length consider them equals.

amukhachov
  • 5,822
  • 1
  • 41
  • 60
1

Perhaps what you need is a large dictionary for similar words and common spelling mistakes, for which you would use for each word to "translate" to one single entry or key.

This would be useful for custom words, so you could add "str" in the same key as "strength".

However, you could also make a few automated methods, i.e. when your word isn't found in the dictionary, to loop recursively for 1 letter difference (either missing or replaced) and can recurse into deeper levels, i.e. 2 missing letters etc.

ericosg
  • 4,926
  • 4
  • 37
  • 59
1

I found a few projects that do text to phonemes translations, don't know which one is best

Olivier Refalo
  • 50,287
  • 22
  • 91
  • 122
1

If you want to find similar word beginnings, you can use a stemmer. Stemmers reduce words to a common beginning. The most known algorithm if the Port Stemmer (http://tartarus.org/~martin/PorterStemmer).

Levenshtein, as pointed above, is great, but computational heavy for distances greater than one or two.

Alberto
  • 499
  • 4
  • 23