📄 searchkeywords.java
字号:
/* -*- mode:java; indent-tabs-mode:nil; c-basic-offset:2 -*- * * $RCSFile$ $Revision: 1.7 $ $Date: 2006/02/01 00:20:29 $ * * Copyright (c) 2002-2004 Autonomy Corp. All Rights Reserved. * Permission to use, copy, modify, and distribute this file is hereby * granted without fee, provided that the above copyright notice appear * in all copies. */import java.io.*;import java.util.*;import java.net.URL;import com.ultraseek.xpa.search.*;/** * A simple demo application that searches * an in-memory keyword list for matches. * This is useful for "editorially picked" search * results. * * @see KeywordSearchable * @see EditorialSearchable */public class SearchKeywords { SearchKeywords() { } /** * Number of results to print as part of the sample output. */ static final int MAX_RESULTS = 4; public final static boolean VERBOSE = false; /** Utility function to print out verbose messages */ public static void VerbosePrint(String msg) { if (VERBOSE) System.out.println(msg); } /** Utility function to input a keyword string */ public static String inputKeyword(BufferedReader in, String prompt) throws IOException { System.out.print(prompt +": "); String keyword = in.readLine().trim(); return keyword; } /** Utility function to input y/n response */ public static boolean inputYesNo(BufferedReader in, String prompt) throws IOException { while (true) { System.out.print(prompt + " (y/n): "); String response = in.readLine(); if (response != null) { response = response.trim().toUpperCase(); if (response.length() > 0) { char answer = response.charAt(0); if (answer == 'Y') return true; if (answer == 'N') return false; } } System.out.println("Must enter y or n" ); } } /** * For demo purposes, create a search result to return for a keyword. * In actual use this would be your editorially picked result * to return for the keyword. */ public static FixedSearchResult makeKeywordResult(String title) { Date now = new Date(); URL url = null; try { url = new URL("http://www.yourcorp.com/"+title ); } catch (java.net.MalformedURLException e) { /*ignore*/ }; return new FixedSearchResult((float)1.0, // score (float)0.0, // quality url, // url "Title: " + title, // title "Description: " + title, // description "", // publisher "", // fsKeywords 0, // size now, // date now, // indexed (short)0, // remoteLinkCount (short)0, // flags null, // topics null, // termTFs null, // extra null, // SearchServer null // SearchCollection ); } /** * Show the results from a search */ public static void showResults(String prompt, SearchResultList searchResultList) { System.out.println(prompt); System.out.println(); VerbosePrint("Here is the SearchResultList object:\n"+ searchResultList); try { Collection relatedTopics = searchResultList.getRelatedTopics(); System.out.println(); if (relatedTopics != null || !relatedTopics.isEmpty()) { System.out.println("Here are the SearchTopic objects:"); Iterator iterator = relatedTopics.iterator(); while (iterator.hasNext()) { SearchTopic searchTopic = (SearchTopic)iterator.next(); System.out.println(searchTopic); } } else { System.out.println("There are no SearchTopic objects."); } System.out.println(); if (!searchResultList.isEmpty()) { System.out.println("There are about " + searchResultList.getResultCount() + " search results."); System.out.println("Here are the SearchResult objects:"); Iterator iterator = searchResultList.iterator(); int counter = 0; try { while (iterator.hasNext() && counter++ < MAX_RESULTS) { SearchResult searchResult = (SearchResult)iterator.next(); System.out.println(); System.out.println("" + counter + " " + searchResult.getTitle() + "\n" + " " + searchResult.getDescription() ); VerbosePrint(searchResult.toString()); } } catch (NoSuchElementException e) { /* No more results from server, not an error. */ } } else { System.out.println("There are no SearchResult objects."); } } catch (IOException e) { System.out.println( "Exception while printing results: "+ e.toString()); } } public static void main(String[] args) throws Exception { InputStreamReader inputStreamReader = new InputStreamReader(System.in); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); System.out.println(); System.out.println("This demo application searches"); System.out.println("in a program defined keyword list."); KeywordSearchable kwSearchable = new KeywordSearchable(); if (inputYesNo(bufferedReader,"Y for Exact match, N for any terms match")) kwSearchable.setExact(true); else kwSearchable.setExact(false); // Collect a title for a dummy search result. // Then collect a series of keywords to associate with the title. System.out.println(); System.out.println("The application will now collect 'Search Results'"); System.out.println("and keywords to associate with each result."); System.out.println(); for (int count = 1;;count++) { String title = inputKeyword(bufferedReader, "Enter Title for search result " + count + ": RETURN to end"); if ((title == null) || title.length() <= 0) break; List keywords = new ArrayList(5); for (;;) { String keyword = inputKeyword(bufferedReader, "Enter keyword for '" + title + "', RETURN to end" ); if ((keyword == null) || keyword.length() <= 0) break; keywords.add(keyword); } FixedSearchResult res = makeKeywordResult(title); kwSearchable.add((String [])keywords.toArray(new String[keywords.size()]),res); } VerbosePrint("Here is the KeywordSearchable object:\n"+ kwSearchable); for (;;) { System.out.println(); System.out.println("Enter a query."); System.out.print("search: "); String line = bufferedReader.readLine(); if (line==null) break; Query query = Query.parse(line); System.out.println(); VerbosePrint("Here is the Query object:\n"+ query); /* Start the searches running */ SearchResultList keywordResults = kwSearchable.search(query); /* Now display the results */ showResults( "Keyword Matches", keywordResults ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -