オートコンプリート検索javaのjavaのjavaの自動マッチングピンイン検索

                  オートコンプリート検索javaのjavaのjavaの自動マッチングピンイン検索

 

依存するJARパッケージ

<!-- https://mvnrepository.com/artifact/org.ansj/ansj_seg -->
<dependency>
    <groupId>org.ansj</groupId>
    <artifactId>ansj_seg</artifactId>
    <version>5.1.6</version>
</dependency>

 

第二に、コードの実装

1、SearcuUtilsは、特定の検索を実現します

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.nlpcn.commons.lang.index.MemoryIndex;
import org.nlpcn.commons.lang.pinyin.Pinyin;

/**
 * description: 使用 ansj_seg 实现简单的文字搜索
 * @version v1.0
 * @author w
 * @date 2020年2月29日下午9:00:42
 **/

public class SearcuUtils {
	// 构造方法私有化
	private SearcuUtils INSTANSE = new SearcuUtils ();
	
	/**|
	 * description: 实现简单文字搜索
	 * @param key  用于搜索的关键词
	 * @param wordList  待搜索的词库
	 * @return List<String>
	 * @version v1.0
	 * @author w
	 * @date 2020年2月29日 下午9:12:54
	 */
	@SuppressWarnings("unchecked")
	public static List<String> search(String key , List<String> wordList){
		MemoryIndex<String> memoryIndex = new MemoryIndex<String>();
        if (StringUtils.isBlank(key) || CollectionUtils.isEmpty(wordList)) {
            return Collections.EMPTY_LIST;
        }
        for (String word : wordList) {
        	// 汉字转为完整拼音,如:中国 -- zhongguo
            String fullChar = StringUtils.join(Pinyin.pinyin(word), "");
            // 汉字转为拼音缩写,如:中国 -- zg
            String firstChar = StringUtils.join(Pinyin.firstChar(word), "");
            memoryIndex.addItem(word, word, fullChar, firstChar);
        }
        return memoryIndex.suggest(key);
	} 
	
	/**
	 * description: 加载待搜索的词库 --- 临时用
	 * @param path
	 * @return List<String>
	 * @version v1.0
	 * @author w
	 * @date 2020年2月29日 下午9:15:51
	 */
	public static List<String> loadWordList() {
		List<String> list = new ArrayList<>();
		list.add("中国");
		list.add("中华人民共和国");
		list.add("中国人民");
		list.add("我爱中国");
		return list;
	}
}

 

2、SearchController検索マッチコントローラ

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.runcode.utils.SearcuUtils;

/**
 * description: 搜索Controller
 * @version v1.0
 * @author w
 * @date 2020年2月29日下午8:58:30
 **/
@RestController
@RequestMapping(value = "/searchController")
public class SearchController {

	@RequestMapping(value= "/search")
	public String search(String key) {
		List<String> search = SearcuUtils.search(key, SearcuUtils.loadWordList());
		return StringUtils.join(search, ",") ;
	}
}

 

第三に、テスト

1、ブラウザ、入力します。http:// localhost:8280 /春-MVC-観光/ searchController /キー= RM検索?

リターン結果:中国の人々 、中国の人民共和国

2、输入ます。http:// localhost:?8280 /春-MVC-観光/ searchController /検索キー=中国

リターン結果:中国、中国の人々は、私は中国を愛し

 

IVの概要

パッケージの特定の実装である1、主に単純な問題ではありません需要を満たすために、ansj_segパッケージに依存します。

2は、フロントエンドで、簡単な検索が一致し、自動的に実現することができます。

 

 

公開された156元の記事 ウォンの賞賛159 ビュー490 000 +

おすすめ

転載: blog.csdn.net/HaHa_Sir/article/details/104583033