如何从字符串中找到匹配的单词,以及百分比值?

How to find matched words from strings, and the percentage value?

我有主弦

String main = "hi how are you? i am fine.";

然后是几个字符串..

String string1 = "hi people what is the need................ how to ................... do the needful.................................. and you can only need the change......................................... in the fine situation................................................. to do the task................... i am alright............... are you okay?";

(大概100字左右)

String string2 = "how to do...................i don't know the need......... what i am supposed to do......................fine i am.......... okay..";

(大概100字左右)

String string3 = "some string again................";

(大概100字左右)

String string4 = "some string again................";

(大概100字左右)

现在,我要做的是,在 main 字符串中有 7 个单词..

如您所见,所有这 7 个词都出现在 string1.. 4 个词出现在 string2.. 并继续..

所以,现在 string1 的百分比值为 100%。 对于 string2,百分比值为 57.xx%.. 依此类推..

所以,我想以编程方式获取这些百分比值..

到目前为止我尝试过的是,

String perc;

String[] q1 = str1.split(" ");
String[] q2 = main.split(" ");

for (String temp1: q1) {
    for (String temp2: q2) {
        if(temp1.equals(temp2)) {
            // Some code here
        }
    }
}

现在我不知道从哪里开始?

这里是如何完成的:

String main = "hi how are you? i am fine.";
// Extract all the words whose length is > 1 and remove duplicates
Set<String> mainWords = new HashSet<>();
for (String s : main.split("\W")) {
    if (s.length() > 1) {
        mainWords.add(s);
    }
}
String string1 = "hi people what is the need................ how to ................... do the needful.................................. and you can only need the change......................................... in the fine situation................................................. to do the task................... i am alright............... are you okay?";

Set<String> mainWordsToFind = new HashSet<>(mainWords);
// Iterate over all the words and remove the word from the list to find
for (String word : string1.split("\W")) {
    if (word.length() > 1) {
        mainWordsToFind.remove(word);
    }
}

// Print the percent of word found
System.out.println((double) (mainWords.size() - mainWordsToFind.size()) / mainWords.size());

输出:

1.0

试试这个

    String string1 = "hi people what is the need how to  do the needful and you can only need the changein the fine situation to do the task i am alright are you okay?";


    String string2 = "how to do i don't know the need what i am supposed to do fine i am okay..";

    int count = 0;
    String[] array = string1.split(" ");
    ArrayList<String> processedWords = new ArrayList<>();
    for(String str : array){

        if (string2.contains(str) && !processedWords.contains(str) && str.length() > 0){
                System.out.println(str);

            processedWords.add(str);
            count++;
        }

    }
    System.out.println("Total words: " +array.length);
    System.out.println("Count: " +count);

    System.out.println("Percent value: " +((float)count/array.length)*100);