2014-12-26 3 views
0

두 파일 File1.txt와 File2.txt가 있습니다. 두 파일 모두 텍스트를 포함합니다. 이 파일에있는 일반적인 단어의 총 수를 알고 싶습니다. 이 코드를 사용하여 각 파일의 총 단어 수를 얻었습니다.두 텍스트 파일의 일치하는 단어의 총 수는 어떻게 얻습니까?

public int get_Total_Number_Of_Words(File file) { 
    try { 
     Scanner sc = new Scanner(new FileInputStream(file)); 
     int count = 0; 
     while (sc.hasNext()) { 
      sc.next(); 
      count++; 
     } 
     return count; 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return 0; 
} 

이 코드를 사용하여 두 파일 사이의 공통 단어를 계산하는 방법을 알려주십시오.

+0

텍스트 파일의 크기는 얼마나됩니까? – fge

+0

길이가 다를 수 있습니다. –

+0

그리고 구두점 등은 어떨까요? – fge

답변

0

당신은 어떤 종류의 비교가 있어야합니다. 따라서 중첩 된 루프를 사용하여이를 수행 할 수 있습니다.

String word1, word2; 
int numCommon = 0; 
try { 
    Scanner sc = new Scanner(new FileInputStream(file)); 
    Scanner sc2 = new Scanner(new FileInputStream(file2)); 
    while (sc.hasNext()) { 
     word1 = sc.next(); 
     while(sc2.hasNext()){ 
      word2 = sc2.next(); 
      if(word2.equals(word1)) 
       numCommon++; 
     } 
    } 
    return numCommon; 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
return 0; 
1

지도 구현을 사용하십시오. 단어를 키로, 정수는 키를 찾을 때마다 증가하는 값으로 사용하십시오. 빌라!

public static void main(String[] args) { 
    String[] wordList = new String[]{"test1","test2","test1","test3","test1", "test2", "test4"}; 
    Map<String, Integer> countMap = new HashMap<String, Integer>(); 
    for (String word : wordList) { 
     if (countMap.get(word)==null) { 
      countMap.put(word, 1); 
     } 
     else { 
      countMap.put(word, countMap.get(word)+1); 
     } 
    } 
    System.out.println(countMap); 

} 

결과는 다음과 같습니다

{test4=1, test2=2, test3=1, test1=3} 
+0

예를 통해 친절하게 설명합니다. –

+0

또한 줄을 단어로 나눌 필요가 있습니다. 다음과 같이 사용할 수 있습니다 :'String line = sc.next(); \t String [] words = line.split ("[\\. \\ s]");'. – Wickoo

0

나는 다음 두 가지를 비교,이 목록을 작성하고 하나 개의 목록에 하나 TEXTFILE에서 단어를 추가하고 다른 목록에 다른 텍스트 파일에서 단어를 추가 할 것 똑같은 단어를 세 어라.

1

여기에 자바 8 a project of mine를 사용하여 솔루션입니다 :

당신도 Set의 동시 구현을 사용하도록 선택한 경우이 병렬 처리 될 수
private static final Pattern WORDS = Pattern.compile("\\s+"); 

final LargeTextFactory factory = LargeTextFactory.defaultFactory(); 

final Path file1 = Paths.get("pathtofirstfile"); 
final Path file2 = Paths.get("pathtosecondfile"); 

final List<String> commonWords; 

try (
    final LargeText t1 = factory.fromPath(file1); 
    final LargeText t2 = factory.fromPath(file2); 
) { 
    final Set<String> seen = new HashSet<>(); 

    final Stream<String> all 
     = Stream.concat(WORDS.splitAsStream(t1), WORDS.splitAsStream(t2)); 

    commonWords = all.filter(s -> { return !seen.add(s); }) 
     .collect(Collectors.toList()); 
} 

// commonWords contains what you want 

.

+0

존경하는 선생님 나는 단순화 된 해결책이 필요했다. 그러나 감사합니다. –

관련 문제