|
|
@@ -21,50 +21,46 @@ import java.util.List; |
|
|
|
@Slf4j |
|
|
|
public class LanguageDetect { |
|
|
|
|
|
|
|
private static Detector detect = null; |
|
|
|
private static Detector initDetector(){ |
|
|
|
try { |
|
|
|
// DetectorFactory.loadProfile(Thread.currentThread().getContextClassLoader().getResource("profiles").getPath()); |
|
|
|
// return DetectorFactory.create(); |
|
|
|
List<String> profile = new ArrayList<>(); |
|
|
|
|
|
|
|
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(); |
|
|
|
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); |
|
|
|
Resource[] resources = resolver.getResources("classpath:profiles/*"); |
|
|
|
for (Resource resource:resources) { |
|
|
|
InputStream inputStream = resource.getInputStream(); |
|
|
|
byte[] bytes = new byte[0]; |
|
|
|
inputStream.read(bytes); |
|
|
|
profile.add(new String(bytes)); |
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
log.error("查看初始化数据--"+ JSONObject.toJSONString(profile)); |
|
|
|
if(!profile.isEmpty()){ |
|
|
|
DetectorFactory.loadProfile(profile); |
|
|
|
detect = DetectorFactory.create(); |
|
|
|
// String str = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8); |
|
|
|
String str = new String(ByteStreams.toByteArray(inputStream)); |
|
|
|
profile.add(str); |
|
|
|
// InputStream inputStream = resource.getInputStream(); |
|
|
|
// byte[] bytes = new byte[(int) resource.contentLength()]; |
|
|
|
// inputStream.read(bytes); |
|
|
|
// profile.add(new String(bytes)); |
|
|
|
} |
|
|
|
} catch (LangDetectException e) { |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
log.error("语言检测初始化错误。"+e.getMessage()); |
|
|
|
} |
|
|
|
if(!profile.isEmpty()){ |
|
|
|
DetectorFactory.clear(); |
|
|
|
DetectorFactory.loadProfile(profile); |
|
|
|
return DetectorFactory.create(); |
|
|
|
} |
|
|
|
} catch (LangDetectException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
log.error("语言检测初始化错误。"+e.getMessage()); |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
public static String detect(String str){ |
|
|
|
if(detect == null){ |
|
|
|
initDetector(); |
|
|
|
} |
|
|
|
if(detect == null){ |
|
|
|
Detector detector = initDetector(); |
|
|
|
if(detector == null){ |
|
|
|
return null; |
|
|
|
} |
|
|
|
detect.append(str); |
|
|
|
detector.append(str); |
|
|
|
// try { |
|
|
|
// detect.getProbabilities(); |
|
|
|
// } catch (LangDetectException e) { |
|
|
@@ -72,7 +68,7 @@ public class LanguageDetect { |
|
|
|
// } |
|
|
|
|
|
|
|
try { |
|
|
|
String language = detect.detect(); |
|
|
|
String language = detector.detect(); |
|
|
|
log.info("语种识别(" + str + ")-----" + language); |
|
|
|
return language; |
|
|
|
} catch (LangDetectException e) { |
|
|
@@ -104,6 +100,6 @@ public class LanguageDetect { |
|
|
|
// } |
|
|
|
// |
|
|
|
// } |
|
|
|
System.out.println(detect("aa")); |
|
|
|
System.out.println(detect("科技公司")); |
|
|
|
} |
|
|
|
} |