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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
#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<nASCIICodesSize; i++)
{
fCSV << i;
fCSV.put(',');
}
fCSV << "TOTAL";
fCSV.put('\n');
for (int i=0; i<nNumberOfFiles; i++)
{
for (int j=0; j<nASCIICodesSize; j++)
{
fCSV << anASCIICodes[i][j];
fCSV.put(',');
}
fCSV.put('\n');
}
return;
}
void outputHTML(double anCosineSimilarity[][NUMBER_OF_FILES], int nNumberOfFiles, int nASCIICodesSize, ofstream& fHTML, char asInputFileNames[][FILENAME_MAX])
{
fHTML << "<HTML>\n"; //HTML lines to make content within the HTML file
fHTML << "<HEAD>\n";
fHTML << "<TITLE>\n";
fHTML << "Renato Mendez\n";
fHTML << "</TITLE>\n";
fHTML << "8 by 8 table <br>\n";
fHTML << "</HEAD>\n";
fHTML << "</HTML>\n";
fHTML << "<BODY>\n";
fHTML << "Cosine Similarity in text files\n";
fHTML << "<TABLE BORDER=\"1\"> \n";
fHTML << "<th></th>\n";
fHTML << "<th> PreambleDutch.txt </th> \n";
fHTML << "<th> PreambleEnglish.txt </th> \n";
fHTML << "<th> PreambleLatin.txt </th> \n";
fHTML << "<th> PreambleSpanish.txt </th> \n";
fHTML << "<th> PreambleGerman.txt </th> \n";
fHTML << "<th> PreamblePortuguese.txt </th> \n";
fHTML << "<th> PreambleFrench.txt </th> \n";
fHTML << "<th> PreambleItalian.txt </th> \n";
fHTML << "<tr>";
for (int i=0; i<nNumberOfFiles; i++)
{
fHTML << "<td>" << asInputFileNames[(i)]<< "</td>";
for (int j=0; j<nNumberOfFiles; j++)
{
fHTML << "<td>" << anCosineSimilarity[i][j] << "</td>";
for (int i1=0; i1<nASCIICodesSize; i1++)
{
}
}
fHTML << "</tr>";
}
fHTML << "</TABLE>\n";
fHTML << "</BODY>\n";
return;
}
|