因需求,现需分词接口,故记录之。

1、需要依赖:

 <!-- https://mvnrepository.com/artifact/com.janeluo/ikanalyzer -->
<dependency>
<groupId>com.janeluo</groupId>
<artifactId>ikanalyzer</artifactId>
<version>2012_u6</version>
</dependency>

maven依赖

2、完整代码如下:

 public JSONArray entropy(String content, Integer quantity) throws Exception {
List<String> words = extract(DelHtmlTagUtil.delHTMLTag(content), quantity);
JSONArray array = calculateWordEntropy(words);
return array;
} /**
* 传入String类型的文章,智能提取单词放入list
*
* @param content 传入分词的内容
* @param quantity 截取关键字在几个单词以上的数量,默认为1
* @return
*/
private List<String> extract(String content, Integer quantity) throws IOException {
List<String> list = Lists.newArrayList();
StringReader reader = new StringReader(content);
IKSegmenter ik = new IKSegmenter(reader, true);
Lexeme lex = null;
while ((lex = ik.next()) != null) {
//String typeString = lex.getLexemeTypeString(); 词语类型
String word = lex.getLexemeText();
if (word.length() > quantity) {//判断截取关键字在几个单词以上的数量
list.add(word);
}
}
return list;
} private JSONArray calculateWordEntropy(List<String> words) throws Exception{ int length = words.size();
ArrayList<String[]> wordList = new ArrayList<String[]>();
// 将分好的词每3个一组存到数组中
for (int i = 0; i < length; i++) { String[] wordSeg = new String[3];
if (i == 0) {
wordSeg[0] = "null";
wordSeg[1] = words.get(i);
wordSeg[2] = words.get(i + 1);
} else if (i == length - 1) {
wordSeg[0] = words.get(i - 1);
wordSeg[1] = words.get(i);
wordSeg[2] = "null";
} else {
wordSeg[0] = words.get(i - 1);
wordSeg[1] = words.get(i);
wordSeg[2] = words.get(i + 1);
} wordList.add(wordSeg); }
// 去除重复的词
List<String> lists = Lists.newArrayList();
for (int l = 0; l < length; l++) {
lists.add(words.get(l));
}
List<String> tempList = Lists.newArrayList();
for (String str : lists) {
if (!(tempList.contains(str))) {
tempList.add(str);
}
}
String[] wordClean = new String[tempList.size()];
for (int m = 0; m < tempList.size(); m++) {
wordClean[m] = tempList.get(m);
}
// 统计每个词的词频
int[] frequent = new int[wordClean.length];
for (int j = 0; j < wordClean.length; j++) {
int count = 0;
for (int k = 0; k < words.size(); k++) {
if (wordClean[j].equals(words.get(k))) {
count++;
}
}
frequent[j] = count;
}
// 将三元组中中间的那个词相同的存到一个list中,然后计算该词的信息熵
double[] allEntropy = new double[wordClean.length];
for (int n = 0; n < wordClean.length; n++) {
ArrayList<String[]> wordSegList = new ArrayList<String[]>();
int count = 1;
for (int p = 0; p < wordList.size(); p++) {
String[] wordSegStr = wordList.get(p);
if (wordSegStr[1].equals(wordClean[n])) {
count++;
wordSegList.add(wordSegStr);
}
}
String[] leftword = new String[wordSegList.size()];
String[] rightword = new String[wordSegList.size()];
// 计算左信息熵
for (int i = 0; i < wordSegList.size(); i++) {
String[] left = wordSegList.get(i);
leftword[i] = left[0];
}
// 去除左边重复的词
List<String> listsLeft = new ArrayList<String>();
for (int l = 0; l < leftword.length; l++) {
listsLeft.add(leftword[l]);
}
List<String> tempListLeft = new ArrayList<String>();
for (String str : listsLeft) {
if (!(tempListLeft.contains(str))) {
tempListLeft.add(str);
}
}
String[] leftWordClean = new String[tempListLeft.size()];
for (int m = 0; m < tempListLeft.size(); m++) {
leftWordClean[m] = tempListLeft.get(m);
}
// 统计左边每个词的词频
int[] leftFrequent = new int[leftWordClean.length];
for (int j = 0; j < leftWordClean.length; j++) {
int leftcount = 0;
for (int k = 0; k < leftword.length; k++) {
if (leftWordClean[j].equals(leftword[k])) {
leftcount++;
}
}
leftFrequent[j] = leftcount;
}
// 计算左熵值
double leftEntropy = 0;
for (int i = 0; i < leftFrequent.length; i++) {
double a = (double) leftFrequent[i] / count;
double b = Math.log((double) leftFrequent[i] / count);
leftEntropy += -a * b;
// leftEntropy +=
// (-(double)(leftFrequent[i]/count))*Math.log((double)(leftFrequent[i]/count));
}
// 计算右信息熵
for (int i = 0; i < wordSegList.size(); i++) {
String[] right = wordSegList.get(i);
rightword[i] = right[2];
}
// 去除右边重复的词
List<String> listsRight = new ArrayList<String>();
for (int l = 0; l < rightword.length; l++) {
listsRight.add(rightword[l]);
}
List<String> tempListRight = new ArrayList<String>();
for (String str : listsRight) {
if (!(tempListRight.contains(str))) {
tempListRight.add(str);
}
}
String[] rightWordClean = new String[tempListRight.size()];
for (int m = 0; m < tempListRight.size(); m++) {
rightWordClean[m] = tempListRight.get(m);
}
// 统计右边每个词的词频
int[] rightFrequent = new int[rightWordClean.length];
for (int j = 0; j < rightWordClean.length; j++) {
int rightcount = 0;
for (int k = 0; k < rightword.length; k++) {
if (rightWordClean[j].equals(rightword[k])) {
rightcount++;
}
}
rightFrequent[j] = rightcount;
}
// 计算右熵值
double rightEntropy = 0.0;
for (int i = 0; i < rightFrequent.length; i++) {
double a = (double) rightFrequent[i] / count;
double b = Math.log((double) rightFrequent[i] / count);
rightEntropy += -a * b;
// rightEntropy +=
// (-(double)(rightFrequent[i]/count))*Math.log((double)(rightFrequent[i]/count));
}
// 计算词的总信息熵
double wordEntropy = leftEntropy + rightEntropy;
allEntropy[n] = wordEntropy; }
JSONArray list = new JSONArray();
for (int i = 0; i < allEntropy.length; i++) {
JSONObject obj = new JSONObject();
obj.put("name", wordClean[i]);
obj.put("entropy", allEntropy[i]);
list.add(obj);
}
Collections.sort(list, (o1, o2) -> {
Double d1 = ((JSONObject) o1).getDouble("entropy");
Double d2 = ((JSONObject) o2).getDouble("entropy");
return d2.compareTo(d1);
}); return list;
}

处理代理

ikanalyzer分词,计算信息熵排序分词结果的更多相关文章

  1. python 分词计算文档TF-IDF值并排序

    文章来自于我的个人博客:python 分词计算文档TF-IDF值并排序 该程序实现的功能是:首先读取一些文档,然后通过jieba来分词,将分词存入文件,然后通过sklearn计算每一个分词文档中的tf ...

  2. IKAnalyzer结合Lucene实现中文分词

    1.基本介绍 随着分词在信息检索领域应用的越来越广泛,分词这门技术对大家并不陌生.对于英文分词处理相对简单,经过拆分单词.排斥停止词.提取词干的过程基本就能实现英文分词,单对于中文分词而言,由于语义的 ...

  3. php 分词 —— PHPAnalysis无组件分词系统

    分词,顾名思义就是把词语分开,从哪里分开?当然是一大堆词语里了,一大堆词语是什么?是废话或者名言.这在数据库搜索时非常有用. 官方网站 http://www.phpbone.com/phpanalys ...

  4. 自然语言处理之中文分词器-jieba分词器详解及python实战

    (转https://blog.csdn.net/gzmfxy/article/details/78994396) 中文分词是中文文本处理的一个基础步骤,也是中文人机自然语言交互的基础模块,在进行中文自 ...

  5. 利用IK分词器,自定义分词规则

    IK分词源码下载地址:https://code.google.com/p/ik-analyzer/downloads/list lucene源码下载地址:http://www.eu.apache.or ...

  6. Python 结巴分词(1)分词

    利用结巴分词来进行词频的统计,并输出到文件中. 结巴分词github地址:结巴分词 结巴分词的特点: 支持三种分词模式: 精确模式,试图将句子最精确地切开,适合文本分析: 全模式,把句子中所有的可以成 ...

  7. 【Lucene3.6.2入门系列】第05节_自定义停用词分词器和同义词分词器

    首先是用于显示分词信息的HelloCustomAnalyzer.java package com.jadyer.lucene; import java.io.IOException; import j ...

  8. Lucene学习-深入Lucene分词器,TokenStream获取分词详细信息

    Lucene学习-深入Lucene分词器,TokenStream获取分词详细信息 在此回复牛妞的关于程序中分词器的问题,其实可以直接很简单的在词库中配置就好了,Lucene中分词的所有信息我们都可以从 ...

  9. 盘古分词demo,盘古分词怎么用

    1.下载PanGu.dll dll地址:http://download.csdn.net/detail/dhfekl/7493687 2.将PanGu.dll和词库引入到项目 最新词库地址:http: ...

随机推荐

  1. OpencvSharp 在WPF的Image控件中显示图像

    1.安装OpencvSharp 我使用的是VS2013 社区版,安装OpencvSharp3.0 在线安装方法:进入Tools,打开NuGet的包管理器 搜索Opencv 安装之后就可以使用,无需再做 ...

  2. .NET中的FileUpload控件的使用-Jquery(一)

    FileUpload在HTML中是个常用的基础控件,在涉及到上传各种格式的文件时候都会用到:笔者前段时间正好用到它做上传功能,记录下来做一些累积, 前端到后台用的是的Jquery中的Ajax进行数据传 ...

  3. Win10远程桌面出现 身份验证错误,要求的函数不受支持,这可能是由于CredSSP加密Oracle修正 解决方法

    升级至win10 最新版本10.0.17134,远程桌面连接Window Server时报错信息如下: 出现身份验证错误,要求的函数不正确,这可能是由于CredSSP加密Oracle修正. 解决方法: ...

  4. 过滤器中获取form表单或url请求数据

    var httpFormData = filterContext.HttpContext.Request.Form; var logContent = string.Empty; //获取url的 l ...

  5. java环境的配置与安装(windows、macos、linux)

    一.windows下: 1.下载jdk:https://www.oracle.com2.安装教程:https://www.cnblogs.com/zlslch/p/5658399.html 二.mac ...

  6. centos7下elasticSearch安装配置

    OS:Centos7x虚拟机 1H2Gjdk:1.8elasticsearch:5.6.0 1.下载“elasticsearch-5.6.0.tar.gz”解压到/usr/local/elastics ...

  7. WebDriver高级应用实例(9)

    9.1封装操作表格的公用类 目的:能够使自己编写操作表格的公用类,并基于公用类进行表格中的元素的各类操作 被测网页的网址的HTML代码: <html> <body> <t ...

  8. POJ 2491

    #include<iostream>#include<stdio.h>#include<string>#define MAXN 400using namespace ...

  9. Vue2.5开发去哪儿网App 第五章笔记 下

    1. 多个元素或组件的过渡 多个元素的过渡: <style> .v-enter,.v-leace-to{ opacity: 0; } .v-enter-active,.v-leave-ac ...

  10. Kotlin 语言作为Android新的官方语言

    苹果用 swift 作为官方语言替代 object-c  ; 谷歌 刚开完io大会 也宣布 用  kotlin 作为官方语言 替代java.工具还是xcode ,android studio. 来自于 ...