first commit
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package com.mhd.gateway.uilts;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 构建敏感词库
|
||||
*/
|
||||
public class SensitiveWordLibrary {
|
||||
|
||||
private Set<String> sensitiveWords;
|
||||
|
||||
public SensitiveWordLibrary() {
|
||||
sensitiveWords = new HashSet<>();
|
||||
}
|
||||
|
||||
public void addSensitiveWord(String word) {
|
||||
sensitiveWords.add(word);
|
||||
}
|
||||
|
||||
public boolean isSensitiveWord(String word) {
|
||||
return sensitiveWords.contains(word);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.mhd.gateway.uilts;
|
||||
|
||||
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* 加载敏感词库
|
||||
*/
|
||||
public class SensitiveWordLoader {
|
||||
|
||||
|
||||
public static void load(SensitiveWordLibrary library) throws IOException {
|
||||
URL resource = SensitiveWordLoader.class.getClassLoader().getResource("disabledWord/politics.txt");
|
||||
if(null == resource){
|
||||
throw new IOException("加载敏感词库失败");
|
||||
}
|
||||
String path = resource.getPath();
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
library.addSensitiveWord(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.mhd.gateway.uilts;
|
||||
|
||||
|
||||
import org.ansj.domain.Result;
|
||||
import org.ansj.splitWord.analysis.NlpAnalysis;
|
||||
|
||||
/**
|
||||
* 待检测文本分词
|
||||
*/
|
||||
public class TextSegmentation {
|
||||
|
||||
public static String[] segment(String text) {
|
||||
Result result = NlpAnalysis.parse(text);
|
||||
return result.getTerms().stream()
|
||||
.map(term -> term.getName())
|
||||
.toArray(String[]::new);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user