基于hashset对中文词快速查询
下载附件"百度分词词库", 里面大约有10w个词, 使用C语言或者Java实现单词快速查找功能(不借助第三方类库工具或者数据库), 将单词载入内存中, 建立词索引, 实现快速查找单词是否存在. 最终表现的功能是输入一个词, 返回这个词是否存在.
主类query
将文件读入到hashset/内存中
字符缓存输入流 读取文件将文件内容放到set中
然后关闭文件流
public class query { public SetreadWordFile() { Set wordSet = null; // 要读取的文件路径 File file = new File( "index/src/Test/百度分词词库.txt"); try { // 读取文件输入流 InputStreamReader read = new InputStreamReader(new FileInputStream(file), "utf-8"); // 文件是否是文件 和 是否存在 if (file.isFile() && file.exists()) { wordSet = new HashSet (); //字符缓存输入流 BufferedReader br = new BufferedReader(read); String txt = null; // 读取文件,将文件内容放入到set中 while ((txt = br.readLine()) != null) { wordSet.add(txt); } br.close(); } // 关闭文件流 read.close(); } catch (Exception e) { e.printStackTrace(); } return wordSet; } }
public class TestQuery { public static void main(String[] args) { while(true) { System.out.println("请输入汉字,输入#表结束"); Scanner scanner=new Scanner(System.in); String word=scanner.next(); if(word.equals("#")){ System.out.println("再见 欢迎你下次使用"); break; }else { int flag=0; query query = new query(); Setwords= query.readWordFile(); for (String word1:words){ if (word1.equals(word)){ flag=1;break; } } if (flag==1){ System.out.println("存在"); }else{ System.out.println("不存在"); } } } } }