#ifndef structES_H #define structES_H #include #include #include //gia th getch() #include #include //gia thn infile,ifstream, #include #include #include #include #using void returnCurrentDirectory(char buffer[]) { /* Get the current working directory: */ if( _getcwd( buffer, _MAX_PATH ) == NULL ) perror( "_getcwd error" ); } using namespace System; using namespace System::IO; /* Pairnei ena char[] to metatrepei se String* kai to epistrefei */ String* convertToString(char buffer[]) { String* stringVal; stringVal = System::Convert::ToString(buffer); return stringVal; } using namespace std; /* klash me mia synarthsh pou elegxei an ena string einai arithmos */ struct nondigit { public: bool operator() (char c) {return !isdigit(c);} }; /* klash pou antistoixei se mia leksh */ struct word_freq { public: /* h leksh */ string word; /* o arithmos emfanisewn ths lekshs */ int count; word_freq* next; word_freq* prev; int doc_appearence; /* Methodos egkatastashs me ena orisma */ word_freq(string w) { word=w; count=0; doc_appearence=0; next=NULL; prev=NULL; } }; struct word_appear { public: /* h leksh */ string word; /* o arithmos emfanisewn ths lekshs */ int count; /* arithmos emfanisewn ths lekshs se olo to arxeio */ bool flag; int document_appearence; /*arithmos twn arthrwn pou emfanizetai h leksh */ word_appear* next; /* Methodos egkatastashs me ena orisma */ word_appear(string w) { word=w; count=0; next=NULL; flag=true; document_appearence=0; } }; struct remaining_words { private: word_freq* head; word_freq* tail; /* o synolikos arithos twn stoixeiwn ths listas, dhl oi lekseis */ int count; public: remaining_words() { head=NULL; tail=NULL; count=0; } /* prosthetei mia leskh sth lista */ void add(string word,int count); /* afairei ena stoixeio apo th lista */ void pop(); /* epistrefei true an einai adeia h lista */ bool IsEmpty(); /* adeiazei th lista me diadoxika pop */ void EmptyList(); bool IsReamainingWord(string word,int *app_count); int RemainingWordsReturnCount(); void CopyList(string * WordsArray); }; void remaining_words::add(string word,int count) { word_freq* ptr=head; (this->count)++;/*afkshsh tou metrhth twn leksewn ths listas kata 1 */ word_freq* new_wf=new word_freq(word); if(head==NULL) //den iparxei kanena stoixeio sth lista { head=new_wf; //tithetai ws prwto stoixeio ths listas new_wf->next=NULL; tail=new_wf; //afou einai to monadiko stoixeio ths listas tithetai kai ws teleftaio new_wf->prev=NULL; } else { word_freq* ptr=head; while (ptr!=NULL)//gia olh th lista me tis lekseis { if(ptr->word>new_wf->word) //an exei vrethei hdh h oxi { if(ptr==head) { head=new_wf; ptr->prev=head; head->next=ptr; } else { ptr->prev->next=new_wf; new_wf->prev=ptr->prev; new_wf->next=ptr; ptr->prev=new_wf; } goto end; } ptr=ptr->next; } word_freq* temp; temp=tail; temp->next=new_wf; //prostithetai sto telos tis listas new_wf->prev=tail; tail=new_wf; //tithetai ws teleftaio stoixeio tis listas } end: new_wf->count=count; //o arithmos emfanisewn ths lekshs sto arxeio tithetai iso me count */ } //eksagei to prwto stoixeio ths listas void remaining_words::pop() { //h lista den einai adeia if(head!=NULL) { //iparxoun toulaxiston 3 stoixeia sth lista if((head->next!=NULL)&&(head->next->next!= NULL)) { word_freq *temp1; word_freq *temp2; temp1=head; temp2=head->next; head=temp2; delete temp1; } //iparxoun 2 to poly stoixeia sth lista else //!(head->next!=NULL)&&(head->next->next!= NULL) { //iparxoun 2 akrivws stoixeia if(head->next!=NULL) { word_freq *temp1; temp1=head; head->next=NULL; head=tail; delete temp1; } //iparxei mono 1 stoixeio else //!(head->next!=NULL) { word_freq* temp1; temp1=head; head=NULL; tail=NULL; } } } } //Epistrefei True an einai adeia h lista alliws epistrefei False bool remaining_words::IsEmpty() { if( head == NULL) return true; return false; } void remaining_words::EmptyList() { while(!IsEmpty()) pop(); count=0; } bool remaining_words::IsReamainingWord(string word2,int *app_count) { word_freq* ptr=head; while (ptr!=NULL)//gia olh th lista me tis lekseis { if(ptr->word==word2) //an exei vrethei hdh h oxi { (*app_count)=ptr->count; //de vrethike pros to paron thn elegxoume me thn epomenh lesk ths listas return true; } if(ptr->word>word2) goto end; ptr=ptr->next; } end: return false; } int remaining_words::RemainingWordsReturnCount() { return count; } void remaining_words::CopyList(string * WordsArray) { word_freq* ptr=head; int i=0; while (ptr!=NULL)//gia olh th lista me tis lekseis { WordsArray[i]=ptr->word; i++; ptr=ptr->next; } } /* h syndedemenh lista me stoixeia tis lekseis */ struct freq_count { private: word_freq* head; word_freq* tail; double** RemainingWords; string* WordsArray; int number_of_words; /* o synolikos arithos twn stoixeiwn ths listas, dhl oi lekseis */ int count; public: freq_count() { head=NULL; tail=NULL; count=0; RemainingWords= NULL; WordsArray=NULL; } /* prosthetei mia leskh sth lista */ void add(string word,int app_count); /* grafei tis lekseis ths listas sto arxeio x */ void print_out(char x[],int y,string title,int documents); /* afairei ena stoixeio apo th lista */ void pop(); /* epistrefei true an einai adeia h lista */ bool IsEmpty(); /* adeiazei th lista me diadoxika pop */ void EmptyList(); void MakeArray(int documents,int words); void DestroyArray(int words,int documents); void InitializeArray(int documents,int words); void MakeWordsArray(remaining_words rw); int BinarySearch(string key); void WriteTable(char x[],int words,int documents); string doubleToString(double num); }; void freq_count::add(string word,int app_count) { word_freq* ptr=head; /* krataei an h leksh yparxei hdh sth lista */ int found=1; count++; //+1 oi synolikes lekseis toy keimenou while (ptr!=NULL && found!=0)//gia olh th lista me tis lekseis { found=(word.compare(ptr->word)); //an exei vrethei hdh h oxi if (found!=0) ptr=ptr->next; //de vrethike pros to paron thn elegxoume me thn epomenh lesk ths listas } if (found==0) //thn exoume hdh vrei th leksh (ptr->count)++; //ara afksanoume ton arithmo emfanisewn ths kata 1 else //th vriskoume gia prwth fora { word_freq* new_wf=new word_freq(word); if(head==NULL) //den iparxei kanena stoixeio sth lista { head=new_wf; //tithetai ws prwto stoixeio ths listas new_wf->next=NULL; tail=new_wf; //afou einai to monadiko stoixeio ths listas tithetai kai ws teleftaio } else { word_freq* temp; temp=tail; temp->next=new_wf; //prostithetai sto telos tis listas tail=new_wf; //tithetai ws teleftaio stoixeio tis listas } (new_wf->count)++; //o arithmos emfanisewn ths lekshs sto arthro +1 */ new_wf->doc_appearence=app_count; } } //Epistrefei True an einai adeia h lista alliws epistrefei False bool freq_count::IsEmpty() { if( head == NULL) return true; return false; } //eksagei to prwto stoixeio ths listas void freq_count::pop() { //h lista den einai adeia if(head!=NULL) { //iparxoun toulaxiston 3 stoixeia sth lista if((head->next!=NULL)&&(head->next->next!= NULL)) { word_freq *temp1; word_freq *temp2; temp1=head; temp2=head->next; head=temp2; delete temp1; } //iparxoun 2 to poly stoixeia sth lista else //!(head->next!=NULL)&&(head->next->next!= NULL) { //iparxoun 2 akrivws stoixeia if(head->next!=NULL) { word_freq *temp1; temp1=head; head->next=NULL; head=tail; delete temp1; } //iparxei mono 1 stoixeio else //!(head->next!=NULL) { word_freq* temp1; temp1=head; head=NULL; tail=NULL; } } } } void freq_count::EmptyList() { count=0; while(!IsEmpty()) pop(); } void freq_count::MakeArray(int documents,int words) { number_of_words=words; RemainingWords= new double *[words]; for(int i=0;iWrite(y); streamW->Write("\n\n"); streamW2->Write(y-1); streamW2->Write(title.c_str()); streamW2->Write("\n\n"); int index; while ((ptr!=NULL)&&(ptr!=tail)) { index=BinarySearch(ptr->word); temp=(double)ptr->doc_appearence; temp2=documents/temp; temp3=(double)ptr->count; temp4=temp3*log10(temp2); RemainingWords[index][y-1]=temp4; streamW->Write(ptr->word.c_str()); streamW->Write(" "); streamW->Write(ptr->count); streamW->Write(" "); streamW->Write(ptr->doc_appearence); streamW->Write("\n"); ptr=ptr->next; //proxwrame sthn epomenh leksh ths listas } streamW->Write("\n\n"); streamW->Close(); streamW2->Close(); } void freq_count::WriteTable(char x[],int words,int documents) { char SubDirectoryChar2[_MAX_PATH]; using namespace std; using namespace System; using namespace System::IO; returnCurrentDirectory(SubDirectoryChar2); strcat_s(SubDirectoryChar2,"\\articles\\"); char Rows[_MAX_PATH]; using namespace std; using namespace System; using namespace System::IO; returnCurrentDirectory(SubDirectoryChar2); strcat_s(SubDirectoryChar2,"\\articles\\"); strcpy_s(Rows,SubDirectoryChar2); char filename[100]; strcpy_s(filename,"Rows"); strcat_s(filename,x); strcat_s(Rows,filename); strcat_s(SubDirectoryChar2,"TableFile"); strcat_s(SubDirectoryChar2,x); String* path=convertToString(SubDirectoryChar2); String* path2=convertToString(Rows); StreamWriter* streamW; StreamWriter* streamW2; // Create a file to write to. streamW = File::CreateText(path); // Create a file to write to. streamW2 = File::CreateText(path2); string temp; string temp2; string temp3; for(int i=0;iWrite(temp.c_str()); if(j!=(documents-1)) streamW->Write(" "); } if(i!=(words-1)) streamW->Write("\n"); streamW2->Write(WordsArray[i].c_str()); streamW2->Write("\n"); } streamW->Close(); streamW2->Close(); } string freq_count::doubleToString(double num) { using namespace std; using std::string;char buf[80]; sprintf_s(buf,"%lf", num); string ret = buf; ret = ret.substr(0,ret.find_last_not_of('0')+1); if(ret[ret.length()-1] == '.') ret = ret.substr(0,ret.length()-1); return ret; } int freq_count::BinarySearch(string key) { // function: // Searches sortedArray[first]..sortedArray[last] for key. // returns: index of the matching element if it finds key, // otherwise -(index where it could be inserted)-1. // parameters: // sortedArray in array of sorted (ascending) values. // first, last in lower and upper subscript bounds // key in value to search for. // returns: // index of key, or -insertion_position -1 if key is not // in the array. This value can easily be // transformed into the position to insert it. int first=0; int last=number_of_words; while (first <= last) { int mid = (first + last) / 2; // compute mid point. if (key > WordsArray[mid]) first = mid + 1; // repeat search in top half. else if (key < WordsArray[mid]) last = mid - 1; // repeat search in bottom half. else return mid; // found it. return position ///// } return -(first + 1); // failed to find key } /* h syndedemenh lista me stoixeia tis lekseis */ struct appearrence_count { private: word_appear* head; word_appear* tail; /* o synolikos arithos twn stoixeiwn ths listas, dhl oi lekseis */ int count; public: appearrence_count() { head=NULL; tail=NULL; count=0; } /* prosthetei mia leskh sth lista */ void add(string word); /* afairei ena stoixeio apo th lista */ void pop(); /* epistrefei true an einai adeia h lista */ bool IsEmpty(); /* adeiazei th lista me diadoxika pop */ void EmptyList(); void turn_flags_on(); void print_out(remaining_words *rw,int document_counter); }; void appearrence_count::add(string word) { word_appear* ptr=head; /* krataei an h leksh yparxei hdh sth lista */ int found=1; while (ptr!=NULL && found!=0)//gia olh th lista me tis lekseis { found=word.compare(ptr->word); //an exei vrethei hdh h oxi if (found!=0) ptr=ptr->next; //de vrethike pros to paron thn elegxoume me thn epomenh lesk ths listas } if (found==0) //thn exoume hdh vrei th leksh { (ptr->count)++; //ara afksanoume ton arithmo emfanisewn ths kata 1 if(ptr->flag==true) { (ptr->document_appearence)++; ptr->flag=false; } } else //th vriskoume gia prwth fora { count++; //+1 oi synolikes lekseis toy keimenou word_appear* new_wf=new word_appear(word); if(head==NULL) //den iparxei kanena stoixeio sth lista { head=new_wf; //tithetai ws prwto stoixeio ths listas new_wf->next=NULL; tail=new_wf; //afou einai to monadiko stoixeio ths listas tithetai kai ws teleftaio } else { word_appear* temp; temp=tail; temp->next=new_wf; //prostithetai sto telos tis listas tail=new_wf; //tithetai ws teleftaio stoixeio tis listas } (new_wf->count)++; //o arithmos emfanisewn ths lekshs sto arthro +1 */ (new_wf->document_appearence)++; new_wf->flag=false; } } //Epistrefei True an einai adeia h lista alliws epistrefei False bool appearrence_count::IsEmpty() { if( head == NULL) return true; return false; } //eksagei to prwto stoixeio ths listas void appearrence_count::pop() { //h lista den einai adeia if(head!=NULL) { //iparxoun toulaxiston 3 stoixeia sth lista if((head->next!=NULL)&&(head->next->next!= NULL)) { word_appear *temp1; word_appear *temp2; temp1=head; temp2=head->next; head=temp2; delete temp1; } //iparxoun 2 to poly stoixeia sth lista else //!(head->next!=NULL)&&(head->next->next!= NULL) { //iparxoun 2 akrivws stoixeia if(head->next!=NULL) { word_appear *temp1; temp1=head; head->next=NULL; head=tail; delete temp1; } //iparxei mono 1 stoixeio else //!(head->next!=NULL) { word_appear* temp1; temp1=head; head=NULL; tail=NULL; } } } } void appearrence_count::EmptyList() { count=0; while(!IsEmpty()) pop(); } void appearrence_count::turn_flags_on() { word_appear* ptr = head; while ((ptr!=NULL)&&(ptr!=tail)) { ptr->flag=true; ptr=ptr->next; //proxwrame sthn epomenh leksh ths listas } } void appearrence_count::print_out(remaining_words *rw,int document_counter) { word_appear* ptr = head; float float_count=(float)document_counter;/*synolikos arithmos arthrwn sto arxeio */ float percentage; /*pososto emfanishs se arthra */ while ((ptr!=NULL)&&(ptr!=tail)) { percentage=(ptr->document_appearence/float_count)*100; //ipologizoume to pososto emfanishs if((percentage>=0.2)&&(percentage<=15)) //an einai mesa sto orio pou theloume { (*rw).add(ptr->word,ptr->document_appearence); } ptr=ptr->next; //proxwrame sthn epomenh leksh ths listas } } struct stop_word { public: string word; stop_word* next; stop_word(string w) { word=w; next=NULL; } }; struct stop_words_list { private: stop_word* head; stop_word* tail; public: stop_words_list() { head=NULL; tail=NULL; } void add(string word); bool IsStopWord(string word); }; void stop_words_list::add(string word) { stop_word* ptr=head; stop_word* new_wf=new stop_word(word); if(head==NULL) //den iparxei kanena stoixeio sth lista { head=new_wf; //tithetai ws prwto stoixeio ths listas tail=new_wf; new_wf->next=NULL; } else { stop_word* temp; temp=tail; temp->next=new_wf; //prostithetai sto telos tis listas tail=new_wf; //tithetai ws teleftaio stoixeio tis listas } } //Epistrefei True an einai adeia h lista alliws epistrefei False bool stop_words_list::IsStopWord(string word) { stop_word* ptr=head; bool found=false; while (ptr!=NULL && !found)//gia olh th lista me tis lekseis { found=(word==ptr->word); //an exei vrethei hdh h oxi if (!found) ptr=ptr->next; //de vrethike pros to paron thn elegxoume me thn epomenh lesk ths listas } if (found) //thn exoume hdh vrei th leksh return true; return false; } //eksagei to prwto stoixeio ths listas #endif /* Web_clustering.cpp */ #pragma warning(push) #pragma warning(disable : 4561) #include #include #include #include #include #include #include"web_clustering.h" #include //gia th transform #using using namespace boost; /* Regular expressions */ /* taftopoiei string ths morfhs .I k opou k arithmos Sta arxeia pou exoume me arthra afto dhlwnei thn arxh enos arthrou */ regex expression1("^([.]I\\s[1-9][0-9]*)$"); /* taftopoiei string ths morfhs .X Sta arxeia pou exoume me arthra afto dhlwnei to telos toy keimenou poy mas endiaferei enos arthrou */ regex expression2("^([.]X)$"); /* taftopoiei string ths morfhs .W Sta arxeia pou exoume me arthra afto dhlwnei thn arxh toy keimenou poy mas endiaferei enos arthrou */ regex expression3("^([.]W)$"); regex expression4("^([.]T)$"); regex expression5("^([.]A)$"); using namespace System; using namespace System::IO; /*dhmioyrgei to fakelo articles kai trexei to filelist.exe */ void run(); using namespace std; #define MAX_NUMBER_OF_FILES 100 #define MAX_FILENAME 50 #define MAX_WORD_LENGTH /* dhmiourgei ta filenames twn newn arxeiwn apo ta palia p.x. MED.ALL => MED2.ALL */ string produce_name(string x); void main() { /* dhmioyrgoume to fakelo articles kai trexoume to filelist.exe */ run(); using namespace std; /*periexei ta filenames twn arxikwn arxeiwn */ string files[MAX_FILENAME]; /*periexei ta filenames twn newn arxeiwn */ string files2[MAX_FILENAME]; /* afta ta smheia stikshs orizoun thn srxh mias lekshs */ const string delims(" \t,.; ( ) ' / \" -:!{}[]<>?+=:@#$%^&*~`"); /* periexei mia th seira pou molis diavasthke apo ena arxeio se string */ string line; /* periexei mia th seira pou molis diavasthke apo ena arxeio se char[] */ char line2[MAX_FILENAME]; /* periexei kathe fora mia leksh */ string temp; /* metrhths pou deixnei posa arxeia me arthra yparxoun sto fakelo */ int i=0; /* syndedemenh lista pou vazoume tis lekseis kathe arthrou*/ freq_count c; /* syndedemenh lista pou vazoume tis lekseis pou kratame apo kathe arthro*/ remaining_words rw; /* syndedemenh lista pou vazoume tis lekseis kathe arxeiou*/ appearrence_count ac; /*metavlhth pou xrhsimopoioume gia na pername tis synolikes emfaniseis mias lekshs */ int app_count; /* metavlhth pou metra ton arithmo arthrwn se ena arxeio */ int document_counter; /* syndedemenh lista pou vazoume ta stop words */ stop_words_list swl; /* metavlhth gia to anoigma tou arxeiou me ta stop words */ ifstream file; /* metavlhth gia to anoigma tou arxeiou me th lista me ta .ALL arxeia */ ifstream file2; /* anoigma toy arxeiou me ta stop words */ file.open("stop words.txt"); /* periexei kathe fora mia stop word */ string temp_word; /*To anoigma tou arxeiou stop words.txt apetyxe */ if (file.fail()) { cout << "Opening of file \"stop words.txt\" failed.\n"; _getch(); exit(1); } /* mexri to telos toy arxeiou */ while ( !file.eof() ) { /*diavazoume mia seira apo to arxeio dhl mia leksh */ file.getline(line2,MAX_FILENAME); /* anathetoume th leksh se mia metavlhth string */ temp_word.assign(line2); /* thn prosthetoyme sth lista me tis stop words */ swl.add(temp_word); } /* anoigoume to arxeio me th lista me ta arxeia me ta arthra */ file2.open("directory_listing.txt"); /*To anoigma tou arxeiou a apetyxe */ if (file2.fail()) { cout << "Opening of file \"a\" failed .\n"; _getch(); exit(1); } /* mexri to telos toy arxeiou */ while ( !file2.eof() ) { /* diavazoume mia seira dhl to filename enos arxeiou me arthra */ file2.getline(line2,MAX_FILENAME); /* prosthetoume to filename ston pinaka me ta filenames */ files[i].assign(line2); /* paragoume to onoma toy neou arxeiou poy tha prokypsei kai to prosthetoume ston pinaka */ files2[i]=produce_name(files[i]); /* afksanoume ton metrhth arxeiwn me arthra kata 1 */ i++; } /* periexei to filename toy arxeiou pou tha epeksergastoume amesws meta */ char arxeio[MAX_NUMBER_OF_FILES][MAX_FILENAME]; /* periexei to filename toy neou arxeiou pou tha dhmiourghsoume amesws meta */ char neo_arxeio[MAX_NUMBER_OF_FILES][MAX_FILENAME]; printf("The articles will now be parsed...\n\n"); bool title_flag=false; string title; /* gia ola ta arxeia me arthra */ for(int j=0;j