后台服务
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.

105 lines
3.4 KiB

  1. package com.iformall.language;
  2. import com.cybozu.labs.langdetect.Detector;
  3. import com.cybozu.labs.langdetect.DetectorFactory;
  4. import com.cybozu.labs.langdetect.LangDetectException;
  5. import lombok.extern.slf4j.Slf4j;
  6. import lombok.var;
  7. import org.springframework.core.io.Resource;
  8. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  9. import sun.misc.IOUtils;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. @Slf4j
  15. public class LanguageDetect {
  16. private static Detector detect = null;
  17. private static void initDetector(){
  18. if(detect == null){
  19. try {
  20. List<String> profile = new ArrayList<>();
  21. try {
  22. PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
  23. Resource[] resources = resolver.getResources("classpath:profiles/*");
  24. for (Resource resource:resources) {
  25. try {
  26. byte[] bytes = new byte[0];
  27. bytes = new byte[resource.getInputStream().available()];
  28. resource.getInputStream().read(bytes);
  29. String str = new String(bytes);
  30. profile.add(str);
  31. log.info(str);
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  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. return detect.detect();
  64. } catch (LangDetectException e) {
  65. e.printStackTrace();
  66. log.error("语言检测错误。");
  67. }
  68. return null;
  69. }
  70. public static void main(String[] args) {
  71. // var resolver = new PathMatchingResourcePatternResolver();
  72. // Resource[] resources = new Resource[0];
  73. // try {
  74. // resources = resolver.getResources("classpath:profiles/*");
  75. // } catch (IOException e) {
  76. // e.printStackTrace();
  77. // }
  78. // for (var resource:resources) {
  79. // System.out.println(resource.getFilename());
  80. // try {
  81. // byte[] bytes = new byte[0];
  82. // bytes = new byte[resource.getInputStream().available()];
  83. // resource.getInputStream().read(bytes);
  84. // String str = new String(bytes);
  85. // System.out.println(str);
  86. // } catch (IOException e) {
  87. // e.printStackTrace();
  88. // }
  89. //
  90. // }
  91. System.out.println(detect("aa"));
  92. }
  93. }