📄 spelling.java
字号:
/* -*- mode:java; indent-tabs-mode:nil; c-basic-offset:2 -*- * $RCSFile$ $Revision: 1.11 $ $Date: 2006/02/01 00:20:28 $ * Copyright (c) 2003 Autonomy Corp. All Rights Reserved. */import java.io.*;import java.util.*;import com.ultraseek.xpa.server.*;/** * Spelling Suggestion sample application. * <p> * This application demonstrates how to use Ultraseek to * suggest queries based on the re-spelling of a search query. * <p> * The suggestions can vary based on the collections being searched, * and the <code>Locale</code> in use for the query. * <p> * To use this application, edit the file "Sample.properties" * to point to your Ultraseek server. * <p> * Also edit the array <code>locales</code> to set the locales * to use for the demo. * * @since XPA2.1 * @see SpellServer * @see UltraseekServer#getSpeller * @requires Ultraseek 5.0 */public class Spelling { /* Locales to use for this demo of spelling suggestions */ static Locale[] locales = { Locale.getDefault(), Locale.ENGLISH, Locale.FRENCH, Locale.GERMAN, new Locale("es","") // Spanish }; /** * 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); static Collection allCollections; // All collections on 'server' static { try { allCollections = server.getSearchCollections(); } catch (IOException e) { System.out.println( "Problem: " + e ); }; } /** * Creates a spelling suggestion for a query, using the specified * <code>Locale</code> and set of <code>UltraseekCollection</code>. */ static void demoSuggestion(String msg, String query, Locale locale, Collection searchCollections) { if (locale==null) locale = Locale.getDefault(); long start = System.currentTimeMillis(); // Fetch a spelling suggestion String suggestion; try { // Get a speller for this set of collections. // Normally you would do this during initialization. SpellServer speller = server.getSpeller(searchCollections); suggestion = speller.getSuggestion(query,locale); } catch (IOException e) { // In a real application you would handle an IOException by // ignoring it, and not displaying any spelling suggestion. // In this demo, we will report the problem. suggestion = "Problem fetching suggestion: " + e; } long end = System.currentTimeMillis(); StringBuffer sb = new StringBuffer(80); sb.append(msg); String part2 = " " + (end-start) + "ms"; for (int i = sb.length() + part2.length(); i < 21; i++) sb.append( " " ); sb.append(part2); sb.append( " --> " ); if (!suggestion.equals("")) { // Highlight the differences from the original query String diffs[] = SpellServer.splitForHighlight(query,suggestion); for (int i = 0; i < diffs.length; i++) { if (i%2==1) sb.append("<<"); sb.append(diffs[i]); if (i%2==1) sb.append(">>"); } } System.out.println( sb.toString() ); } /** * Create spelling suggestions for a query using different Locales. */ static void demoLocales(String query) { System.out.println("\nLocale Demo - Query: " + query); for (int i = 0; i < locales.length; i++) { demoSuggestion(locales[i].toString(), query, locales[i], null); } } /** * Create spelling suggestions for a query using different * search collections. */ static void demoCollections(String query) { System.out.println("\nCollection Demo - Query: " + query); demoSuggestion("<Default>", query, null, null); // Default search demoSuggestion("<All>", query, null, allCollections); // All collections Iterator i = allCollections.iterator(); while (i.hasNext()) { UltraseekCollection usc = (UltraseekCollection) i.next(); Collection c = new ArrayList(1); c.add( usc ); demoSuggestion(usc.getID(), query, null, c); } } static public void main(String args[]) throws IOException { System.out.println("Spelling Demo"); System.out.println("Using Ultraseek at " + server ); System.out.println(" (version " + server.getVersionString() +")" ); if (!server.versionAtLeast(5,0,0)) { System.out.println("Ultraseek version 5.0 (or newer) is required for this demo." ); return; } System.out.println( "" ); demoSuggestion("Welcome", "Ultraseek suggestss queries by re-spelling terms", Locale.ENGLISH, null); System.out.println( "\n--- Suggested terms must exist in the searched collection" ); demoCollections("Veerity Ultrasek missspelling deemo"); System.out.println( "\n--- The user's Locale is used to guide suggestions" ); demoLocales("helllo, bonjourr, gooten tag, hoola"); InputStreamReader inputStreamReader = new InputStreamReader(System.in); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); while (true) { System.out.print("\nEnter a query (EOF to end): "); String line = bufferedReader.readLine(); if (line==null) break; demoCollections( line ); demoLocales( line ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -