package com.example.myapplication;

import java.util.*;

public class ClassificationCoreTest {
    static class Course { long id; String name; Course(long id, String name){this.id=id;this.name=name;} }
    static class Chapter { long id; long subjectId; String name; Chapter(long id,long sid,String name){this.id=id;this.subjectId=sid;this.name=name;} }

    static Set<String> tokens(String text){
        Set<String> t=new HashSet<>();
        if (text==null) return t;
        String[] parts=text.toLowerCase(Locale.ROOT).split("[^\u4e00-\u9fa5a-zA-Z0-9]+");
        for(String p:parts) if(p.length()>1) t.add(p);
        return t;
    }

    static double overlap(Set<String> a,List<String> b){
        if(a.isEmpty()||b==null||b.isEmpty()) return 0.0;
        int hit=0; for(String x:b) if(a.contains(x)) hit++;
        return Math.max(0.0, Math.min(1.0, (double)hit/Math.max(1,b.size())));
    }

    static List<String> tokenize(String text){
        List<String> out=new ArrayList<>();
        if(text==null||text.isEmpty()) return out;
        String[] parts=text.toLowerCase(Locale.ROOT).split("[^\u4e00-\u9fa5a-zA-Z0-9]+");
        for(String p:parts) if(p.length()>1) out.add(p);
        return out;
    }

    public static void main(String[] args){
        List<Course> courses=new ArrayList<>();
        courses.add(new Course(2,"数据结构"));
        courses.add(new Course(4,"操作系统"));
        Map<Long,List<Chapter>> catalog=new HashMap<>();
        catalog.put(2L, Arrays.asList(
                new Chapter(15,2,"Chapter 3 Stack and Queue"),
                new Chapter(17,2,"Chapter 5 Trees and Binary Trees"))
        );
        catalog.put(4L, Arrays.asList(
                new Chapter(41,4,"Chapter 5 Memory Management"),
                new Chapter(43,4,"Chapter 7 IO Systems"))
        );

        String text="reviewed queue and stack operations, practiced linked list";
        Set<String> note=tokens(text);
        double best=0.0; Course bc=null; Chapter ch=null;
        for(Course c:courses){
            for(Chapter x:catalog.getOrDefault(c.id, Collections.emptyList())){
                double s=overlap(note, tokenize(x.name))*0.3 + overlap(note, tokenize(c.name))*0.2;
                if(s>best){best=s; bc=c; ch=x;}
            }
        }
        System.out.println((bc!=null?bc.name:"")+" | "+(ch!=null?ch.name:"")+" | score="+best);
    }
}