KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > websphinx > searchengine > Excite


1 /*
2  * WebSphinx web-crawling toolkit
3  *
4  * Copyright (c) 1998-2002 Carnegie Mellon University. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
20  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
23  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  */

32
33 package websphinx.searchengine; // ALB 12/97
34

35 import websphinx.*;
36 import java.net.URL JavaDoc;
37 import java.net.URLEncoder JavaDoc;
38 import java.net.MalformedURLException JavaDoc;
39
40 /**
41  * <A HREF="http://www.excite.com/">Excite</a> search engine.
42  * @author Adam Berger
43  */

44 public class Excite implements SearchEngine {
45
46     static Pattern patCount = new Regexp (
47         "<font size=-1 face=\"arial, helvetica\">(?:About )?<b>(\\d+)</b> documents? match your query.");
48     static Pattern patNoHits = new Regexp (
49         "No documents match the query."
50     );
51
52     static Pattern patResult = new Tagexp (
53         "<FONT COLOR=navy><B>(?{score})</B></FONT>" // score
54
+ "<B>(?{link}<a>(?{title})</a>)</B>" // title and main link
55
+ "<BR><B><I></I></B>" // URL
56
+ "<BR><B><I></I></B>(?{description})" // Summary
57
+ "<BR><B><I></I></B><a><p></a>" // More Like This
58
);
59
60     static Pattern patMoreLink = new Tagexp (
61         "<INPUT TYPE=submit NAME=next VALUE=\"Next Results\">"
62     );
63
64     /**
65      * Classify a page. Sets the following labels:
66      * <TABLE>
67      * <TR><TH>Name <TH>Type <TH>Meaning
68      * <TR><TD>searchengine.source <TD>Page label <TD>Excite object that labeled the page
69      * <TR><TD>searchengine.count <TD>Page field <TD>Number of results on page
70      * <TR><TD>searchengine.results <TD>Page fields <TD>Array of results. Each result region
71      * contains subfields: score, title, description, and link.
72      * <TR><TD>searchengine.more-results <TD>Link label <TD>Link to a page containing more results.
73      * </TABLE>
74      */

75     public void classify (Page page) {
76         String JavaDoc title = page.getTitle ();
77         if (title != null &&
78             (title.startsWith ("Excite Search Results"))) {
79             page.setObjectLabel ("searchengine.source", this);
80
81             Region count = patCount.oneMatch (page);
82             if (count != null)
83                 page.setField ("searchengine.count", count.getField ("0"));
84             
85             Region[] results = patResult.allMatches (page);
86             SearchEngineResult[] ser = new SearchEngineResult[results.length];
87             for (int i=0; i<results.length; ++i)
88                 ser[i] = new SearchEngineResult (results[i]);
89             page.setFields ("searchengine.results", ser);
90
91             PatternMatcher m = patMoreLink.match (page);
92             while (m.hasMoreElements ()) {
93                 Link link = (Link)m.nextMatch ();
94                 link.setLabel ("searchengine.more-results");
95                 link.setLabel ("hyperlink");
96             }
97         }
98     }
99
100     /**
101      * Priority of this classifier.
102      */

103     public static final float priority = 0.0F;
104     
105     /**
106      * Get priority of this classifier.
107      * @return priority.
108      */

109     public float getPriority () {
110         return priority;
111     }
112
113     /**
114      * Make a query URL for Excite.
115      * @param keywords list of keywords, separated by spaces
116      * @return URL that submits the keywords to Excite.
117      */

118     public URL JavaDoc makeQuery (String JavaDoc keywords) {
119         try {
120             return new URL JavaDoc("http://www.excite.com/search.gw?trace=a&search="
121                          + URLEncoder.encode(keywords));
122         } catch (MalformedURLException JavaDoc e) {
123             throw new RuntimeException JavaDoc ("internal error");
124         }
125     }
126
127     /**
128      * Get number of results per page for this search engine.
129      * @return typical number of results per page
130      */

131     public int getResultsPerPage () {
132         return 10;
133     }
134
135     /**
136      * Search Excite.
137      * @param keywords list of keywords, separated by spaces
138      * @return enumeration of SearchEngineResults returned by an Excite query constructed from the keywords.
139      */

140     public static Search search (String JavaDoc keywords) {
141         return new Search(new Excite(), keywords);
142     }
143
144     /**
145      * Search Excite.
146      * @param keywords list of keywords, separated by spaces
147      * @param maxResults maximum number of results to return
148      * @return enumeration of SearchEngineResults returned by an Excite query constructed from the keywords.
149      * The enumeration yields at most maxResults objects.
150      */

151     public static Search search (String JavaDoc keywords, int maxResults) {
152         return new Search (new Excite(), keywords, maxResults);
153     }
154 }
155
Popular Tags