首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

应用Lucene简单生成摘要

2013-04-26 
使用Lucene简单生成摘要public static String luceneSummary(String txt) throws ParseException, IOExcep

使用Lucene简单生成摘要
public static String luceneSummary(String txt) throws ParseException, IOException, InvalidTokenOffsetsException{String pQuery= "穆 沙拉 法院"; //关键字Formatter formatter = new SimpleHTMLFormatter("<font color='red'>","</font>"); //高亮 Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);QueryParser parser = new QueryParser(Version.LUCENE_30, "", analyzer); Highlighter highlighter = new Highlighter(formatter , new QueryScorer(parser.parse(pQuery)));// Fragmenter fragmenter = new SimpleFragmenter(100); // highlighter.setTextFragmenter(fragmenter); highlighter.setTextFragmenter(new NullFragmenter()); //不要限制子多少 String[] strArray = txt.split("([。|,])"); //以逗号分隔传入的文章,逐句的提取摘要,这样的摘要最起码是一个句子 String text = ""; for(String str : strArray){ //循环每句话 String ret = highlighter.getBestFragment(analyzer, "", str); if(ret!=null){ text += ret+","; if(text.length()>300) //如果摘要累计300个字就停止 break; } }return text;} public static void main(String[] args) throws IOException, ParseException, InvalidTokenOffsetsException { //args[0]传一篇文章试试就知道了 luceneSummary(args[0]);}

?

热点排行