import java.util.*;
import AntiDico.*;
import Dico.*;
import Collocation.*;
import Lemmatiseur.*;
import ListePonderee.*;
import OutilTexte.*;
import OutilWeb.*;
import someutils.*;
import Graphe.*;
import graphe.*;
import java.io.*;

public class cooccurences{

    public static void main(String [] arg){
	int NBMOTMIN = 5, NBMOTMAX = 25;
	Date a = new Date();
	AntiDico antidico = new AntiDico();
	antidico.fichier2dico("ressources/antidico.txt");
	Dico dico = new Dico();
	dico.fichier2dico("ressources/dico.txt");
	Lemmatiseur lemmatiseur = new Lemmatiseur();
	lemmatiseur.fichier2dico("ressources/lemme.txt");
	String mot = "";
	
	for(int i=0; i<arg.length; i++){
	    if(i != 0)
		mot += "%20";
	    mot += arg[i];
	}

	System.out.println("------");
	System.out.println(mot);
	System.out.println("------");

	Vector v = new Vector();

	try{	
	    String fichier = mot.replaceAll("[^a-zA-Z0-9]+", "_");
	    File f = new File("base/"+fichier+"/txt/");
	    String[] fichiers = f.list();
	    String ligne;
	    String texte;
	    
	    for(int i=0;i<fichiers.length;i++){
		texte = "";
		BufferedReader in = new BufferedReader(new FileReader("base/"+fichier+"/txt/"+fichiers[i]));
		
		while((ligne = in.readLine()) != null)
		    texte += ligne;

		v.addElement(texte);
	    }
	}   
	catch(Exception e){System.out.println(e);}

    	System.out.println("Lemmatisation des listes de cooccurences possibles en cours");
	Vector w = new Vector();
	Vector x;
	for(int i=0; i<v.size(); i++){
	    String s = (String)v.elementAt(i);
	    //  s = OutilTexte.fenetre(mot, s, 20);
	    x = OutilTexte.identifieEntite(mot, s, dico);
	    x = OutilTexte.lemmatisation(x, lemmatiseur);
	    x = OutilTexte.stopListe(x, antidico);
	    w.addElement(x);
	}
	System.out.println("Lemmatisation des listes de cooccurences termine");
	a = new Date();
	System.out.println("---"+a+"---");

	System.out.println(w);	

	// nbHit(mot)/Total(nbHit(X))
	System.out.println("Pondration en cours");
	for(int i=0; i<w.size(); i++){
	    OutilTexte.ponderation((Vector)w.elementAt(i), dico);
	}
	System.out.println("Pondration termine");
	a = new Date();
	System.out.println("---"+a+"---");

	
	System.out.println("Epuration en cours");
	for(int i=0; i<w.size(); i++){
	    OutilTexte.epuration((Vector)w.elementAt(i), dico, 60000000); //nb page francophones a priori
	}
	System.out.println("Epuration termine");
	a = new Date();
	System.out.println("---"+a+"---");
	
	System.out.println("Suppression des mots n'apparaissant pas dans au moins 4 % des pages");
	OutilTexte.presence(w, 0.04f);
	System.out.println("Suppression termine");
	a = new Date();
	System.out.println("---"+a+"---");
	
	for(int i=w.size()-1; i>=0; i--){
	    if(((Vector)w.elementAt(i)).size() < 1){
		w.removeElementAt(i);
	    }
	}
	System.out.println(w);

	System.out.println("fusion");
	int av = 0, ap = 0;
	do{
	    av = w.size();
	    w =  ListePonderee.fusion(w, 0.49f);	    
	    ap = w.size();
	    System.out.println(av+"#"+ap);
	    }
	while(ap < av);
	System.out.println("fusion termine");

	a = new Date();
	System.out.println("---"+a+"---");
	System.out.println(w);	
	System.out.println("Suppression des listes trop courtes (ie. infrieure  "+NBMOTMIN+" mots).");	
	for(int i=w.size()-1; i>=0; i--){
	    if(((Vector)w.elementAt(i)).size() < NBMOTMIN){
		w.removeElementAt(i);

	    }
	}
 	System.out.println("Suppression des listes trop courtes termines");
	a = new Date();
	System.out.println("---"+a+"---");
	
	for(int i=0; i<w.size(); i++){
	    Vector ww = (Vector)w.elementAt(i);
	    ListePonderee.tri(ww);
	}
		  
	for(int i=0; i<w.size(); i++){
	    Vector ww = (Vector)w.elementAt(i);
	    int k = 0;
	    System.out.println("--- SENS ---");
	    while(k<ww.size()){
		System.out.println(k+"#@ "+(Couple)ww.elementAt(k));
		k++;
	    }
	}	    
	

	int HITMIN = 750;
	//ajout
	for(int i=0; i<w.size(); i++){
	    Vector ww = (Vector)w.elementAt(i);
	    int k = 0;
	    while(k<ww.size()){
		String mot1 = ((Couple)ww.elementAt(k)).getMot();
		System.out.println(mot1);
		Collocation collocations = new Collocation(mot1, lemmatiseur, antidico);
		for(int j=0; j<v.size(); j++){		    
		    collocations.rechercheCollocations((String)v.elementAt(j), HITMIN);
		}
		k++;
		System.out.println(collocations.collocations);
	    }
	}



	
    }
}

