KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > websphinx > searchengine > Google


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.google.com/">Google</a> search engine.
42  * @author Justin Boitano
43  */

44 public class Google implements SearchEngine {
45
46     static Pattern patCount = new Regexp (
47         "</b> of approximately <b>\\d+,?(\\d+)</b> for <b>"
48     );
49     static Pattern patNoHits = new Regexp (
50         "Your search did not produce any results"
51     );
52
53     static Pattern patResult = new Tagexp (
54         "<p>(?{link}<a>(?{title})</a>)<font>" // title and main link
55
+ "<BR>(?{description}.*?)<font color=green>" //description of link
56
);
57
58     static Pattern patMoreLink = new Tagexp (
59          "<A HREF=/search?q=*><img><br><font>.*?</a>"
60     );
61
62     /**
63      * Classify a page. Sets the following labels:
64      * <TABLE>
65      * <TR><TH>Name <TH>Type <TH>Meaning
66      * <TR><TD>searchengine.source <TD>Page label <TD>Google object that labeled the page
67      * <TR><TD>searchengine.count <TD>Page field <TD>Number of results on page
68      * <TR><TD>searchengine.results <TD>Page fields <TD>Array of results. Each result region
69      * contains subfields: rank, title, description, and link.
70      * <TR><TD>searchengine.more-results <TD>Link label <TD>Link to a page containing more results.
71      * </TABLE>
72      */

73     public void classify (Page page) {
74         String JavaDoc title = page.getTitle ();
75     if(title !=null && title.startsWith("Google Search:")){
76         page.setObjectLabel("searchengine.source",this);
77
78             Region count = patCount.oneMatch (page);
79         
80             if (count != null)
81                 page.setField ("searchengine.count", count.getField ("0"));
82             
83             Region[] results = patResult.allMatches (page);
84             SearchEngineResult[] ser = new SearchEngineResult[results.length];
85
86             for (int i=0; i<results.length; ++i)
87                 ser[i] = new SearchEngineResult (results[i]);
88             page.setFields ("searchengine.results", ser);
89
90             PatternMatcher m = patMoreLink.match (page);
91         
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 Google.
115      * @param keywords list of keywords, separated by spaces
116      * @return URL that submits the keywords to Google.
117      */

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

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

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

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