Saitoe said:#include "cvector.h"
#include "cmap.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void PrintCVector(CVector*v);
int StrPtrCompare(const void *ptr1, const void *ptr2);
int AllAlpha(char*a);
int IntPtrCompare(const void *ptr1, const void *ptr2);
int main(int argc, const char *argv[])
{
printf("Assignment 2 has begun!n");
CVector *stopWord = CVectorAlloc(100*sizeof(char*),400,NULL);
CVector *inputWord = CVectorAlloc(100*sizeof(char*),600,NULL);
CVector *key = CVectorAlloc(100*sizeof(char*),300,NULL);
CMap *tagCloud = CMapAlloc(sizeof(int),300,NULL);
CVector *topIndex = CVectorAlloc(sizeof(int),50,NULL);
CVector *topKey = CVectorAlloc(100*sizeof(char*),50,NULL);
char*buffer_ptr;
int stopBool=0;
//This flips the bool flag if there is no 2nd argument
FILE *fp = fopen(argv[2], "r");
if (fp == NULL) stopBool=1;
else
{
while (!feof(fp))
{
/*Reads stopword file line by line into buffer, then appends to
the vector*/
buffer_ptr=(char*)malloc(1000*sizeof(char*));
fscanf(fp,"%s",buffer_ptr);
CVectorAppend(stopWord,&buffer_ptr);
}
CVectorSort(stopWord,StrPtrCompare);
fclose(fp);
}
//Reads in the input into the inputWord vector
FILE *fp1 = fopen(argv[1],"r");
if(fp1==NULL) return 1;
while(!feof(fp1))
{
buffer_ptr=(char*)malloc(1000*sizeof(char*));
fscanf(fp1,"%s",buffer_ptr);
CVectorAppend(inputWord,&buffer_ptr);
}
fclose(fp1);
/*This subroutine loops over all of the words obtained in from the
input and checks them against the conditions established in the
assignment. An element will only be added if all of the characters are
alphabetical and if the element is > 3 in length. If the element
satisfies these two conditions, the element is checked to make sure
that it is not a stopword, if there are stopwords. Finally, it is
checked to ensure that the element has not already been added. If it
hasn't, then the element will be added to the tagCloud CMap, and if it
is already present, then the value at that key will be extracted and incremented.
*/
int i;
int*val;
for(i=0;i<CVectorCount(inputWord);i++)
{
char*element = *(char**)CVectorNth(inputWord,i);
if(strlen(element)>3&&AllAlpha(element)==1)
{
if(stopBool==1||CVectorSearch(stopWord,&element,StrPtrCompare,0,1)==-1)
{
if(CMapGet(tagCloud,element)==NULL)
{
val=(int*)malloc(sizeof(int));
*val=1;
CMapPut(tagCloud,element,val);
CVectorAppend(key,&element);
}
else
{
val=CMapGet(tagCloud,element);
*val=*val+1;
}
}
}
}
/*Only loops 50 times, or the length of key, whichever is less. This
subroutine determines the indecies of the 50 elements with the highest
frequency. It loops over key, checking to make sure the index being
checked isn't already in the topIndex CVector. Then, it uses
intermediate variables max and maxIndex to keep track of the current
maxima for each iteration, and stores them in topIndex once the inner
loop finishes iterating. Note: the if statements and the variable
assignments above both of them could be condensed to one line, but I
left it as is so as to not reduce readability
*/
int max =0;
int maxIndex=0;
i=0;
while(i<50 && i<CVectorCount(key))
{
max=0;
maxIndex=0;
CVectorSort(topIndex, IntPtrCompare);
int j;
for(j=0;j<CVectorCount(key);j++)
{
int x=CVectorSearch(topIndex,&j,IntPtrCompare,0,1);
if(x==-1)
{
int y = *(int*)CMapGet(tagCloud,*(char**)CVectorNth(key,j));
if(y>max)
{
max=y;
maxIndex=j;
}
}
}
CVectorAppend(topIndex,&maxIndex);
i++;
}
/*Fills in the topKey by simply iterating through topIndex. This is
necessary to produce a sorted result*/
for(i=0;i<CVectorCount(topIndex);i++)
{
char*element=*(char**)CVectorNth(key,*(int*)CVectorNth(topIndex,i));
CVectorAppend(topKey,&element);
}
//Sort the list of keys and then print according to desired output format
CVectorSort(topKey,StrPtrCompare);
for(i=0;i<CVectorCount(topKey);i++)
{
printf("%st%dn",*(char**)CVectorNth(topKey,i),*(int*)CMapGet(tagCloud,*(char**)CVectorNth(topKey,i)));
}
//Free up memory
CVectorFree(inputWord);
CVectorFree(stopWord);
CVectorFree(key);
CVectorFree(topIndex);
CVectorFree(topKey);
CMapFree(tagCloud);
return 0;
}
//Comparison of 2 integers, as given in Lab2
int IntPtrCompare(const void *ptr1, const void *ptr2)
{
return *(int *)ptr1-*(int *)ptr2;
}
//Comparison of 2 strings, as derived in Lab2
int StrPtrCompare(const void *ptr1, const void *ptr2)
{
return strcmp(*(char**)ptr1,*(char**)ptr2);
}
//Function to print CVector V that stores char*. Used for testing
void PrintCVector(CVector*v)
{
int i;
for(i=0;i<CVectorCount(v);i++)
{
printf("%sn",*(char**)CVectorNth(v,i));
}
}
//Returns 1 if all elements are alphabetical characters
//Returns 0 otherwise
int AllAlpha(char*a)
{
int i;
for(i=0;i<strlen(a);i++)
if((int)a<65 || (int)a>122)
return 0;
return 1;
} Saitoe said:#include "cvector.h"
#include "cmap.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void PrintCVector(CVector*v);
int StrPtrCompare(const void *ptr1, const void *ptr2);
int AllAlpha(char*a);
int IntPtrCompare(const void *ptr1, const void *ptr2);
int main(int argc, const char *argv[])
{
printf("Assignment 2 has begun!n");
CVector *stopWord = CVectorAlloc(100*sizeof(char*),400,NULL);
CVector *inputWord = CVectorAlloc(100*sizeof(char*),600,NULL);
CVector *key = CVectorAlloc(100*sizeof(char*),300,NULL);
CMap *tagCloud = CMapAlloc(sizeof(int),300,NULL);
CVector *topIndex = CVectorAlloc(sizeof(int),50,NULL);
CVector *topKey = CVectorAlloc(100*sizeof(char*),50,NULL);
char*buffer_ptr;
int stopBool=0;
//This flips the bool flag if there is no 2nd argument
FILE *fp = fopen(argv[2], "r");
if (fp == NULL) stopBool=1;
else
{
while (!feof(fp))
{
/*Reads stopword file line by line into buffer, then appends to
the vector*/
buffer_ptr=(char*)malloc(1000*sizeof(char*));
fscanf(fp,"%s",buffer_ptr);
CVectorAppend(stopWord,&buffer_ptr);
}
CVectorSort(stopWord,StrPtrCompare);
fclose(fp);
}
//Reads in the input into the inputWord vector
FILE *fp1 = fopen(argv[1],"r");
if(fp1==NULL) return 1;
while(!feof(fp1))
{
buffer_ptr=(char*)malloc(1000*sizeof(char*));
fscanf(fp1,"%s",buffer_ptr);
CVectorAppend(inputWord,&buffer_ptr);
}
fclose(fp1);
/*This subroutine loops over all of the words obtained in from the
input and checks them against the conditions established in the
assignment. An element will only be added if all of the characters are
alphabetical and if the element is > 3 in length. If the element
satisfies these two conditions, the element is checked to make sure
that it is not a stopword, if there are stopwords. Finally, it is
checked to ensure that the element has not already been added. If it
hasn't, then the element will be added to the tagCloud CMap, and if it
is already present, then the value at that key will be extracted and incremented.
*/
int i;
int*val;
for(i=0;i<CVectorCount(inputWord);i++)
{
char*element = *(char**)CVectorNth(inputWord,i);
if(strlen(element)>3&&AllAlpha(element)==1)
{
if(stopBool==1||CVectorSearch(stopWord,&element,StrPtrCompare,0,1)==-1)
{
if(CMapGet(tagCloud,element)==NULL)
{
val=(int*)malloc(sizeof(int));
*val=1;
CMapPut(tagCloud,element,val);
CVectorAppend(key,&element);
}
else
{
val=CMapGet(tagCloud,element);
*val=*val+1;
}
}
}
}
/*Only loops 50 times, or the length of key, whichever is less. This
subroutine determines the indecies of the 50 elements with the highest
frequency. It loops over key, checking to make sure the index being
checked isn't already in the topIndex CVector. Then, it uses
intermediate variables max and maxIndex to keep track of the current
maxima for each iteration, and stores them in topIndex once the inner
loop finishes iterating. Note: the if statements and the variable
assignments above both of them could be condensed to one line, but I
left it as is so as to not reduce readability
*/
int max =0;
int maxIndex=0;
i=0;
while(i<50 && i<CVectorCount(key))
{
max=0;
maxIndex=0;
CVectorSort(topIndex, IntPtrCompare);
int j;
for(j=0;j<CVectorCount(key);j++)
{
int x=CVectorSearch(topIndex,&j,IntPtrCompare,0,1);
if(x==-1)
{
int y = *(int*)CMapGet(tagCloud,*(char**)CVectorNth(key,j));
if(y>max)
{
max=y;
maxIndex=j;
}
}
}
CVectorAppend(topIndex,&maxIndex);
i++;
}
/*Fills in the topKey by simply iterating through topIndex. This is
necessary to produce a sorted result*/
for(i=0;i<CVectorCount(topIndex);i++)
{
char*element=*(char**)CVectorNth(key,*(int*)CVectorNth(topIndex,i));
CVectorAppend(topKey,&element);
}
//Sort the list of keys and then print according to desired output format
CVectorSort(topKey,StrPtrCompare);
for(i=0;i<CVectorCount(topKey);i++)
{
printf("%st%dn",*(char**)CVectorNth(topKey,i),*(int*)CMapGet(tagCloud,*(char**)CVectorNth(topKey,i)));
}
//Free up memory
CVectorFree(inputWord);
CVectorFree(stopWord);
CVectorFree(key);
CVectorFree(topIndex);
CVectorFree(topKey);
CMapFree(tagCloud);
return 0;
}
//Comparison of 2 integers, as given in Lab2
int IntPtrCompare(const void *ptr1, const void *ptr2)
{
return *(int *)ptr1-*(int *)ptr2;
}
//Comparison of 2 strings, as derived in Lab2
int StrPtrCompare(const void *ptr1, const void *ptr2)
{
return strcmp(*(char**)ptr1,*(char**)ptr2);
}
//Function to print CVector V that stores char*. Used for testing
void PrintCVector(CVector*v)
{
int i;
for(i=0;i<CVectorCount(v);i++)
{
printf("%sn",*(char**)CVectorNth(v,i));
}
}
//Returns 1 if all elements are alphabetical characters
//Returns 0 otherwise
int AllAlpha(char*a)
{
int i;
for(i=0;i<strlen(a);i++)
if((int)a<65 || (int)a>122)
return 0;
return 1;
} |