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

📄 highlight.java

📁 关于Ultraseek的一些用法,刚初学,所以都是比较简单
💻 JAVA
字号:
/* -*- mode:java; indent-tabs-mode:nil; c-basic-offset:2 -*- *  $RCSFile$ $Revision: 1.10 $ $Date: 2006/02/01 00:20:30 $ *  Copyright (c) 2003 Autonomy Corp.  All Rights Reserved. */import java.io.*;import java.util.*;import com.ultraseek.xpa.search.*;import com.ultraseek.xpa.server.*;/** * Highlighting of Titles, Description, and documents. * <p> * This application demonstrates how to use Ultraseek to * highlight search terms appearing in a title or description. * <p> * It also demonstrates which documents can be highlighted * by the Ultraseek server. * <p> * To use this application, edit the file "Sample.properties"  * to point to your Ultraseek server. *  * @see UltraseekSearchResult#getTitleForHighlight * @see UltraseekSearchResult#getDescriptionForHighlight * @see UltraseekSearchResult#isDocumentHighlightable * @see UltraseekSearchResult#getMimeType * @since XPA2.1 * @requires Ultraseek 5.1.1 */public class Highlight {  /**   * Loads parameters from the file Sample.properties   */  static String getString(String key, String def) {    try {      String resourceName = System.getProperty("Sample");      if (resourceName==null) resourceName = "Sample";      ResourceBundle settings = ResourceBundle.getBundle( resourceName );      return settings.getString(key);    } catch (MissingResourceException e) {      return def;    } catch (Exception e) {      System.out.println( "Property file problem: "  + e );      return def;    }  }  static final String serverName  = getString("UltraseekServer.host",                                               "software-demo.ultraseek.com");  static final int    serverPort  = Integer.valueOf(getString("UltraseekServer.port",                                                               "80")).intValue();  static final String serverProto = getString("UltraseekServer.protocol",                                               "http");  static UltraseekServer server = new UltraseekServer(serverName, serverPort, serverProto);  /**   * Highlight terms with * for display at the terminal.   */  static String highlightResult(String split[]) {    if (split==null) return "";    StringBuffer sb = new StringBuffer();    for (int i = 0; i < split.length; i++) {      if (i % 2 == 1) sb.append( "***" );      sb.append(split[i]);      if (i % 2 == 1) sb.append( "***" );    }    return sb.toString();  }  /**   * HTMLQuotes a String.   * @return the HTML quoted string.   * @param s the String to quote.   */  public static String htmlquote(String s) {    StringBuffer sb = new StringBuffer(s.length() * 2);    for (int i = 0; i < s.length(); i++) {      char ch = s.charAt(i);      if (ch == '&') sb.append("&amp;");      else if (ch == '"') sb.append("&quot;");      else if (ch == '<') sb.append("&lt;");      else if (ch == '>') sb.append("&gt;");      else if (ch > '\u00FF') {        sb.append( "&#" );        sb.append( Integer.toHexString(ch) );        sb.append( ";" );      } else        sb.append( ch );    }    return sb.toString();  }  public static String HIGHLIGHT_START = "<b>";  public static String HIGHLIGHT_END   = "</b>";  public static String PASSAGE_SEP     = "&nbsp;...&nbsp;";  /**   * Prepares HTML for a search result's description.   * <p>   * The DESCRIPTION_SEPARATOR (Unicode Horizontal Ellipsis) does not   * always display very well in a web page.  This function remaps it to   * three ASCII dots.   * <p>   * The result of this function is HTML quoted.   * @param text the description of a search result.   * @return the HTML-Quoted and prepared description.   */  public static String quoteDescription(String text) {    /* find each passage, replace the separator with ... */    StringBuffer sb = new StringBuffer(text.length() * 2);    while (text.length() > 0) {        int next_separator = text.indexOf(UltraseekSearchResult.DESCRIPTION_SEPARATOR);        if (next_separator == -1) {          sb.append( htmlquote(text) );          break;        } else {          String before = text.substring(0,next_separator);          sb.append( htmlquote(before) );          sb.append( PASSAGE_SEP );          text = text.substring(next_separator+1);        }      }    return sb.toString();  }  /**   * Prepares HTML for a search result's description.   * <p>   * The DESCRIPTION_SEPARATOR (Unicode Horizontal Ellipsis) does not   * always display very well in a web page.  This function remaps it to   * three ASCII dots.   * <p>   * Highlighting of search terms uses HIGHLIGHT_START and    * HIGHLIGHT_END around portions to be highlighted.   * <p>   * The result of this function is HTML quoted.   * @param sr The UltraseekSearchResult to prepare a description.   * @return the HTML-Quoted and prepared description.   * @exception IOException There was a problem fetching the description.   */  public static String makeDescription(UltraseekSearchResult sr)     throws IOException {    String parts[] = sr.getDescriptionForHighlight();    StringBuffer sb = new StringBuffer(1024);    for (int i = 0; i < parts.length; i++) {      if (i % 2 == 1) {        /* Odd numbered array element - highlight this part */        sb.append( HIGHLIGHT_START );        sb.append( quoteDescription( parts[i] ));        sb.append( HIGHLIGHT_END );      } else {        /* Even numbered array element - no highlighting */        sb.append( quoteDescription( parts[i] ));      }    }    return sb.toString();  }  /**   * Simple display of a search result.   */  static void showResult(SearchResult sr)     throws IOException {    System.out.println("---------------------------------");    if (sr instanceof UltraseekSearchResult) {      UltraseekSearchResult usr = (UltraseekSearchResult) sr;      System.out.print("Title             : ");      System.out.println(highlightResult(usr.getTitleForHighlight()));      String description = makeDescription(usr);      System.out.print("Description       : ");      System.out.println( description );      System.out.print("URL               : ");      System.out.println(usr.getURLString());      if (usr.getMimeType()==null)        System.out.println("Document MIME type: unknown");      else        System.out.println("Document MIME type: " + usr.getMimeType());      if (usr.isDocumentHighlightable()) {        System.out.println("This document can be highlighted by Ultraseek.");        System.out.print("Terms: ");        Iterator it = usr.getTermTFs().keySet().iterator();        while (it.hasNext()) {          String term = (String) it.next();          System.out.print( "'" + term + "' " );        }        System.out.println();      } else        System.out.println("This document cannot be highlighted by Ultraseek.");    } else {      System.out.println(sr.getTitle());      System.out.println(sr.getDescription());      System.out.println(sr.getURLString());      System.out.println("Not an UltraseekSearchResult.");    }    System.out.println();  }  static void demoHighlight(String query)    throws IOException {    Query q = Query.parse(query);    final int SHOWCOUNT = 4;    try {      SearchResultList srl = server.search(q);      // Show summary of search results.      System.out.println();      System.out.println( "" + srl.getDocCount() + " documents were searched.");      System.out.println( "" + srl.getResultCount() + " document(s) match your query.");      System.out.println( "" + srl.size() + " search result(s) have been prioritized.");      System.out.println( "" + Math.min(SHOWCOUNT,srl.size()) +                           " search result(s) will be displayed." );      Iterator it = srl.getTermDFs().keySet().iterator();      while (it.hasNext()) {        String term = (String) it.next();        Integer count = (Integer) srl.getTermDFs().get(term);        System.out.println( term + ": " + count.intValue() + " documents." );      }            int count = 0;      try {        // Only show a few results for this demo.        while (count < SHOWCOUNT) {          SearchResult sr = (SearchResult) srl.get(count);          showResult(sr);          count++;        }      } catch (IndexOutOfBoundsException e) {        // Normal end of list - not an error.      }    } catch (QueryNotSupportedException e) {      System.out.println( "Query problem: " + e );    }  }  static public void main(String args[])     throws IOException {    System.out.println("Highlight Demo");    System.out.println("Using Ultraseek at " + server );    System.out.println(" (version " + server.getVersionString() +")" );    if (!server.versionAtLeast(5,1,1)) {      System.out.println("Ultraseek version 5.1.1 (or newer) is required for this demo." );      return;    }    InputStreamReader inputStreamReader = new InputStreamReader(System.in);    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);    while (true) {      System.out.print("\n\nEnter a query (EOF to end): ");      String line = bufferedReader.readLine();      if (line==null) break;      try {        demoHighlight( line );      } catch (IOException e) {        System.out.println( "Problem communicating with server: " + e );      } catch (XPARuntimeException e) {        System.out.println( "Problem communicating with server: " + e.getCause() );      }    }  }}

⌨️ 快捷键说明

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