首先介绍一下这个网址非常有用本文所有的关于Anjs起源来自这里请先查看一下
在本次测试使用的是
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.ansj.splitWord.analysis.BaseAnalysis;
import org.ansj.splitWord.analysis.DicAnalysis;
import org.ansj.splitWord.analysis.IndexAnalysis;
import org.ansj.splitWord.analysis.NlpAnalysis;
import org.ansj.splitWord.analysis.ToAnalysis;
import org.ansj.app.keyword.KeyWordComputer;
import org.ansj.app.keyword.Keyword;
import org.apache.hadoop.io.Text;
import com.sun.javafx.collections.MapAdapterChange;
public static void main(String[] args) {
// 分词
String str = "洁面仪配合洁面深层清洁毛孔 清洁鼻孔面膜碎觉使劲挤才能出一点点皱纹 脸颊毛孔修复的看不见啦 草莓鼻历史遗留问题没辙 脸和脖子差不多颜色的皮肤才是健康的 长期使用安全健康的比同龄人显小五到十岁 28岁的妹子看看你们的鱼尾纹" ;
System.out.println(BaseAnalysis.parse(str));
System.out.println(ToAnalysis.parse(str));
System.out.println(DicAnalysis.parse(str));
System.out.println(IndexAnalysis.parse(str));
System.out.println(NlpAnalysis.parse(str));
}
结果
抓取关键词
//抓取关键词 并转换成String类型
public static String zhuaqu(String content){
KeyWordComputer key=new KeyWordComputer(10);
Iterator it = key.computeArticleTfidf(content).iterator() ;
StringBuffer value = new StringBuffer();
while(it.hasNext()) {
Keyword key2=(Keyword)it.next();
value.append(key2.getName()+"="+key2.getFreq()+" ");
}
String out = value.toString();
String OutValue = out.substring(0, out.length()-1);
System.out.println(OutValue);
return OutValue;
}
调用
String tt2 ="咖啡什么味道的比较hao";
String pp = zhuaqu(tt2);
结果
//抓取关键字并返回map
public static HashMap<String, Integer> TestMap(String content){
KeyWordComputer key=new KeyWordComputer(10);
Iterator it = key.computeArticleTfidf(content).iterator() ;
HashMap<String,Integer> valuemap = new HashMap<String,Integer>();
while(it.hasNext()) {
Keyword key2=(Keyword)it.next();
valuemap.put(key2.getName(), key2.getFreq());
}
return valuemap;
}
结果
以下两个放法仅做参考自己看情况使用切分
//字符串转换成map
public static HashMap<String, Integer> StringToMap(String value){
String[] cc = value.toString().split(" ");
String[] bb =null;
HashMap<String, Integer> tmpMap = new HashMap<String, Integer>();
for (int i = 0; i < cc.length; i++) {
bb = cc[i].toString().split("=");
tmpMap.put(bb[0], Integer.parseInt(bb[1]));
bb=null;
}
return tmpMap;
}
//map转换成字符串
private String MapToString(HashMap<String, Integer> map1) {
// TODO Auto-generated method stub
StringBuffer OutValue = new StringBuffer();
String outString = "";
for (Entry<String, Integer> entry : map1.entrySet()) {
OutValue.append(entry.getKey() + " " + entry.getValue());
}
outString = OutValue.toString();
return outString;
}
本文章有很多不足之处,希望大家多多指正。欢迎大家转载,请注明出处!码农打字不易,敬请谅解,谢谢!