nadjet1807
Messages postés5Date d'inscriptionlundi 14 novembre 2011StatutMembreDernière intervention10 mars 2012
-
10 mars 2012 à 10:54
shaft_amine
Messages postés58Date d'inscriptionmercredi 30 juillet 2008StatutMembreDernière intervention 1 février 2018
-
18 mars 2012 à 09:01
bonjour.j'ai un algorithme apriori mais ne fonctione pas .
//---- apriori.java
//---- input file need:
//---- 1. config.txt
//---- four lines, each line a integer
//---- item number, transaction number , minsup
//---- 2. transa.txt
package apriori_algo ;
import java.io.*;
import java.lang.Integer.* ;
import java.lang.Object.* ;
import java.util.*;
import org.jdom.*;
import org.jdom.output.*;
import java.lang.String.*;
//-------------------------------------------------------------
// Class Name : apriori
// Purpose : main program class
//-------------------------------------------------------------
public class apriori_algo {
public static void main(String[] args) throws IOException {
//-------------------------------------------------------------
// Class Name : aprioriProcess
// Purpose : main processing class
//-------------------------------------------------------------
class aprioriProcess {
//Nous allons commencer notre arborescence en crant la racine XML
//qui sera ici "personnes".
static Element racine ;//= new Element("Apriori");
//On cre un nouveau Document JDOM bas sur la racine que l'on vient de crer
static org.jdom.Document document ;// = new Document(racine);
private final int HT=1; // state of tree node (hash table or
private final int IL=2; // itemset list)
int N; // total item #
int M; // total transaction #
int minsup ;
Vector largeitemset = new Vector() ;
Vector candidate = new Vector() ;
Vector Support = new Vector() ;
//-------------------------------------------------------------
// Class Name : candidateelement
// Purpose : object that will be stored in Vector candidate
// : include 2 item
// : a hash tree and a candidate list
//-------------------------------------------------------------
class candidateelement {
hashtreenode htroot;
Vector candlist;
}
//-------------------------------------------------------------
// Class Name : hashtreenode
// Purpose : node of hash tree
//-------------------------------------------------------------
class hashtreenode {
int nodeattr; // IL or HT
int depth;
Hashtable ht;
Vector itemsetlist;
//-------------------------------------------------------------
// Class Name : itemsetnode
// Purpose : node of itemset
//-------------------------------------------------------------
class itemsetnode {
String itemset;
int counter;
public itemsetnode(String s1,int i1) {
itemset=new String(s1);
counter=i1;
}
public itemsetnode() {
itemset=new String();
counter=0;
}
//-------------------------------------------------------------
// Method Name: printhashtree
// Purpose : print the whole hash tree
// Parameter : htn is a hashtreenode (when other method call this method,it is the root)
// : transa : special transaction with all items occurr in it.
// : a : recursive depth
// Return :
//-------------------------------------------------------------
public void printhashtree(hashtreenode htn,String transa,int a) {
if (htn.nodeattr == IL ) {
System.out.println("Node is an itemset list");
System.out.println(" depth :<"+htn.depth+">");
System.out.println(" iteset:<"+htn.itemsetlist+">");
}
else { // HT
System.out.println("Node is a hashtable");
if (htn.ht==null)
return;
for (int b=a+1;b<=N;b++)
if (htn.ht.containsKey(Integer.toString(getitemat(b,transa)))) {
System.out.println(" key:<"+getitemat(b,transa));
printhashtree((hashtreenode)htn.ht.get(Integer.toString(getitemat(b,transa))),transa,b);
}
}
}
//-------------------------------------------------------------
// Method Name: getconfig
// Purpose : open file config.txt
// : get the total number of items of transaction file
// : and the total number of transactions
// : and minsup
//-------------------------------------------------------------
public void getconfig() throws IOException {
FileInputStream file_in;
DataInputStream data_in;
String oneline=new String();
int i=0;
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
String response = "";
System.out.println("Appuyer sur 'C' changer la configuration and le fichier de transaction par défault");
System.out.print("Ou sur n'import quelle touche pour continuer. ");
try {
response = reader.readLine();
} catch (Exception e) {
System.out.println(e);
}
int res=response.compareTo("C") * response.compareTo("c");
//-------------------------------------------------------------
// Method Name: getitemat
// Purpose : get an item from an itemset
// : get the total number of items of transaction file
// Parameter : int i : i-th item ; itemset : string itemset
// Return : int : the item at i-th in the itemset
//-------------------------------------------------------------
public int getitemat(int i,String itemset) {
String str1=new String(itemset);
StringTokenizer st=new StringTokenizer(itemset);
int j;
if (i > st.countTokens())
System.out.println("eRRor! in getitemat, !!!!");
for (j=1;j<=i;j++)
str1=st.nextToken();
return(Integer.valueOf(str1).intValue());
}
//-------------------------------------------------------------
// Method Name: itesetsize
// Purpose : get item number of an itemset
// Parameter : itemset : string itemset
// Return : int : the number of item of the itemset
//-------------------------------------------------------------
public int itemsetsize(String itemset) {
StringTokenizer st=new StringTokenizer(itemset);
return st.countTokens();
}
//-------------------------------------------------------------
// Method Name: gensubset
// Purpose : generate all subset given an itemset
// Parameter : itemset
// Return : a string contains all subset deliminated by ","
// : e.g. "1 2,1 3,2 3" is subset of "1 2 3"
//-------------------------------------------------------------
public String gensubset(String itemset) {
int len=itemsetsize(itemset);
int i,j;
String str1;
String str2=new String();
String str3=new String();
if (len==1)
return null;
for (i=1;i<=len;i++) {
StringTokenizer st=new StringTokenizer(itemset);
str1=new String();
for (j=1;j=minsup
// Parameter : int n : n-itemset
// Return :
//-------------------------------------------------------------
public void createlargeitemset(int n) {
Vector candlist=new Vector();
Vector lis=new Vector(); //large item set
hashtreenode htn=new hashtreenode();
int i;
// System.out.println("Generating "+n+"-large item set ....");
candlist=((candidateelement)candidate.elementAt(n-1)).candlist;
htn=((candidateelement)candidate.elementAt(n-1)).htroot;
getlargehash(0,htn,fullitemset,lis);
largeitemset.addElement(lis);
} // end public void createlargeitemset(int n)
//-------------------------------------------------------------
// Method Name: getlargehash
// Purpose : recursively traverse candidate hash tree
// : to find all large itemset
// Parameter : htnf is a hashtreenode (when other method call this method,it is the root)
// : cand : candidate itemset string
// : int i : recursive depth
// : Vector lis : Vector that stores large itemsets
// Return :
//-------------------------------------------------------------
public void getlargehash(int i,hashtreenode htnf,String transa,Vector lis) {
Vector tempvec=new Vector();
int j;
if (htnf.nodeattr==IL) {
tempvec=htnf.itemsetlist;
for (j=1;j<=tempvec.size();j++)
if (((itemsetnode)tempvec.elementAt(j-1)).counter >= ((minsup * M) / 100))
{
lis.addElement( ((itemsetnode)tempvec.elementAt(j-1)).itemset ) ;
Support.addElement(((itemsetnode)tempvec.elementAt(j-1)).counter ) ;
}
}
else {
if (htnf.ht==null)
return;
for (int b=i+1;b<=N;b++)
{
if (htnf.ht.containsKey(Integer.toString(getitemat(b,transa))))
getlargehash(b,(hashtreenode)htnf.ht.get(Integer.toString(getitemat(b,transa))),transa,lis);
}
}
}
//-------------------------------------------------------------
// Method Name: transatraverse
// Purpose : read each transaction, traverse hashtree,
// incrment approporiate itemset counter.
// Parameter : int n : n-itemset
// Return :
//-------------------------------------------------------------
public void transatraverse(int n) {
FileInputStream file_in;
DataInputStream data_in;
String oneline=new String();
int i=0,j=0,len=0;
String transa;
hashtreenode htn=new hashtreenode();
StringTokenizer st;
String str0;
int numRead=0;
//System.out.println("Traverse "+n+"-candidate hashtree ... ");
htn=((candidateelement)candidate.elementAt(n-1)).htroot;
try {
file_in = new FileInputStream(transafile);
data_in = new DataInputStream(file_in);
racine = new Element("Apriori");
document = new Document(racine);
Element Frequent = new Element("Frequent");
racine.addContent(Frequent);
String h= String.valueOf(k);
Attribute Niveau = new Attribute("Niveau",h);
Frequent.setAttribute(Niveau);
int o=1;
while (sttt.hasMoreTokens()) {
String hh = String.valueOf(o);
Element itemfreqent = new Element("itemfreqent");
Frequent.addContent(itemfreqent);
Attribute Num = new Attribute("Num",hh);
Attribute support = new Attribute("Support",String.valueOf(Support.elementAt(o-1)));
itemfreqent.setAttribute(Num);
itemfreqent.setAttribute(support);
String fer =sttt.nextToken().toString();
itemfreqent.setText(fer);
o++;
}
enregistre("Frequents du Niveau "+k+".xml");
//affiche();
Support = new Vector() ;
}
System.out.println("Execution time is: "+((s2-s1)/1000) + " seconds.");
System.out.println("End.");
//affiche();
//enregistre("Itemfrequent.xml");
}
//==============================================================================
//
// Afficher le contenue du fichier XML
//
//==============================================================================
static void affiche()
{
try
{
//On utilise ici un affichage classique avec getPrettyFormat()
XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
sortie.output(document, System.out);
}
catch (java.io.IOException e){}
}
//==============================================================================
//
// Enregistrer dans le fichier XML
//
//==============================================================================
static void enregistre(String fichier)
{
try
{
//On utilise ici un affichage classique avec getPrettyFormat()
XMLOutputter sortie = new XMLOutputter(Format.getPrettyFormat());
//Remarquez qu'il suffit simplement de crer une instance de FileOutputStream
//avec en argument le nom du fichier pour effectuer la srialisation.
sortie.output(document, new FileOutputStream(fichier));
}
catch (java.io.IOException e){}
}
cs_Julien39
Messages postés6414Date d'inscriptionmardi 8 mars 2005StatutModérateurDernière intervention29 juillet 2020371 10 mars 2012 à 12:16
Bonjour,
Est ce que tu penses sincèrement que quelqu'un va lire tout ton programme pour détecter l'erreur ?
Si tu ne fais pas l'effort de décrire ce qui te poses problème, tu n'auras pas de réponse. Essaye d'extraire les lignes qui te posent problème et de nous dire ce qui ne va pas : exception, fonctionnement inattendu, problème de rafraichissent d'une fenêtre ?
cs_Julien39
Messages postés6414Date d'inscriptionmardi 8 mars 2005StatutModérateurDernière intervention29 juillet 2020371 10 mars 2012 à 15:29
Allez, encore un effort, peux tu nous recopier la trace de ton exception et également les lignes qui sont à l'origine de la première exception qui est levée (celle qui apparait en premier).
shaft_amine
Messages postés58Date d'inscriptionmercredi 30 juillet 2008StatutMembreDernière intervention 1 février 2018 18 mars 2012 à 09:01
C'est mon algorithme nadjet1807 ca fonctionne bien il faut d'abord bien lire ce que j'ai écri concernant l'API jdom pour créer les fichier XML contenet les itemsets fréquent