⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tester.java

📁 java实现的全文搜索引擎
💻 JAVA
字号:
package cn.edu.nju.software.ruse;

import java.io.File;
import java.util.List;

/**
 * A facade for testing your RUSE. Change the implementation of the
 * two methods to adapt your version of RUSE.
 * 
 */
public class Tester {

	/**
	 * Entrance to the indexing functionality.
	 * 
	 * @param dataDir
	 *            the root directory of the files
	 * @param indexDir
	 *            the directory to place your index data
	 */
	public static void index(File dataDir, File indexDir) {
		/**
		 * Read the files from the directory offered.
		 * */
		long startbuildIndexTime = System.nanoTime();
		String[] args = new String[1];
		args[0] = dataDir.toString();
		DirList dl = new DirList(args);
//dl.printList();

		/**
		 * Use MyProcess to handle the words in the file.And then build Index.
		 * */
		MyProcess mp = new MyProcess(dl.getList());
		mp.processDoc();
		mp.processTxt();
		mp.buildIndex(indexDir.toString());
		long estimatedbuildIndexTime = System.nanoTime() - startbuildIndexTime;
		System.out.println("It takes " + estimatedbuildIndexTime + " ns to build index!");
	}

	/**
	 * Entrance to the search functionality.
	 * 
	 * @param indexDir
	 *            the index directory
	 * @param query
	 *            the query
	 * @param order
	 * 			0: no order; 1: ordered by file size; 2: ordered by last modified time
	 * @return a list containing the results that match the query
	 */
	public static List<Result> search(File indexDir, String query, int order) {
		/**
		 * Use MySearcher to handle the query.First,read index from disk to memory.
		 * */
		long startSearchTime = System.nanoTime();
		MySearcher ms = new MySearcher(indexDir.toString());
		ms.parse(query);
		long estimatedSearchTime = System.nanoTime() - startSearchTime;
		System.out.println("It takes " + estimatedSearchTime + " ns to search files.");
		
		long startPrintTime = System.nanoTime();
		MyResult mr = new MyResult(query, ms.getResult());
		List<Result> result = null;
		switch (order) {
		case 0:
			result =  mr.getResult();
			break;
		case 1:
			result = mr.getResultBySize();
			break;
		case 2:
			result = mr.getResultByTime();
			break;
		default :{
				System.out.println("@param order");
				System.out.println("0: no order; 1: ordered by file size; 2: ordered by last modified time");
			}
		}
		long estimatedPrintTime = System.nanoTime() - startPrintTime;
		System.out.println("It takes " + estimatedPrintTime + " ns to get files.");
		return result;
	}
	
	public static class Result {
		public int fileSize;
		public String fileName,  modTime;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -