Case study: Interfacing to a search engine Assignment Help

Assignment Help: >> Java Programming Concepts >> Case study: Interfacing to a search engine

Case study: Interfacing to a search engine:

This case study deals with writing program code that enables a user to carry out a set of sophisticated searches using a search engine. A search engine is a program, accessible via the internet, that enables the user to discover information based on a number of keywords. For example, if you want to find all the web pages that reference Towcester rugby club then you would type in:

Towcester Rugby Club

There are a number of search engines that are available for free use (at the time of writing in October 2005). Almost certainly the best known and most used is Google. This is because it uses software that employs novel methods to return accurate results.

During 2004 the Google developers released a Java API that enabled programmers to develop code that interacts with Google. An example of its use is shown below. Here a simple query is fed to Google and it produces the URL of any web pages that match the query together with the title of the page.

import com.google.soap.search.*;
public class GoogleSearchDemo

{
public static void main(String args [])
{
GoogleSearch searchItem = new GoogleSearch();
searchItem.setKey("...");
searchItem.setQueryString("\"Towcester Rugby Club\"");
searchitem.setMaxResults(5);
// Start the search try
{
GoogleSearchResult searchResult = searchItem.doSearch();
// process the results
GoogleSearchResultElement [] re = searchResult.getResultElements();
for(int i = 0; i < re.length; i++)
{
System.out.println(re [i].getURL() + " " + re [i].getTitle());
}
}
catch(Exception e)
{
System.out.println("Problem "+e);
}
}
}

The import statement:

import com.google.soap.search.*;

first imports the Java library for the Google API. The line:

GoogleSearch searchItem = new GoogleSearch();

then creates a GoogleSearch object. This is the main object used to retrieve data from the Google search engine's databases. The next line:

searchItem.setKey("...");

sets a key that has to be used by any program that accesses the Google search engine. Each user of Google is assigned a key when they register for this form of use of the search engine. This key is unique to its user and, at the time of writing (October 2005), allows up to 1000 searches per day.

The next two lines are:

searchItem.setQueryString("\"Towcester Rugby Club\"");
searchItem.setMaxResults(5);

The first line sets the query that is to be applied to the Google database; notice the escape character \ used to introduce double-quoted characters. The next line specifies that only the top five pages that match the query are to be returned. At the time of writing Google allows you to recover only the top ten pages for a search, although users have been promised that this will change as the Google API becomes more mature. Once these parameters have been set the search can be carried out. This is done via the line:

GoogleSearchResult searchResult = searchItem.doSearch();

This creates a GoogleSearchResult object that represents the result of the search. The next two lines:
GoogleSearchResultElement [] re = searchResult.getResultElements();

place in the array re, which contains GoogleSearchResultElement objects, the details of each of the web pages that have been retrieved.

The array can then be iterated through and the details of each page displayed. This is shown below:

for (int i = 0; i < re.length; i++)
{
System.out.println(re [i].getURL()
+ " " + re [i].getTitle());
}

Here the URL of the page is displayed together with the title of the page. The title of the page is displayed using HTML commands so that, for example, the first page that was extracted from the query:

Towcester Rugby Club is:

https://www.towsrus.org/<b>Towcester</b> <b>Rugby</b>

<b>Club</b> Mini and Junior Section

where the <b>...</b> tags indicate that the text delineated by them will be displayed in bold.

The case study that I will describe here integrates Google searching with the use of the regular expression API detailed in the previous case study. Its functionality is described below:

1 When the program is first invoked a window is shown to the user.
2 The window contains two buttons used for searching. The first will invoke a Google search. The second will invoke a search based on a regular expression. A third button will be used to quit the application.
3 The search terms for the Google search are typed inside a text field. The regular expression searched is typed in a second text field.
4 When the first button is clicked a combo box is populated with the URLs of the pages that have been retrieved from the Google search.
5 When the URLs from the Google search are returned the user can click any one of them in the combo box. This will result in the URL being highlighted. When the regular expression button is then clicked the program will search for any strings matching a typed regular expression. If the page described by the URL contains a string matching the regular expression then this fact is displayed in a third text field; if no match is found then this is announced in the third text field.
6 When the user wishes to exit the program he or she clicks a third button. An example of the window is shown in Figure.

2172_google search.png

Figure The window created by the second case study

This, then, is an informal specification of the program. It will use facilities detailed here for Google searching, facilities provided by the regular expression library detailed in the previous case study and base Java facilities for creating and displaying visual objects such as buttons and text fields.

The code for this case study is detailed below. It carries out a number of technical functions: 

  • it creates the visual objects that the user will interact with and lays them out in a window;
  • it responds to button-click events; 
  • when responding to a button click it carries out the function associated with this event.

It is important to point out that there is no error processing associated with this code - for example, there is no check that the user has typed in some text for a Google search.

The first code imports all the libraries that are required and declares the visual objects that are to be used:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import com.google.soap.search.*; //Used for Google search
import java.io.*;
import java.net.*;
import org.apache.regexp.*;

public class QueryWindow extends JFrame
implements ActionListener
{
JPanel buttonPanel;
JButton googleButton, regExpButton, quitButton; JComboBox URLList;
JTextField googleText, reText, resultText;
}

The three buttons are used to quit the program, carry out a Google search and refine the Google search by using a regular expression. The class QueryWindow is a JFrame object that listens to action events. A panel is used to hold the three buttons and a combo box is used to hold the URLs that are retrieved when the Google search is invoked. The constructor for the window is shown below:

public QueryWindow()
{
//Set up the frame super(); setSize(400, 300);
setTitle("Google Search-Case Study");

Container cp = getContentPane();

cp.setLayout(new GridLayout(8,1));
//Set up buttons

buttonPanel = new JPanel();

googleButton = new JButton("Google");
regExpButton = new JButton("RegExpression");

quitButton = new JButton("Quit");

buttonPanel.add(googleButton);

buttonPanel.add(quitButton);

buttonPanel.add(regExpButton);
//Set remainder of visual objects
URLList = new JComboBox();
googleText = new JTextField(30); //Length of 30 chars

reText = new JTextField(20); //Length of 20 chars

resultText = new JTextField(10); //Length of 10 chars

cp.add(buttonPanel);
cp.add(URLList);
cp.add(new JLabel("Google Text"));
cp.add(googleText);
cp.add(new JLabel("Regular expression text"));
cp.add(reText);
cp.add(new JLabel("Result of regexp search"));
cp.add(resultText);

//Register all listeners

quitButton.addActionListener(this);

googleButton.addActionListener(this);

regExpButton.addActionListener(this); 

}

It first sets up a window of length 400 pixels and height 300 pixels, sets the title, and creates a container for the visual objects.

The three buttons are created and added to a panel. Then the combo box and three text fields are created. All the visual objects are then added to the frame, including the three labels that identify the text fields.

Finally, the frame is designated as a listener to all of the three buttons.

The code that is executed when any of the buttons is clicked is shown below. It is part of the actionPerformed method, which is implemented in the interface ActionListener.

public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Quit"))
System.exit(0);
if(e.getActionCommand().equals("Google"))
{
//Remove the result of any previous searches URLList.removeAllItems();
//Now do the Google search executeGoogleSearch();
}
if(e.getActionCommand().equals("RegExpression"))
executeReSearch();
}

When the quit button is executed the program exits. When the Google search button is executed the combo box holding the results of the Google search is cleared of any results from a previous search and the method executeGoogleSearch is executed. When the regular expression search button is clicked the method executeReSearch is executed.

The code for executeGoogleSearch is shown below:

public void executeGoogleSearch()
{
//Get the search terms used for the Google search
String searchTerms = googleText.getText();
//Start a search
GoogleSearch search = new GoogleSearch();
//Set the Google search key
search.setKey("Key goes here");
//Set the search string and specify
//that 8 hits are to be returned search.setQueryString(searchTerms); search.setMaxResults(8);

try
{

//Carry out the Google search

GoogleSearchResult searchResult = search.doSearch();
// process the results
GoogleSearchResultElement [] re =
searchResult.getResultElements();
//Add the URLs of the Google search results
//to the JCombo box
for( int i = 0; i < re.length; i++ ) URLList.addItem(re [i].getURL());

}

catch(Exception e)
{
System.out.println("Problem "+e);
}
}

The code first extracts out the terms used for the Google search, creates a new search object, sets the terms for the search and then restricts the number of search items returned to eight.

The code in the try-catch statement then executes the search and places each URL that is found in the combo box.

The code for executeReSearch is shown below:

public void executeReSearch()
{
try
{
//Clear result field resultText.setText("");
//Extract out the selected string
String uRLValue=(String)URLList.getSelectedItem();
//Get the regular expression to be searched for
String regExpression = reText.getText();
//Set up a URL connection
URLConnection connect = (URLConnection)
new URL(uRLValue).openConnection();
//Now create a reader
InputStream is = connect.getInputStream(); BufferedReader bReader = new BufferedReader
(new InputStreamReader(is));
String searchString="";
//Get the contents of the page and place
//each line in searchString String lineRead=""; while(lineRead!=null)
{
lineRead = bReader.readLine();
searchString+=lineRead;
}

//Check whether the string is found in the page
//First create the regular expression that is
//to be searched for
RE searchPattern = new RE(regExpression);
//Now check for a match if(searchPattern.match(searchString))
resultText.setText("Found");
else
resultText.setText("Not found");
//Close down all input objects
bReader.close();
is.close();
}
catch(Exception e)
{
System.out.println ("Error in accessing Web page");
}
}

The code is very similar to that which I described in the previous case study. It first extracts out the URL that the user has selected from the combo box. It then establishes a connection to the web page named by this URL; then a reader is created based on this connection, and the lines of the web page are read and concatenated into the string searchString. This string is then searched for any substrings that match the regular expression found in the text field reText. If there is at least one match then the text field resultText is set to contain the string "Found"; otherwise it is set to contain the string "Not found".

The full code for the case study is shown below:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import com.google.soap.search.*; //Used for Google search
import java.io.*;
import java.net.*;
import org.apache.regexp.*;

public class QueryWindow extends JFrame
implements ActionListener
{
JPanel buttonPanel;
JButton googleButton, regExpButton, quitButton; JComboBox URLList;
JTextField googleText, reText, resultText;

public QueryWindow()

{
//Set up the frame super(); setSize(400, 300);
setTitle("Google Search-Case Study");

Container cp = getContentPane();

cp.setLayout(new GridLayout(8,1));
//Set up buttons

buttonPanel = new JPanel();

googleButton = new JButton("Google");
regExpButton = new JButton("RegExpression");

quitButton = new JButton("Quit");

buttonPanel.add(googleButton);

buttonPanel.add(quitButton);

buttonPanel.add(regExpButton);
//Set remainder of visual objects
URLList = new JComboBox();
googleText = new JTextField(30); //Size of 30 chars

reText = new JTextField(20); //Size of 20 chars

resultText = new JTextField(10); //Size of 10 chars

cp.add(buttonPanel);
cp.add(URLList);
cp.add(new JLabel("Google Text"));
cp.add(googleText);
cp.add(new JLabel("Regular expression text"));
cp.add(reText);
cp.add(new JLabel("Result of regexp search"));
cp.add(resultText);
//Register all listeners quitButton.addActionListener(this); googleButton.addActionListener(this); regExpButton.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Quit")) System.exit(0);
if(e.getActionCommand().equals("Google"))
{
//Remove the result of any previous searches
URLList.removeAllItems();
//Now do the Google search executeGoogleSearch();
}
if(e.getActionCommand().equals("RegExpression"))
executeReSearch();
}

public void executeGoogleSearch()

{
//Get the search terms used for the Google search
String searchTerms = googleText.getText();
//Start a search
GoogleSearch search = new GoogleSearch();
//Set the Google search key search.setKey("Key goes here");
//Set the search string and specify
//that 8 hits are to be returned

search.setQueryString(searchTerms);

search.setMaxResults(8);
try
{
//Carry out the Google search
GoogleSearchResult searchResult = search.doSearch();
// process the results
GoogleSearchResultElement [] re = searchResult.getResultElements();
//Add the URLs of the Google search results
//to the JCombo box
for( int i = 0; i < re.length; i++ )
URLList.addItem(re [i].getURL());
}
catch(Exception e)
{
System.out.println("Problem "+e);
}
}

public void executeReSearch()
{

try
{

//Clear result field resultText.setText("");
//Extract out the selected string
String uRLValue=(String)URLList.getSelectedItem();
//Get the regular expression to be searched for
String regExpression = reText.getText();
//Set up a URL connection
URLConnection connect = (URLConnection)
new URL(uRLValue).openConnection();

//Now create a reader
InputStream is = connect.getInputStream(); BufferedReader bReader = new BufferedReader
(new InputStreamReader(is));
String searchString="";
//Get the contents of the page and place
//each line in searchString String lineRead=""; while(lineRead!=null)

{
lineRead = bReader.readLine();
searchString+=lineRead;
}
//Check whether the string is found in the page
//First create the regular expression that is
//to be searched for
RE searchPattern = new RE(regExpression);
//Now check for a match if(searchPattern.match(searchString))
resultText.setText("Found");

else

resultText.setText("Not found");

//Close down all input objects bReader.close();
is.close();
}
catch(Exception e)
{
System.out.println("Error in accessing Web page");
}
}
}

The class can be simply executed via the class Tester shown below:

public class Tester
{
class Tester
{

public static void main(String [] args)
{
new QueryWindow().setVisible(true);
}
}
}

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your Case study: Interfacing to a search engine homework and assignments? Live Case study: Interfacing to a search engine experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Case study: Interfacing to a search engine homework help, java assignment help and Case study: Interfacing to a search engine projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd