Google Search API
=================
Google Search API give us an occasion to access to google search results. These results will be given in the JSON (JavaScript Object Notation) format.JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
This development needs an input of 7 librairies which are:
- json-lib-2.4-jdk15.jar
- ezmorph-1.0.2.jar
- commons-lang-2.6.jar
- commons-lang-2.6-javadoc.jar
- commons-lang-2.6-sources.jar
- json-simple-1.1-bundle.jar
- json-taglib-0.4.1.jar
These librairies needs to be all used at same time or some exceptions might occure.
Here are the codes to get the number of url in which the searched name is, and the text in the web page which contains the name.
For more informations, these are the links:
- http://code.google.com/apis/websearch/docs/reference.html#_intro_fonje
- http://www.json.org/
=================
Google Search API give us an occasion to access to google search results. These results will be given in the JSON (JavaScript Object Notation) format.JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
This development needs an input of 7 librairies which are:
- json-lib-2.4-jdk15.jar
- ezmorph-1.0.2.jar
- commons-lang-2.6.jar
- commons-lang-2.6-javadoc.jar
- commons-lang-2.6-sources.jar
- json-simple-1.1-bundle.jar
- json-taglib-0.4.1.jar
These librairies needs to be all used at same time or some exceptions might occure.
Here are the codes to get the number of url in which the searched name is, and the text in the web page which contains the name.
package com.google.pack1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import atg.taglib.json.util.JSONArray;
import atg.taglib.json.util.JSONException;
import atg.taglib.json.util.JSONObject;
import atg.taglib.json.util.JSONTokener;
/**
* This class is used to test some algorithm in the POLYPHONET:An Advanced Social Network paper
* @author Pascal
*/
public class Polyphonet {
//Is set for the JSAI case
public static int k = 30;
//co- occurence index
public float f = 0;
public String searchURL = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=8&hl=en&q=";
public static void main(String[] args) {
Polyphonet p = new Polyphonet();
System.out.println(p.coocFunction(80, 20, 100));
p.googleHit("Bujumbura");
p.googleTop("Bujumbura", k);
}
/**
* This method is called to calculate the co-occurence index f
* @param nx: result for x name
* @param ny: result for y name
* @param nxy: result for x and y name
* @return f
*/
public float coocFunction(int nx,int ny,int nxy){
float min=0;
//Determine the minimal value
if(nx>ny){
min = ny;
}else{
min = nx;
}
//calculate the f function
if(nx>k && ny>k){
f = nxy/min;
}else{
f=0;
}
return f;
}
/**
*
* @param name: searched name
* @return: Number of hits retrieved by a given query
*/
public int googleHit(String name){
name = name.replace(" ", "%20");
String sURL = searchURL +name;
try {
//create a url object
URL url = new URL(sURL);
System.out.println("the url is: "+sURL);
//create a url connection object
URLConnection conn = url.openConnection();
System.out.println("The url is well connected");
//get the input stream
InputStream isr = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(isr));
//result of the search
String result = br.readLine();
System.out.println(result);
//create a JSON object
JSONObject jsResult = new JSONObject(result).getJSONObject("responseData").getJSONObject("cursor");
System.out.println(jsResult.toString());
//get the number of result
int n = jsResult.getInt("estimatedResultCount");
System.out.println("The estimated result is: "+n);
return n;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Please verify the URL "+sURL);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("The url didn't succeed to get connected, please try again");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
/**
* This method will search the text in the url found
* @param name: searched name
* @param k: Quantity or number set for the JSAI case in research
* @return: D arrayList which contain all the found text
*/
public ArrayList<String> googleTop(String name, int k){
name = name.replace(" ", "%20");
String sURL = searchURL +name;
try {
//create a url object
URL url = new URL(sURL);
System.out.println("the url is: "+sURL);
//create a url connection object
URLConnection conn = url.openConnection();
System.out.println("The url is well connected");
//get the input stream
InputStream isr = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(isr));
//result of the search
String result = br.readLine();
//create a JSON object
JSONArray jsaResult = new JSONObject(result).getJSONObject("responseData").getJSONArray("results");
//create an arrayList
ArrayList<String> D = new ArrayList<String>();
//get the content of k first results
for(int i = 0; i<jsaResult.size()&&i<k; i++){
//Get each index as an object
JSONObject jsCont = jsaResult.getJSONObject(i);
String content = jsCont.getString("content");
System.out.println("The content is "+ content);
D.add(content);
}
return D;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Please verify the URL "+sURL);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("The url didn't succeed to get connected, please try again");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
For more informations, these are the links:
- http://code.google.com/apis/websearch/docs/reference.html#_intro_fonje
- http://www.json.org/