Java 两个文本文件之间单词匹配百分比的算法
我有两个字符串,里面有很多单词 我的任务是找到两个字符串之间单词匹配的百分比。有人能告诉我我们已经得到精确百分比/匹配词的算法吗 示例:Java 两个文本文件之间单词匹配百分比的算法,java,algorithm,Java,Algorithm,我有两个字符串,里面有很多单词 我的任务是找到两个字符串之间单词匹配的百分比。有人能告诉我我们已经得到精确百分比/匹配词的算法吗 示例: 1. Mason natural fish oil 1000 mg omega-3 softgels - 200 ea 2. Mason Vitamins Omega 3 Fish Oil, 1000mg. Softgels, Bonus Size 200-Count Bottle **Output** should be 8 words matched b
1. Mason natural fish oil 1000 mg omega-3 softgels - 200 ea
2. Mason Vitamins Omega 3 Fish Oil, 1000mg. Softgels, Bonus Size 200-Count Bottle
**Output** should be 8 words matched between two strings.
您可以使用下面的方法。我添加了内联注释来描述您可以尝试的每个步骤。注意,在这个代码示例中,我使用空格字符分割单词。如果您有任何问题,可以添加评论 请注意,我做了匹配单词忽略大小写,因为在给定的示例中不可能有8个匹配单词
public static int matchStrings(String firstString, String SecondString) {
int matchingCount = 0;
//Getting the whole set of words in to array.
String[] allWords = firstString.split("\\s");
Set<String> firstInputset = new HashSet<String>();
//getting unique words in to set
for (String string : allWords) {
firstInputset.add(string);
}
//Loop through the set and check whether number of words occurrence in second String
for (String string : firstInputset) {
if (SecondString.toLowerCase().contains(string.toLowerCase())) {
matchingCount++;
}
}
return matchingCount;
}
publicstaticintmatchstring(stringfirststring,stringsecondstring){
int matchingCount=0;
//将整套单词放入数组。
String[]allWords=firstString.split(\\s”);
Set firstInputset=new HashSet();
//将独特的单词输入到集合中
for(字符串:allWords){
添加(字符串);
}
//循环遍历集合,并检查第二个字符串中是否出现单词数
for(字符串:firstInputset){
if(SecondString.toLowerCase().contains(string.toLowerCase())){
matchingCount++;
}
}
返回匹配计数;
}
您希望如何处理重复项,例如,如果fish
在两个示例字符串中出现两次,那么计数会受到怎样的影响?首先标记句子,然后使用contains()
或其他方法区分单词?你尝试了什么…?什么是算法“你已经有了”?你是如何为给定的示例找到8个匹配的单词的?@TimBiegeleisen我相信你不会找到重复的。谢谢!你能给我推荐一些内置函数来缩短我的编译时间吗。