简体   繁体   English

相同的字母

[英]Letters having the same equivalence

Looking ideas on how to go about accomplishing this. 寻找有关如何实现此目标的想法。 Basically I want certain characters to have equivalence. 基本上,我希望某些字符具有等效性。

For example: M = N 例如:M = N

So: Mum = Nun 所以:妈妈=修女

However: Mum can also equal Num. 但是:Mum也可以等于Num。

I was advised to try a map of replacements and this worked until the third example where not all M's are to be changed to N's. 建议我尝试一张替换图,直到第三个示例中所有M都不要更改为N为止,这才起作用。

Thanks 谢谢

This is the code for the map of replacements: 这是替换地图的代码:

HashMap<String,String> replacements = new HashMap<>();
                replacements.put("n","m");
                replacements.put("m","n");

                String ignoreFirstChar = names[j].charAt(0) + (names[j].substring(1,names[j].length()).replaceAll("[^a-zA-Z]+", "").toLowerCase());

                String result = "";
                for(int i1 = 0; i1 < ignoreFirstChar.length(); i1++) {
                    String rpl = replacements.get(ignoreFirstChar.charAt(i1)+"");
                    result += rpl==null?ignoreFirstChar.charAt(i1):rpl;
                }


                System.out.println(ignoreFirstChar);
                System.out.println(result);

I assume M and m are not equivalent. 我假设M和m不相等。 Therefore, if M = N, we cannot say M = n. 因此,如果M = N,则不能说M = n。 If you would like to use a "map of replacements" as you have been suggested, I would use it for the purpose of normalizing your strings. 如果您想按照建议使用“替换图”,我将使用它来规范化您的字符串。

You would take the current problem of 您将面临当前的问题

Given strings x and y, determine whether x equals y

and change it to 并将其更改为

Given strings x and y, determine whether normalize(x) equals normalize(y)

The purpose of normalizing your strings is to apply any equivalence rules that you have, such as M = N. That way "Mum" would be converted to "Num", and then you can compare the two strings without having to worry about the rules because they've already been applied. 规范化字符串的目的是应用您具有的所有等效规则,例如M =N。这样,“ Mum”将被转换为“ Num”,然后您可以比较两个字符串而不必担心规则因为它们已经被应用了。

The normalize method would look something like normalize方法看起来像

/* 
 * Takes each character in inStr and replaces them as necessary based on
 * your replacement map. For example, if you see an "n", then replace it with "m"
 */
String normalize(String inStr) {
   String newStr;

   // something happens

   return newStr;
}

If case-sensitivity is not important, then you would again normalize your strings by first converting them to lower-case or upper-case (doesn't matter, as long as it is consistent) 如果区分大小写不重要,则可以通过将字符串首先转换为小写或大写来再次规范化字符串(没关系,只要一致即可)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM