赞
踩
锋哥原创的Springboot+Layui python222网站实战:
- <dependency>
- <groupId>com.github.keran213539</groupId>
- <artifactId>IK_Analyzer</artifactId>
- <version>2012FF_hf1_1</version>
- </dependency>
- package com.python222;
-
-
-
- import com.python222.util.AnalyzerUtil;
- import org.apache.lucene.analysis.Analyzer;
- import org.apache.lucene.analysis.TokenStream;
- import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
- import org.wltea.analyzer.lucene.IKAnalyzer;
-
- import java.io.StringReader;
-
- class Python222ApplicationTests {
-
- public static void main(String[] args) throws Exception{
- Analyzer analyzer=new IKAnalyzer();
- TokenStream tokenStream = analyzer.tokenStream("", "Hive执行计划之什么是hiveSQL向量化模式及优化详解");
- CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
- tokenStream.reset();
- while (tokenStream.incrementToken()){
- System.out.println(charTermAttribute.toString());
- }
- tokenStream.close();
- }
- }

我们做一个分词工具类:
- package com.python222.util;
-
- import org.apache.lucene.analysis.Analyzer;
- import org.apache.lucene.analysis.TokenStream;
- import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
- import org.wltea.analyzer.lucene.IKAnalyzer;
-
- /**
- * 分词器工具类
- * @author python222小锋老师
- * @site www.python222.com
- */
- public class AnalyzerUtil {
-
- /**
- * 生成分词
- * @param text
- * @return
- * @throws Exception
- */
- public static String gen(String text)throws Exception{
- StringBuffer sb=new StringBuffer();
- Analyzer analyzer=new IKAnalyzer();
- TokenStream tokenStream = analyzer.tokenStream("", text);
- CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);
- tokenStream.reset();
- while (tokenStream.incrementToken()){
- sb.append(charTermAttribute.toString()+",");
- }
- tokenStream.close();
- return sb.deleteCharAt(sb.length()-1).toString();
- }
- }

生成 逗号隔开的分词结果。
帖子添加和修改模块,加下
article.setKeyWords(AnalyzerUtil.gen(article.getTitle())); // 根据标题生成关键字分词

article模版加下 分词遍历显示代码:
- <div class="downloadAndKeyWords">
- <ul class="keWords">
- 关键字:
- <li th:each="keyWord:${#strings.arraySplit(article.keyWords,',')}"><a th:href="@{'/search/1?keyWord='+${keyWord}}" target="_blank" th:title="${keyWord}" th:text="${keyWord}"></a></li>
- </ul>
- </div>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。