com.github.houbb.auto.summary.api.impl.AutoSummaryWeight Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of auto-summary Show documentation
Show all versions of auto-summary Show documentation
The auto-summary tool for java.
package com.github.houbb.auto.summary.api.impl;
import com.github.houbb.auto.summary.api.IAutoSummaryContext;
import com.github.houbb.auto.summary.api.IAutoSummaryResult;
import com.github.houbb.heaven.annotation.ThreadSafe;
import com.github.houbb.heaven.support.handler.IHandler;
import com.github.houbb.heaven.support.handler.IMapHandler;
import com.github.houbb.heaven.support.tuple.impl.Pair;
import com.github.houbb.heaven.util.guava.Guavas;
import com.github.houbb.heaven.util.lang.ObjectUtil;
import com.github.houbb.heaven.util.lang.StringUtil;
import com.github.houbb.heaven.util.util.CollectionUtil;
import com.github.houbb.heaven.util.util.MapUtil;
import com.github.houbb.keyword.api.IKeywordResult;
import com.github.houbb.keyword.api.impl.KeywordResult;
import com.github.houbb.keyword.model.KeywordFreqBean;
import com.github.houbb.keyword.util.KeywordHelper;
import java.util.*;
/**
* 权重
* project: auto-summary-IAutoSummary
* create on 2020/3/12 21:56
*
* @author binbin.hou
* @since 0.0.2
*/
@ThreadSafe
public class AutoSummaryWeight extends AbstractAutoSummary {
@Override
protected List getSummaryList(List sentenceList, List keywordResults,
IAutoSummaryContext context) {
final int limit = context.limit();
final Map freqMap = MapUtil.toMap(keywordResults, new IMapHandler() {
@Override
public String getKey(IKeywordResult o) {
return o.word();
}
@Override
public Double getValue(IKeywordResult o) {
return o.freq();
}
});
//TOP-K 算法
List> pairs = Guavas.newArrayList();
for(String sentence : sentenceList) {
double rank = calcRank(sentence, freqMap);
Pair pair = Pair.of(sentence, rank);
pairs.add(pair);
}
// 排序
Collections.sort(pairs, new Comparator>() {
@Override
public int compare(Pair o1, Pair o2) {
return o1.getValueTwo().compareTo(o2.getValueTwo());
}
});
//获取前几个
return CollectionUtil.toList(pairs.subList(0, limit), new IHandler, IAutoSummaryResult>() {
@Override
public IAutoSummaryResult handle(Pair objects) {
return AutoSummaryResult.newInstance().sentence(objects.getValueOne())
.weight(objects.getValueTwo());
}
});
}
/**
* 计算评分
* @param sentence 语句
* @param freqMap 频率
* @return 结果
* @since 0.0.2
*/
private double calcRank(final String sentence, final Map freqMap) {
List keywords = KeywordHelper.keyword(sentence, Integer.MAX_VALUE);
double result = 0.0;
for(String keyword : keywords) {
Double freq = freqMap.get(keyword);
if(ObjectUtil.isNotNull(freq)) {
result += freq;
}
}
return result;
}
}