package com.iformall.language; import com.alibaba.fastjson.JSONObject; import com.cybozu.labs.langdetect.Detector; import com.cybozu.labs.langdetect.DetectorFactory; import com.cybozu.labs.langdetect.LangDetectException; import com.google.common.io.ByteStreams; import lombok.extern.slf4j.Slf4j; import lombok.var; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.util.StreamUtils; import sun.misc.IOUtils; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @Slf4j public class LanguageDetect { private static Detector detect = null; private static void initDetector(){ if(detect == null){ try { List<String> profile = new ArrayList<>(); try { PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); Resource[] resources = resolver.getResources("classpath:profiles/*"); for (Resource resource:resources) { InputStream inputStream = resource.getInputStream(); String str = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8); // String str = new String(ByteStreams.toByteArray(inputStream)); profile.add(str); inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } log.error("查看初始化数据--"+ JSONObject.toJSONString(profile)); if(!profile.isEmpty()){ DetectorFactory.loadProfile(profile); detect = DetectorFactory.create(); } } catch (LangDetectException e) { e.printStackTrace(); log.error("语言检测初始化错误。"+e.getMessage()); } } } public static String detect(String str){ if(detect == null){ initDetector(); } if(detect == null){ return null; } detect.append(str); // try { // detect.getProbabilities(); // } catch (LangDetectException e) { // e.printStackTrace(); // } try { String language = detect.detect(); log.info("语种识别(" + str + ")-----" + language); return language; } catch (LangDetectException e) { e.printStackTrace(); log.error("语言检测错误。"); } return null; } public static void main(String[] args) { // var resolver = new PathMatchingResourcePatternResolver(); // Resource[] resources = new Resource[0]; // try { // resources = resolver.getResources("classpath:profiles/*"); // } catch (IOException e) { // e.printStackTrace(); // } // for (var resource:resources) { // System.out.println(resource.getFilename()); // try { // byte[] bytes = new byte[0]; // bytes = new byte[resource.getInputStream().available()]; // resource.getInputStream().read(bytes); // String str = new String(bytes); // System.out.println(str); // } catch (IOException e) { // e.printStackTrace(); // } // // } System.out.println(detect("aa")); } }