后台服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.6 KiB

  1. package com.iformall.language;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.cybozu.labs.langdetect.Detector;
  4. import com.cybozu.labs.langdetect.DetectorFactory;
  5. import com.cybozu.labs.langdetect.LangDetectException;
  6. import com.google.common.io.ByteStreams;
  7. import lombok.extern.slf4j.Slf4j;
  8. import lombok.var;
  9. import org.springframework.core.io.Resource;
  10. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  11. import org.springframework.util.StreamUtils;
  12. import sun.misc.IOUtils;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.nio.charset.StandardCharsets;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. @Slf4j
  19. public class LanguageDetect {
  20. private static Detector detect = null;
  21. private static void initDetector(){
  22. if(detect == null){
  23. try {
  24. List<String> profile = new ArrayList<>();
  25. try {
  26. PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
  27. Resource[] resources = resolver.getResources("classpath:profiles/*");
  28. for (Resource resource:resources) {
  29. InputStream inputStream = resource.getInputStream();
  30. String str = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
  31. // String str = new String(ByteStreams.toByteArray(inputStream));
  32. profile.add(str);
  33. inputStream.close();
  34. }
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. log.error("查看初始化数据--"+ JSONObject.toJSONString(profile));
  39. if(!profile.isEmpty()){
  40. DetectorFactory.loadProfile(profile);
  41. detect = DetectorFactory.create();
  42. }
  43. } catch (LangDetectException e) {
  44. e.printStackTrace();
  45. log.error("语言检测初始化错误。"+e.getMessage());
  46. }
  47. }
  48. }
  49. public static String detect(String str){
  50. if(detect == null){
  51. initDetector();
  52. }
  53. if(detect == null){
  54. return null;
  55. }
  56. detect.append(str);
  57. // try {
  58. // detect.getProbabilities();
  59. // } catch (LangDetectException e) {
  60. // e.printStackTrace();
  61. // }
  62. try {
  63. String language = detect.detect();
  64. log.info("语种识别(" + str + ")-----" + language);
  65. return language;
  66. } catch (LangDetectException e) {
  67. e.printStackTrace();
  68. log.error("语言检测错误。");
  69. }
  70. return null;
  71. }
  72. public static void main(String[] args) {
  73. // var resolver = new PathMatchingResourcePatternResolver();
  74. // Resource[] resources = new Resource[0];
  75. // try {
  76. // resources = resolver.getResources("classpath:profiles/*");
  77. // } catch (IOException e) {
  78. // e.printStackTrace();
  79. // }
  80. // for (var resource:resources) {
  81. // System.out.println(resource.getFilename());
  82. // try {
  83. // byte[] bytes = new byte[0];
  84. // bytes = new byte[resource.getInputStream().available()];
  85. // resource.getInputStream().read(bytes);
  86. // String str = new String(bytes);
  87. // System.out.println(str);
  88. // } catch (IOException e) {
  89. // e.printStackTrace();
  90. // }
  91. //
  92. // }
  93. System.out.println(detect("aa"));
  94. }
  95. }