`
edwardpro
  • 浏览: 301269 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

lunece 用的高亮类

阅读更多

网上有很多高亮的类,但我觉得太负责了(也许他们觉得这样性能更高),我写了一个很简单采用je分词+正则解决高亮问题,实际使用下来还是不错的:

java 代码
  1. import java.util.Iterator;   
  2. import java.util.List;   
  3.   
  4. /**  
  5.  * @author edwardpro  
  6.  *   
  7.  */  
  8. public class HighlightProcess {   
  9.   
  10.     /**  
  11.      *   
  12.      */  
  13.     private String str;   
  14.   
  15.     private String key;   
  16.   
  17.     private static final String HIGH_LIGHT = ";   
  18.   
  19.     public HighlightProcess(String str, String key) {   
  20.         // TODO Auto-generated constructor stub   
  21.         this.str = str;   
  22.         this.key = key;   
  23.     }   
  24.   
  25.     /**  
  26.      * @return the str  
  27.      */  
  28.     public String getStr() {   
  29.         return str;   
  30.     }   
  31.   
  32.     /**  
  33.      * @param str  
  34.      *            the str to set  
  35.      */  
  36.     public void setStr(String str) {   
  37.         this.str = str;   
  38.     }   
  39.   
  40.     public String getResult() {   
  41.   
  42.         List list = WordsManager.splitStrToList(key);   
  43.         for (Iterator it = list.iterator(); it.hasNext();) {   
  44.             String target = (String) it.next();   
  45.             this.str = RegxTools.regxReplace(this.str, target, HIGH_LIGHT);   
  46.         }   
  47.         return this.str;   
  48.     }   
  49.   
  50. }  
  51. 分词这边使用了一个工厂方法,用来装载词库的,返回的是JE分词对象:

    java 代码
    1. import java.io.File;   
    2. import java.io.FileNotFoundException;   
    3. import java.io.FileReader;   
    4. import java.io.IOException;   
    5. import java.util.ArrayList;   
    6. import java.util.Iterator;   
    7. import java.util.List;   
    8. import java.util.StringTokenizer;   
    9.   
    10. import jeasy.analysis.MMAnalyzer;   
    11.   
    12. import org.apache.log4j.Logger;   
    13.   
    14.   
    15. public class WordsManager {   
    16.   
    17.     private static final Logger logger = Logger.getLogger(WordsManager.class);   
    18.   
    19.     private static MMAnalyzer mmanalyzer;   
    20.   
    21.     private static final String DEF_SPT = "<>";   
    22.   
    23.     static {   
    24.         init();   
    25.     }   
    26.   
    27.     private static void init() {   
    28.         reload();   
    29.         mmanalyzer = new MMAnalyzer();   
    30.     }   
    31.   
    32.     public static void reload() {   
    33.         if (AppInit.getScb().getExtDic() != null  
    34.                 && !AppInit.getScb().getExtDic().equalsIgnoreCase("")) {   
    35.             File dir = new File(AppInit.getScb().getExtDic());   
    36.             File[] dics = dir.listFiles();   
    37.             // add dict file   
    38.             for (int i = 0; i < dics.length; i++) {   
    39.                 try {   
    40.                     MMAnalyzer.addDictionary(new FileReader(dics[i]));   
    41.                 } catch (FileNotFoundException e) {   
    42.                     // TODO Auto-generated catch block   
    43.                     logger.error("Read File Error", e);   
    44.                 }   
    45.             }   
    46.         }   
    47.     }   
    48.   
    49.     /**  
    50.      * @return the mmanalyzer  
    51.      */  
    52.     public static MMAnalyzer getMmanalyzer() {   
    53.         return mmanalyzer;   
    54.     }   
    55.   
    56.     /**  
    57.      * @param mmanalyzer  
    58.      *            the mmanalyzer to set  
    59.      */  
    60.     public static void setMmanalyzer(MMAnalyzer mmanalyzer) {   
    61.         WordsManager.mmanalyzer = mmanalyzer;   
    62.     }   
    63.   
    64.     public static String[] splitStrToArray(String source) {   
    65.         try {   
    66.             String target = mmanalyzer.segment(source, DEF_SPT);   
    67.             String[] ts = target.split(DEF_SPT);   
    68.             return ts;   
    69.         } catch (IOException e) {   
    70.             // TODO Auto-generated catch block   
    71.             e.printStackTrace();   
    72.         }   
    73.         return null;   
    74.     }   
    75.   
    76.     public static List splitStrToList(String source) {   
    77.         List ret = new ArrayList();   
    78.         try {   
    79.             String target = mmanalyzer.segment(source, DEF_SPT);   
    80.             StringTokenizer st = new StringTokenizer(target, DEF_SPT);   
    81.             for (; st.hasMoreTokens();) {   
    82.                 ret.add(st.nextToken());   
    83.             }   
    84.         } catch (IOException e) {   
    85.             // TODO Auto-generated catch block   
    86.             logger.error("segment error", e);   
    87.         }   
    88.         return ret;   
    89.   
    90.     }   
    91.   
    92.     public static void removeTag(String content) {   
    93.         MMAnalyzer.removeWord(content);   
    94.     }   
    95. }   

    正则方法:

    java 代码
    1. String reg="$1";  
    java 代码
    1. public static String regxReplace(String str, String key, String rep) {   
    2.     Pattern p = Pattern.compile("(" + key + ")", Pattern.CASE_INSENSITIVE);   
    3.     Matcher m = p.matcher(str);   
    4.     return m.replaceAll(rep);   
    5. }  

    原理很简单,利用分词分开,然后用这则一个个匹配掉目标中的关键字,由于实际中大部分都是替换标题和200字的描述所以并没有使用流方法,下次有时间改用流或者stringbuffer或者其他更接近分词的方法来做下看看,有什么问题,欢迎大家拍砖

  52.   
  53.     public HighlightProcess(String str, String key) {   
  54.         // TODO Auto-generated constructor stub   
  55.         this.str = str;   
  56.         this.key = key;   
  57.     }   
  58.   
  59.     /**  
  60.      * @return the str  
  61.      */  
  62.     public String getStr() {   
  63.         return str;   
  64.     }   
  65.   
  66.     /**  
  67.      * @param str  
  68.      *            the str to set  
  69.      */  
  70.     public void setStr(String str) {   
  71.         this.str = str;   
  72.     }   
  73.   
  74.     public String getResult() {   
  75.   
  76.         List list = WordsManager.splitStrToList(key);   
  77.         for (Iterator it = list.iterator(); it.hasNext();) {   
  78.             String target = (String) it.next();   
  79.             this.str = RegxTools.regxReplace(this.str, target, HIGH_LIGHT);   
  80.         }   
  81.         return this.str;   
  82.     }   
  83.   
  84. }  

分词这边使用了一个工厂方法,用来装载词库的,返回的是JE分词对象:

java 代码
  1. import java.io.File;   
  2. import java.io.FileNotFoundException;   
  3. import java.io.FileReader;   
  4. import java.io.IOException;   
  5. import java.util.ArrayList;   
  6. import java.util.Iterator;   
  7. import java.util.List;   
  8. import java.util.StringTokenizer;   
  9.   
  10. import jeasy.analysis.MMAnalyzer;   
  11.   
  12. import org.apache.log4j.Logger;   
  13.   
  14.   
  15. public class WordsManager {   
  16.   
  17.     private static final Logger logger = Logger.getLogger(WordsManager.class);   
  18.   
  19.     private static MMAnalyzer mmanalyzer;   
  20.   
  21.     private static final String DEF_SPT = "<>";   
  22.   
  23.     static {   
  24.         init();   
  25.     }   
  26.   
  27.     private static void init() {   
  28.         reload();   
  29.         mmanalyzer = new MMAnalyzer();   
  30.     }   
  31.   
  32.     public static void reload() {   
  33.         if (AppInit.getScb().getExtDic() != null  
  34.                 && !AppInit.getScb().getExtDic().equalsIgnoreCase("")) {   
  35.             File dir = new File(AppInit.getScb().getExtDic());   
  36.             File[] dics = dir.listFiles();   
  37.             // add dict file   
  38.             for (int i = 0; i < dics.length; i++) {   
  39.                 try {   
  40.                     MMAnalyzer.addDictionary(new FileReader(dics[i]));   
  41.                 } catch (FileNotFoundException e) {   
  42.                     // TODO Auto-generated catch block   
  43.                     logger.error("Read File Error", e);   
  44.                 }   
  45.             }   
  46.         }   
  47.     }   
  48.   
  49.     /**  
  50.      * @return the mmanalyzer  
  51.      */  
  52.     public static MMAnalyzer getMmanalyzer() {   
  53.         return mmanalyzer;   
  54.     }   
  55.   
  56.     /**  
  57.      * @param mmanalyzer  
  58.      *            the mmanalyzer to set  
  59.      */  
  60.     public static void setMmanalyzer(MMAnalyzer mmanalyzer) {   
  61.         WordsManager.mmanalyzer = mmanalyzer;   
  62.     }   
  63.   
  64.     public static String[] splitStrToArray(String source) {   
  65.         try {   
  66.             String target = mmanalyzer.segment(source, DEF_SPT);   
  67.             String[] ts = target.split(DEF_SPT);   
  68.             return ts;   
  69.         } catch (IOException e) {   
  70.             // TODO Auto-generated catch block   
  71.             e.printStackTrace();   
  72.         }   
  73.         return null;   
  74.     }   
  75.   
  76.     public static List splitStrToList(String source) {   
  77.         List ret = new ArrayList();   
  78.         try {   
  79.             String target = mmanalyzer.segment(source, DEF_SPT);   
  80.             StringTokenizer st = new StringTokenizer(target, DEF_SPT);   
  81.             for (; st.hasMoreTokens();) {   
  82.                 ret.add(st.nextToken());   
  83.             }   
  84.         } catch (IOException e) {   
  85.             // TODO Auto-generated catch block   
  86.             logger.error("segment error", e);   
  87.         }   
  88.         return ret;   
  89.   
  90.     }   
  91.   
  92.     public static void removeTag(String content) {   
  93.         MMAnalyzer.removeWord(content);   
  94.     }   
  95. }   

正则方法:

java 代码
  1. String reg="$1";  
java 代码
  1. public static String regxReplace(String str, String key, String rep) {   
  2.     Pattern p = Pattern.compile("(" + key + ")", Pattern.CASE_INSENSITIVE);   
  3.     Matcher m = p.matcher(str);   
  4.     return m.replaceAll(rep);   
  5. }  

原理很简单,利用分词分开,然后用这则一个个匹配掉目标中的关键字,由于实际中大部分都是替换标题和200字的描述所以并没有使用流方法,下次有时间改用流或者stringbuffer或者其他更接近分词的方法来做下看看,有什么问题,欢迎大家拍砖

分享到:
评论

相关推荐

    lunece全文检索C#

    lunece

    lunece 建立索引与查询示例

    lunece 建立索引与查询示例lunece 建立索引与查询示例lunece 建立索引与查询示例lunece 建立索引与查询示例lunece 建立索引与查询示例lunece 建立索引与查询示例

    lunece

    lunece全文检索

    lunece入门之HelloWorld

    NULL 博文链接:https://huhongyu.iteye.com/blog/1929626

    lunece_search_3.0.zip_lunece+es

    学习lunece的一个小例子,实现一些基本的搜索功能~

    lunece 学习笔记实用知识库分享知识分享

    资源lunece 学习笔记实用知识库分享知识分享

    最新版linux lucene-8.5.1.tgz

    最新版linux lucene-8.5.1.tgz

    Lucene.net高速创建索引

    对sqlserver数据库表,用多线程,高速创建索引。性能很高。参数灵活

    lucene-4.10.2

    这个压缩包包lucene所需的jar包-----------------------

    Lucene检索

    一个简单Lucene全文检索案例 A simple Lucene text retrieval case

    全文检索 lucene

    Lucene是用java实现的成熟的、免费的开源项目,是著名的Apache Jakarta大家庭的一员,并且基于在Apache软件许可 [ASF, License]。同样,Lucene是当前与近几年内非常流行的免费的Java信息搜索(IR)库。

    仿google的搜索

    google搜索时时显示数据,利用ajax和lunece技术实现。lunece获取查询数据,然后用ajax操作鼠标和键盘达到效果。

    solr的学习

    Solr 是Apache下的一个顶级开源项目,采用Java开发,它是基于Lucene的全文搜索服务器。Solr可以独立运行在Jetty、Tomcat等这些Servlet容器中。

    dotlucenetest.jar

    lunece 的.net应用实例.

    NLuke.rar_NLuke

    NLuke 一个可以查看lunece索引文件的工具哦

    Blog-online-System:已经上线的SpringBoot博客项目

    图片使用的七牛云,属性设置在类cn.coderzhx.utils.VariableName里 如果不想用七牛云那么修改为为tomcat的upload目录 如果仅仅是本地运行项目的话,七牛云必须得有域名才行所以不能用,使用七牛云的模块就不能用了,也...

    Lucene+in+action中文版

    Lucene+in+action中文完整版-含目录,学习lunece必备

    lucene in Action 中文版

    上面共有4章,也就是lucene英文翻译的前4章,是我见过最多的了,一般都只有前两章!希望会对想学习lunece 的人有所帮助!

    最新.net技术博客源代码.rar

    自主研发的中文分词技术,速度超过3MB/s,准确率达到90%以上,大大超过网上各种开源中文分词技术,几乎可以和中科院的ICTCLAS相媲美,结合当前最成熟的Lunece的.net版本,实现了功能强大执行快速的全文检索引擎...

    X3BLOG 单用户版 1.0 build80707 (access)

    &lt;br&gt; 自主研发的中文分词技术,速度超过3MB/s,准确率达到90%以上,大大超过网上各种开源中文分词技术,几乎可以和中科院的ICTCLAS相媲美,结合当前最成熟的Lunece的.net版本,实现了功能强大执行快速的全文检索...

Global site tag (gtag.js) - Google Analytics