Moteur de recherche en java

billouxs Messages postés 5 Date d'inscription mardi 15 février 2011 Statut Membre Dernière intervention 14 mars 2011 - 14 mars 2011 à 11:25
billouxs Messages postés 5 Date d'inscription mardi 15 février 2011 Statut Membre Dernière intervention 14 mars 2011 - 14 mars 2011 à 19:05
Bonjour a tous ,

je souhaiterai créer un robot qui parcourera récursivement sur N niveaux, une liste d’URL donné en initialisation du programme.
Merci d'avance
Bon courage !
VOici mon code :

package genie;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;
import javax.swing.text.BadLocationException;
import javax.swing.text.EditorKit;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
/**
 *
 */
public class robot {
@SuppressWarnings("unchecked")
public HashMap<String, ArrayList> Index ;
@SuppressWarnings("unchecked")
public robot () {
Index = new HashMap<String, ArrayList> ();

}
@SuppressWarnings("unchecked")
public HashMap<String, ArrayList> indexer(String adresse,int niveau) throws IOException, BadLocationException{		
int cpt =0;
//Charger la page en fonction de l'url
URL url = new URL(adresse);
URLConnection uconnection = url.openConnection();
Reader rd = new InputStreamReader(uconnection.getInputStream());
//lire le document HTML
EditorKit kit = new HTMLEditorKit();
HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
doc.putProperty("IgnoreCharsetDirective", new Boolean(true));
kit.read(rd, doc, 0);
//Parcourir la balise lien
HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
while (it.isValid()) {
SimpleAttributeSet s = (SimpleAttributeSet) it.getAttributes();
String link = (String) s.getAttribute(HTML.Attribute.HREF);
if (link != null && link.startsWith("http") /*&& link.endsWith(".html")*/) {
String titre=(String) s.getAttribute(HTML.Attribute.TITLE);
//découpage des mots du titre du lien pour stockage dans le arraylist
if(titre!=null){
StringTokenizer st = new StringTokenizer (titre," ");
ArrayList<String> tmp = new ArrayList<String> () ; 
while (st.hasMoreTokens()){
String str=st.nextToken();
if(str.length()>2)
tmp.add(str);
}
Index.put(link, tmp);


}
if(cpt<=niveau){
cpt++;
indexer(link,niveau);
}

}


it.next();
}

return Index; 	
}	
@SuppressWarnings("unchecked")
public String rechercher (String recherche){
Iterator<String> it = Index.keySet().iterator();
String adresse="";
ArrayList<String> tmp = new ArrayList<String> ();
StringTokenizer st = new StringTokenizer (recherche," ");
ArrayList <String> mots = new ArrayList <String> ();
while(st.hasMoreTokens()){
String str=st.nextToken();
mots.add(str);

}
while (it.hasNext()){
String current = it.next();
tmp=Index.get(current);
for(int i=0;i<mots.size();i++){
for (int j=0; j<tmp.size();j++){
if(tmp.get(j).equalsIgnoreCase(mots.get(i))){
adresse=adresse +"\n"+current;
}
}
}
}
return adresse;
}	
}

2 réponses

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
14 mars 2011 à 17:15
Quelle est ta question ?

Remarque : les @SuppressWarnings("unchecked") ne sont pas justifiés ici, tu ferais mieux de régler le problème
0
billouxs Messages postés 5 Date d'inscription mardi 15 février 2011 Statut Membre Dernière intervention 14 mars 2011
14 mars 2011 à 19:05
Dsl si je n'étais pas clair ,

Aufait j'ai créer ce robot la et je souhaiterai qu'il parcourt récursivement sur N niveau , une liste d'URL !Genre le nombre de page que doit parcourir mon moteur de recherche !jespere que j'ai été plus clair Merci !

package genie;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;
import javax.swing.text.BadLocationException;
import javax.swing.text.EditorKit;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
/**
 *
 */
public class robot {

public HashMap<String, ArrayList> Index ;

public robot () {
Index = new HashMap<String, ArrayList> ();

}

public HashMap<String, ArrayList> indexer(String adresse,int niveau) throws IOException, BadLocationException{		
int cpt =0;
//Charger la page en fonction de l'url
URL url = new URL(adresse);
URLConnection uconnection = url.openConnection();
Reader rd = new InputStreamReader(uconnection.getInputStream());
//lire le document HTML
EditorKit kit = new HTMLEditorKit();
HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
doc.putProperty("IgnoreCharsetDirective", new Boolean(true));
kit.read(rd, doc, 0);
//Parcourir la balise lien
HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
while (it.isValid()) {
SimpleAttributeSet s = (SimpleAttributeSet) it.getAttributes();
String link = (String) s.getAttribute(HTML.Attribute.HREF);
if (link != null && link.startsWith("http") /*&& link.endsWith(".html")*/) {
String titre=(String) s.getAttribute(HTML.Attribute.TITLE);
//découpage des mots du titre du lien pour stockage dans le arraylist
if(titre!=null){
StringTokenizer st = new StringTokenizer (titre," ");
ArrayList<String> tmp = new ArrayList<String> () ; 
while (st.hasMoreTokens()){
String str=st.nextToken();
if(str.length()>2)
tmp.add(str);
}
Index.put(link, tmp);


}
if(cpt<=niveau){
cpt++;
indexer(link,niveau);
}

}


it.next();
}

return Index; 	
}	

public String rechercher (String recherche){
Iterator<String> it = Index.keySet().iterator();
String adresse="";
ArrayList<String> tmp = new ArrayList<String> ();
StringTokenizer st = new StringTokenizer (recherche," ");
ArrayList <String> mots = new ArrayList <String> ();
while(st.hasMoreTokens()){
String str=st.nextToken();
mots.add(str);

}
while (it.hasNext()){
String current = it.next();
tmp=Index.get(current);
for(int i=0;i<mots.size();i++){
for (int j=0; j<tmp.size();j++){
if(tmp.get(j).equalsIgnoreCase(mots.get(i))){
adresse=adresse +"\n"+current;
}
}
}
}
return adresse;
}	
}
0
Rejoignez-nous