java 如何检查两个字符串是否近似相等?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10074298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if two Strings are approximately equal?
提问by Shaun Wild
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.
如果有人输入:“强度等级?” 它会做一个功能..然后如果其他人输入:“Str level?” 它会做同样的功能,但我想要它,这样如果有人打错字或类似的东西,它会自动检测他们正在尝试输入的内容,例如:“强度 tlevel?” 也会使函数被调用。
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?
如何检查两个字符串是否彼此相似?
回答by Alain
See this question and answer: Getting the closest string match
请参阅此问答:获取最接近的字符串匹配
Using some heuristics and the Levenshtein distancealgorithm, you can compute the similarity of two strings and take a guess at whether they're equal.
使用一些启发式方法和Levenshtein 距离算法,您可以计算两个字符串的相似度并猜测它们是否相等。
Your only option other than that would be a dictionary of accepted words similar to the one you're looking for.
除此之外,您唯一的选择将是一本包含与您正在寻找的词相似的已接受词的词典。
回答by Sandro
You can use Levenshtein distance.
您可以使用Levenshtein distance。
回答by birdy
I believe you should use one of Edit distance algorithmsto solve your problem. Here is for example Levenstein distancealgorithm 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.
我相信您应该使用编辑距离算法之一来解决您的问题。例如,这里是Java 中的Levenstein 距离算法实现。您可以使用它来比较句子中的单词,如果它们的编辑距离总和小于例如句子长度的 10%,则认为它们相等。
回答by Alberto
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).
如果要查找相似的单词开头,可以使用词干分析器。Stemmers 将单词简化为一个共同的开头。最著名的算法是 Port Stemmer ( http://tartarus.org/~martin/PorterStemmer)。
Levenshtein, as pointed above, is great, but computational heavy for distances greater than one or two.
如上所述,Levenshtein 很棒,但对于大于一两个的距离,计算量很大。
回答by ericosg
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".
这对于自定义单词很有用,因此您可以在与“强度”相同的键中添加“str”。
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.
但是,您也可以使用一些自动化方法,即当您的单词在字典中找不到时,递归循环 1 个字母差异(丢失或替换)并且可以递归到更深层次,即 2 个丢失的字母等。
回答by Olivier Refalo
I found a few projects that do text to phonemes translations, don't know which one is best
找了几个做文本转音素翻译的项目,不知道哪个最好