[1차] 뉴스 클러스터링

magicdrill·2025년 4월 9일
0

[1차] 뉴스 클러스터링

ArrayList, Map, Set 자료구조를 다양하게 사용한다.

Character.isLetter(temp.charAt(i)) : 해당 문자가 알파벳인지 판단 해서 boolean 값으로 반환

map1.getOrDefault(str, 0) : map에 str이라는 key값이 존재한다면 해당 키값의 value 값 반환, 없으면 0 반환
map1.keySet() : map에 존재하는 모든 key값 반환
map2.containsKey(str) : map에 str이라는 key값이 존재여부 boolean 값으로 반환

unionKeys.addAll(map1.keySet()) : map에 존재하는 모든 key값을 set에 전부 삽입 - 정렬, 중복제거

import java.util.*;

class Solution {
    //문자열이 알파벳으로만 이루어져 있는지?
    public boolean isAlphabet(String temp) {
        int i;    
        
        for (i = 0; i < temp.length(); i++) {
            if (!Character.isLetter(temp.charAt(i))) {
                return false;
            }
        }
        return true;
    }
    
    //자카드 유사도
    public double J(ArrayList<String> list1, ArrayList<String> list2) {
        if (list1.size() == 0 && list2.size() == 0) {
            return 1.0;
        }

        Map<String, Integer> map1 = new HashMap<>();
        Map<String, Integer> map2 = new HashMap<>();
        int intersection = 0; //교집합
        int union = 0; //합집합

        for (String str : list1) {
            map1.put(str, map1.getOrDefault(str, 0) + 1);//str이 map에 있으면 그 값, 없으면 0
        }

        for (String str : list2) {
            map2.put(str, map2.getOrDefault(str, 0) + 1);//str이 map에 있으면 그 값, 없으면 0
        }

        // 교집합
        // Map에 들어 있는 모든 키 값들의 집합(Set) 을 반환
        for (String str : map1.keySet()) {
            if (map2.containsKey(str)) { //해당 키가 map 안에 존재하는지 확인
                System.out.print(str + " ");
                intersection += Math.min(map1.get(str), map2.get(str));
            }
        }
        System.out.println("=> intersection : " + intersection);

        // 합집합
        Set<String> unionKeys = new HashSet<>();
        
        // map1과 map2의 키를 모두 넣고 중복은 제거
        unionKeys.addAll(map1.keySet()); 
        unionKeys.addAll(map2.keySet());

        for (String str : unionKeys) {
            int count1 = map1.getOrDefault(str, 0);//str이 map에 있으면 그 값, 없으면 0
            int count2 = map2.getOrDefault(str, 0);//str이 map에 있으면 그 값, 없으면 0
            union += Math.max(count1, count2); // count1, count2 중 더 큰 값 더하기
            System.out.print(str + " ");
        }
        System.out.println("=> union : " + union);

        return (double) intersection / union;
    }
    
    public int solution(String str1, String str2) {
        int answer = 0;
        int i;
        String temp;
        ArrayList<String> arrayListStr1 = new ArrayList<>();
        ArrayList<String> arrayListStr2 = new ArrayList<>();
        
        //str1을 다중집합으로 변환
        str1 = str1.toLowerCase(); // 전부 소문자로 변환
        for(i = 0; i < str1.length() - 1; i++){ // 다중 집합으로 변환하기
            temp = str1.substring(i, i + 2); // 두 글자씩 분리
            
            if(isAlphabet(temp)){// 알파벳으로만 이루어져 있는지?
                arrayListStr1.add(temp);// 삽입
            }
        }
        // 변환 결과 출력
        for(String str : arrayListStr1){
            System.out.print(str + " ");
        }
        System.out.println();
        
        //str2을 다중집합으로 변환
        str2 = str2.toLowerCase(); // 전부 소문자로 변환
        for(i = 0; i < str2.length() - 1; i++){ // 다중 집합으로 변환하기
            temp = str2.substring(i, i + 2); // 두 글자씩 분리
            
            if(isAlphabet(temp)){// 알파벳으로만 이루어져 있는지?
                arrayListStr2.add(temp);// 삽입
            }
        }
        // 변환 결과 출력
        for(String str : arrayListStr2){
            System.out.print(str + " ");
        }
        System.out.println();
        
        answer = (int)(J(arrayListStr1, arrayListStr2) * 65536);
        
        return answer;
    }
}

0개의 댓글