2016-12-07 1 views
1

두 단어가 많은 두 개의 문자열이 있습니다.두 텍스트 파일 간의 단어 일치 비율 알고리즘

내 임무는 두 문자열 사이의 단어 일치 비율을 찾는 것입니다. 누군가 나에게 정확한 백분율/일치 단어를 얻기 위해 이미 알고리즘을 제안 해 줄 수 있습니까?

예 : 아래와 같은 방법을 사용할 수 있습니다

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. 
+0

어떻게 복제본을 처리 하시겠습니까? 두 개의 예제 문자열에서'fish '가 두 번 나타난 경우 어떻게 카운트에 영향을 줍니까? –

+0

문장을'tokenize'하고 나서'contains()'같은 것을 사용하여 단어를 diff합니까? 너 무슨 .. 시도 했어? –

+1

"이미 가지고있는"알고리즘이란 무엇입니까? – nicovank

답변

2

. 내가 시도 할 수있는 각각의 단계를 기술하기 위해 인라인 주석을 추가했다. 이 코드 예제에서는 공백 문자를 사용하여 단어를 분할했습니다. 우려 사항이 있으면 의견을 추가 할 수 있습니다.

일치 단어 은 대소 문자를 무시합니다. 그렇지 않으면 주어진 예제에서 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; 
} 
+0

고마워요! 그것은 나를 위해 일했다. –

+0

내 컴파일 시간을 줄이기 위해 inbuilt 함수를 제안 해 줄 수 있습니까? –