1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <cstdio>
using namespace std;
const int ARRAY_OF_ASCII_CODES_SIZE=256;
const int NUMBER_OF_FILES=8;
void initializeDataArray(int anASCIICodes[][ARRAY_OF_ASCII_CODES_SIZE], int nNumberOfFiles, int nASCIICodesSize);
void initializeOutputArray(double anCosineSimilarity[][NUMBER_OF_FILES], int nNumberOfFiles);
void calculateCosineSimilarity(int anASCIICodes[][ARRAY_OF_ASCII_CODES_SIZE], double anCosineSimilarity[][NUMBER_OF_FILES], int nNumberOfFiles, int nASCIICodesSize);
//Calculates the cosine similarity
void outputCSV(int anASCIICodes[][ARRAY_OF_ASCII_CODES_SIZE], int nNumberOfFiles, int nASCIICodesSize, ofstream& fCSV);
//outputs the data array in a csv file
void outputHTML(double anCosineSimilarity[][NUMBER_OF_FILES], int nNumberOfFiles, int nASCIICodesSize, ofstream& fHTML, char asInputFileNames[][FILENAME_MAX]);
//output the cosine similarity matrix in HTML format
void writeHeader(ofstream& ofHTMLfile);
//writes the HTML header
void writeBody(ofstream& ofHTMLfile, double anCosineSimilarity[][NUMBER_OF_FILES], int nNumberOfFiles, char asInputFileNames[][FILENAME_MAX]);
void writeFooter(ofstream& ofHTMLfile);
//writes the HTML footer
int main(int argc, char *argv[])
{
ifstream fin;
ofstream fHTML;
ofstream fCSV;
unsigned char cInput=' ';
unsigned int nInput=0;
int anASCIICodes[NUMBER_OF_FILES][ARRAY_OF_ASCII_CODES_SIZE];
double anCosineSimilarity[NUMBER_OF_FILES][NUMBER_OF_FILES];
char asInputFileNames[NUMBER_OF_FILES][FILENAME_MAX]; //array to store filenames
if(argc!=9)
{
cout << "There should be 9 command line arguments." << endl;
cout << "Only" << argc << " arguments were entered" << endl;
exit(1);
}
initializeDataArray(anASCIICodes, NUMBER_OF_FILES, ARRAY_OF_ASCII_CODES_SIZE);
initializeOutputArray(anCosineSimilarity, NUMBER_OF_FILES);
for (int i=1; i<argc; i++) //loop through all the command line arguments, except the first
{
char sExample[3]="ab";
string sTemp="";
cout << "argument " << i << "=" << argv[i] << "\n"; //echo the command line arguments
sTemp=argv[i];
memset(asInputFileNames[(i-1)],'\0', FILENAME_MAX);
memcpy(asInputFileNames[(i-1)], sTemp.c_str(), sTemp.length());
fin.open(asInputFileNames[(i-1)]);
if (fin.fail())
{
cout << "Failure opening input file [" << asInputFileNames[(i-1)] << "].\n";
exit(2);
}
else
{
cout << "Input file [" << asInputFileNames[(i-1)] << "] opened.\n";
}
while (!fin.eof())
{
cInput=fin.get();
nInput=(unsigned int)cInput;
if(nInput>=0 && nInput<ARRAY_OF_ASCII_CODES_SIZE)
{
++anASCIICodes[i-1][nInput];
}
}
fin.close();
}
calculateCosineSimilarity(anASCIICodes, anCosineSimilarity, NUMBER_OF_FILES, ARRAY_OF_ASCII_CODES_SIZE);
fHTML.open("project5.html");
if (fHTML.fail())
{
cout << "Failure opening output file project5.html.\n";
exit(3);
}
fCSV.open("project5.csv");
if (fCSV.fail())
{
cout << "Failure opening output file project5.csv.\n";
exit(4);
}
outputCSV(anASCIICodes, NUMBER_OF_FILES, ARRAY_OF_ASCII_CODES_SIZE, fCSV);
outputHTML(anCosineSimilarity, NUMBER_OF_FILES, ARRAY_OF_ASCII_CODES_SIZE, fHTML, asInputFileNames);
fHTML.close();
fCSV.close();
cout << "End of program.\n";
return 0;
}
void initializeDataArray(int anASCIICodes[][ARRAY_OF_ASCII_CODES_SIZE], int nNumberOfFiles, int nASCIICodesSize)
{
for (int i=0; i<nNumberOfFiles; i++)
{
for (int j=0; j<nASCIICodesSize; j++)
{
anASCIICodes[i][j]=0;
}
}
return;
}
void initializeOutputArray(double anCosineSimilarity[][NUMBER_OF_FILES], int nNumberOfFiles)
{
for (int i=0; i<nNumberOfFiles; i++)
{
for (int j=0; j<nNumberOfFiles; j++)
{
anCosineSimilarity[i][j]=0.0;
}
}
return;
}
void calculateCosineSimilarity(int anASCIICodes[][ARRAY_OF_ASCII_CODES_SIZE], double anCosineSimilarity[][NUMBER_OF_FILES], int nNumberOfFiles, int nASCIICodesSize)
{
double nDotProduct=0;
double nRunningSum=0;
double anLength[NUMBER_OF_FILES];
for (int i=0; i<nNumberOfFiles; i++) //calculate the length of each array
{
nRunningSum=0;
for (int j=0; j<nASCIICodesSize; j++)
{
double nCurrentValue=(double)(anASCIICodes[i][j]);
nRunningSum=nRunningSum+(nCurrentValue*nCurrentValue);
}
anLength[i]=sqrt(nRunningSum);
}
for (int i1=0; i1<nNumberOfFiles; i1++)
{
for (int i2=0; i2<nNumberOfFiles; i2++)
{
nDotProduct=0;
for (int j=0; j<nASCIICodesSize; j++)
{
double nProduct=(anASCIICodes[i1][j]*anASCIICodes[i2][j]);
nDotProduct=nDotProduct+nProduct;
}
double nCS=nDotProduct/(anLength[i1]*anLength[i2]);
anCosineSimilarity[i1][i2]=nCS;
}
}
return;
}
void outputCSV(int anASCIICodes[][ARRAY_OF_ASCII_CODES_SIZE], int nNumberOfFiles, int nASCIICodesSize, ofstream& fCSV)
{
for (int i=0; i<ARRAY_OF_ASCII_CODES_SIZE; i++)
{
fCSV << i;
fCSV.put(',');
}
return;
}
void outputHTML(double anCosineSimilarity[][NUMBER_OF_FILES], int nNumberOfFiles, int nASCIICodesSize, ofstream& fHTML, char asInputFileNames[][FILENAME_MAX])
{
return;
}
|