KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > websphinx > searchengine > HotBot


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;
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.hotbot.com/">HotBot</a> search engine.
42  */

43 public class HotBot implements SearchEngine {
44
45     static Pattern patTitle = new Regexp ("^");
46
47     static Pattern patCount = new Regexp (
48         "Returned <B>(\\d+)</b> matches"
49     );
50     static Pattern patNoHits = new Regexp (
51         "Sorry -- your search yielded no results"
52     );
53
54     // FIX: works only for Netscape
55
static Pattern patResult = new Tagexp (
56       "<td><b>(?{rank})</b></td>" // rank
57
+ "<td>(?:<a><img></a>)?" // optional icon
58
+ "(?{link}<a>(?{title})</a>)</td>" // title and main link
59
+ "</tr><tr><td><font>(?{score})</font></td><td>(?{description})<br></td>" // description
60
);
61
62     static Pattern patMoreLink = new Tagexp (
63         "<input type=image name=act.next>"
64     );
65
66     /**
67      * Classify a page. Sets the following labels:
68      * <TABLE>
69      * <TR><TH>Name <TH>Type <TH>Meaning
70      * <TR><TD>searchengine.source <TD>Page label <TD>HotBot object that labeled this page
71      * <TR><TD>searchengine.count <TD>Page field <TD>Number of results on page
72      * <TR><TD>searchengine.results <TD>Page fields <TD>Array of results. Each result region
73      * contains subfields: rank, title, description, and link.
74      * <TR><TD>searchengine.more-results <TD>Link label <TD>Link to a page containing more results.
75      * </TABLE>
76      */

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

109     public static final float priority = 0.0F;
110     
111     /**
112      * Get priority of this classifier.
113      * @return priority.
114      */

115     public float getPriority () {
116         return priority;
117     }
118
119     /**
120      * Make a query URL for HotBot.
121      * @param keywords list of keywords, separated by spaces
122      * @return URL that submits the keywords to HotBot.
123      */

124     public URL JavaDoc makeQuery (String JavaDoc keywords) {
125         try {
126             return new URL JavaDoc("http://www.search.hotbot.com/hResult.html/?SM=MC&MT="
127                            + URLEncoder.encode(keywords)
128                            + "&DV=7&RG=.com&DC=10&DE=2&OPs=MDRTP&_v=2&DU=days&SW=web");
129         } catch (MalformedURLException JavaDoc e) {
130             throw new RuntimeException JavaDoc ("internal error");
131         }
132     }
133
134     /**
135      * Get number of results per page for this search engine.
136      * @return typical number of results per page
137      */

138     public int getResultsPerPage () {
139         return 10;
140     }
141
142     /**
143      * Search HotBot.
144      * @param keywords list of keywords, separated by spaces
145      * @return enumeration of SearchEngineResults returned by a HotBot query constructed from the keywords.
146      */

147     public static Search search (String JavaDoc keywords) {
148         return new Search (new HotBot(), keywords);
149     }
150
151     /**
152      * Search HotBot.
153      * @param keywords list of keywords, separated by spaces
154      * @param maxResults maximum number of results to return
155      * @return enumeration of SearchEngineResults returned by an HotBot query constructed from the keywords.
156      * The enumeration yields at most maxResults objects.
157      */

158     public static Search search (String JavaDoc keywords, int maxResults) {
159         return new Search (new HotBot(), keywords, maxResults);
160     }
161 }
162
Popular Tags