📄 main.cpp
字号:
/*=============================================================================|| Description: This program is an implementation of Apriori| learning algorithm.|| Usage: webminer infofile|| infofile file containing informations about features and| the test dataset (eg, weather.id3)|=============================================================================*/#include <strings.h>#include <stdio.h>#include <fcntl.h>#include <stdlib.h>#include <math.h>#include <string.h>#include "apriori.h"#include "rule.h"#include "util.h"int pagenum;ItemSets *traindata = NULL;Apriori *m_apriori;AssociationRule *m_rules;ItemSets *readin(char *infofile){ FILE *fp; char line[4096]; char *t, *s; int pageno, i; ItemSets *result; ItemSet *psession; if((fp = fopen(infofile, "rt")) == NULL) { printf(" Cannot open the data file %s !\n", infofile); exit(-9); } pagenum = -1; result = new ItemSets(); while(fgets(line, 4096, fp) != NULL) { psession = new ItemSet(); if (strchr(line, ',') != NULL) { s = (char *) new char[strlen(line) + 1]; strcpy(s, line); /* can't strtok arg1 directly in case it's const */ if ((t = strtok(s, ",")) == NULL) { delete s; break; } for ( ; t != NULL; t = strtok((char *)NULL, ",")) { pageno = atoi(t); if(pagenum < pageno) pagenum = pageno; for(i = 0; i < psession->count; i++) if(pageno < psession->itemof(i)) break; psession->insert(i, pageno); } delete s; } else { pageno = atoi(line); if(pagenum < pageno) pagenum = pageno; psession->add(pageno); } result->add(psession); delete psession; } fclose(fp); pagenum++; return(result);}main( int argc, char *argv[] ){ int traincnt = 0, i, j; double interestmax; char datafile[100], rules[100]; /* Get command line arguments */ if ( argc < 2 ) { printf("Usage : apriori datafile \n\n "); exit(-9); } strcpy(datafile, argv[1]); for(i = strlen(datafile)-1; i >= 0; i--) if(datafile[i] == '.') { datafile[i] = '\0'; break; } strcpy(rules, datafile); strcat(rules, ".rules"); // Read in the structure of the web site from a map file printf("\nRead in data from %s ... \n", argv[1]); traindata = readin(argv[1]); printf(" %d sessions have been read in !\n\n", traindata->count); printf("\nTo find all the large itemsets ... \n"); m_apriori = new Apriori(); m_rules = new AssociationRule(); m_apriori->setsupport(0.01); m_rules->setconfidence(0.5); // Finding large Itemsets from training data m_apriori->FindLargeItemSets(traindata); // Association Rules from extracted large item sets m_rules->m_LargeItemSets = m_apriori->m_Ls; printf("\nTo generate all the rules ... \n"); m_rules->genrules(); m_rules->writeto(rules); printf(" %d rules have been written to %s \n", m_rules->m_numRules, rules); // Clear off delete m_apriori; delete m_rules; delete traindata;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -