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

📄 ruse.java

📁 java实现的全文搜索引擎
💻 JAVA
字号:
/**
* This is the main class.
 * */
package cn.edu.nju.software.ruse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import conf.PropertyMgr;


public class RUSE {

	/**
	 * @author spring
	 */
	
	/**
	 * @param Really Useful Search Engine (RUSE).A search engine is simply a utility that answers queries and gives reasonable results among a
	 * set of documents. RUSE only deals with textual information, that is, documents and queries are all text.
	 */
 
	public static void main(String[] args) {
		
		/*
		 * Read property
		 * */
		String fileDir = null;
		String query = null;
		System.out.print("Please enter the File Directory:");
		BufferedReader temp = new BufferedReader(new InputStreamReader(System.in));
		try {
			fileDir = temp.readLine();
			args = new String[1];
			args[0] = fileDir;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.print("Please enter the query expression:");
		try {
			query = temp.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/**
		 * Read the files from the directory offered.
		 * */
		long startbuildIndexTime = System.nanoTime();
		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("F:\\");
		long estimatedbuildIndexTime = System.nanoTime() - startbuildIndexTime;
		System.out.println("It takes " + estimatedbuildIndexTime + " ns to build index!");
		
		/**
		 * Use MySearcher to handle the query.First,read index from disk to memory.
		 * */
		long startSearchTime = System.nanoTime();
		MySearcher ms = new MySearcher("F:\\");
		//String query = "(hockey OR (physics AND town)) OR (jump AND (gold OR silver))";
		ms.parse(query);//(A OR (B AND C)) OR (D AND (E OR F))
		long estimatedSearchTime = System.nanoTime() - startSearchTime;
		System.out.println("It takes " + estimatedSearchTime + " ns to search files.");
		
		MyResult mr = new MyResult(query,ms.getResult());
		//mr.printResult();
		//mr.printResultBySize();
		//mr.printResultByTime();
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		String s;
		String s1;
		String s2;
		usage();
		try {
			while((s = stdin.readLine()) != null && s.length() != 0) {
				int choice = Integer.parseInt(s);
				switch (choice) {
				case 1:
					mr.printResult();
					break;
				case 2:
					mr.printResultByTime();
					break;
				case 3:
					mr.printResultBySize();
					break;
				case 4: {
					usageForRelevant();
					System.out.println("The Result contains " + mr.getResult().size() + " Files!");
					while((s1 = stdin.readLine()) != null && s1.length() != 0) {
						int subChoice = Integer.parseInt(s1);
						switch (subChoice) {
						case 1:{
							System.out.println();
							System.out.print("Enter the NUMBER: ");
							while((s2 = stdin.readLine()) != null && s2.length() != 0) {
								if(s2.equals("quit")) {
									System.out.println("About to Quit...");
									System.exit(0);
								}
								int num = Integer.parseInt(s2);
								mr.printNext(num);
							}
							break;
						}
						case 2:
							System.out.println("About to Quit...");
							System.exit(0);
						}
					}
					break;
				}
				case 5:
					System.out.println("About to Quit...");
					System.exit(0);
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	 private static void usage() {
		System.out.println();
        System.out.println("Usage: choose the number of the actions you want RUSE to perform!");
        System.out.println("1.Print all the result in one time!");
        System.out.println("2.Print all the result order by TIME!");
        System.out.println("3.Print all the result order by SIZE!");
        System.out.println("4.Print the relevantest result!");
        System.out.println("5.Quit!");
        System.out.println();
	 }
	 
	 private static void usageForRelevant() {
		System.out.println();
        System.out.println("Usage: choose the number of actions you want RUSE to perform!");
        System.out.println("1.Enter the number of FILES you want to print!");
        System.out.println("2.Quit!");
        System.out.println();
	 }
}

⌨️ 快捷键说明

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