Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if two Strings are approximately equal?

Tags:

java

string

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?

like image 893
Shaun Wild Avatar asked Apr 09 '12 13:04

Shaun Wild


People also ask

How do you know if two strings are almost equal?

Two strings word1 and word2 are considered almost equivalent if the differences between the frequencies of each letter from 'a' to 'z' between word1 and word2 is at most 3 . Given two strings word1 and word2 , each of length n , return true if word1 and word2 are almost equivalent, or false otherwise.

Can we use == operator for strings?

In String, the == operator is used to comparing the reference of the given strings, depending on if they are referring to the same objects. When you compare two strings using == operator, it will return true if the string variables are pointing toward the same java object. Otherwise, it will return false .

How do you check if two strings are almost equal in Python?

operators. The simplest way to check if two strings are equal in Python is to use the == operator. And if you are looking for the opposite, then !=

Can we use == to compare strings in JavaScript?

In JavaScript, strings can be compared based on their “value”, “characters case”, “length”, or “alphabetically” order: To compare strings based on their values and characters case, use the “Strict Equality Operator (===)”.


2 Answers

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.

like image 147
Alain Avatar answered Oct 23 '22 14:10

Alain


You can use Levenshtein distance.

like image 29
Sandro Avatar answered Oct 23 '22 14:10

Sandro