You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.4 KiB
Java
72 lines
2.4 KiB
Java
package com.ruoyi.biemo.nlp;
|
|
|
|
import com.hankcs.hanlp.classification.classifiers.IClassifier;
|
|
import com.hankcs.hanlp.classification.classifiers.NaiveBayesClassifier;
|
|
import com.ruoyi.biemo.utils.TestUtility;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* 情感分析
|
|
*/
|
|
public class SentimentAnalysisUtils {
|
|
public static final String CORPUS_FOLDER = TestUtility.ensureTestData("ChnSentiCorp情感分析酒店评论", "http://hanlp.linrunsoft.com/release/corpus/ChnSentiCorp.zip");
|
|
public static String analysis(String text){
|
|
String result = "";
|
|
IClassifier classifier = new NaiveBayesClassifier();
|
|
try {
|
|
classifier.train(CORPUS_FOLDER);
|
|
result = classifier.classify(text);
|
|
} catch (IOException ioException) {
|
|
ioException.printStackTrace();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static Object analysisImport(MultipartFile[] files, String name) {
|
|
if(files!=null&&files.length>0){
|
|
File file1 = new File(CORPUS_FOLDER+"/"+name);
|
|
if(!file1.getParentFile().exists()){
|
|
file1.mkdirs();
|
|
}
|
|
if(!file1.exists()){
|
|
file1.mkdirs();
|
|
}
|
|
for(int i=0;i<files.length;i++){
|
|
MultipartFile file = files[i];
|
|
try {
|
|
File file2 = new File(file1.getCanonicalPath()+"/"+ file.getOriginalFilename());
|
|
file.transferTo(file2);
|
|
} catch (IOException ioException) {
|
|
ioException.printStackTrace();
|
|
}
|
|
}
|
|
IClassifier classifier = new NaiveBayesClassifier();
|
|
try {
|
|
classifier.train(CORPUS_FOLDER);
|
|
} catch (IOException ioException) {
|
|
ioException.printStackTrace();
|
|
}
|
|
}
|
|
return "ok";
|
|
}
|
|
|
|
public static List<String> getCurrSentiments(){
|
|
File file = new File(CORPUS_FOLDER);
|
|
File[] dirs = file.listFiles();
|
|
List<String> result = new ArrayList<>();
|
|
if(dirs!=null&&dirs.length>0){
|
|
for(int i=0;i<dirs.length;i++){
|
|
if(dirs[i].isDirectory()){
|
|
result.add(dirs[i].getName());
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
}
|