void generateTagCloudData(struct hashMap *ht, char *outFileName ) { /* Read data to get max value */ struct mapItr *myItr; myItr = createMapIterator(ht); char *word; int *count; int maxVal = 0; while(hasNextMap(myItr)) { word = (char *)nextMap(myItr); count = (int *)atMap(ht,word, myCompare, hash2); if(*count > maxVal) maxVal = *count; } printf("MAX VAL = %d\n", maxVal); initMapIterator(ht, myItr); /* Normalize all values */ while(hasNextMap(myItr)) { word = (char*) nextMap(myItr); count = (int *)atMap(ht,word, myCompare, hash2); *count = sqrt(*count); } /* Now write them to file */ initMapIterator(ht, myItr); FILE *tagFile = fopen(outFileName, "w+"); while(hasNextMap(myItr)) { word = (char *)nextMap(myItr); count = atMap(ht,word, myCompare, hash2); fprintf(tagFile, "%s,%d\n", word, *count); } fclose(tagFile); }
int main (int argc, const char * argv[]) { const char* filename; struct hashMap *hashTable; int tableSize = 10; clock_t timer; FILE *fileptr; /* this part is using command line arguments, you can use them if you wish but it is not required. DO NOT remove this code though, we will use it for testing your program. if you wish not to use command line arguments manually type in your filename and path in the else case. */ if(argc == 2) filename = argv[1]; else filename = "input1.txt"; /*specify your input text file here*/ printf("opening file: %s\n", filename); timer = clock(); hashTable = createMap(tableSize); /*... concordance code goes here ...*/ fileptr = fopen(filename, "r"); if (fileptr != NULL) { int *val, *x; char *word; while ((word = getWord(fileptr))) { if (containsKey(hashTable, word)) { val = (int *) atMap(hashTable, word); (*val) ++; free(word); } else { x = malloc(sizeof(int)); *x = 1; insertMap(hashTable, word, x); } } fclose(fileptr); } else printf("Error opening file.\n"); /*... concordance code ends here ...*/ printMap(hashTable, keyPrint, valPrint); timer = clock() - timer; printf("\nconcordance ran in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC); printf("Table emptyBuckets = %d\n", emptyBuckets(hashTable)); printf("Table count = %d\n", size(hashTable)); printf("Table capacity = %d\n", capacity(hashTable)); printf("Table load = %f\n", tableLoad(hashTable)); printf("Deleting keys\n"); removeKey(hashTable, "and"); removeKey(hashTable, "me"); removeKey(hashTable, "the"); printMap(hashTable, keyPrint, valPrint); printKeyValues(hashTable, keyPrint, valPrint); /* Test out the iterator */ #ifdef ITERATOR_IN struct mapItr *myItr; myItr = createMapIterator(hashTable); KeyType key; /* Free up our keys and values using our iterator!! Also printing them as we go along */ while(hasNextMap(myItr)) { key = nextMap(myItr); int *value = atMap(hashTable,key); printf("Freeing ...Key = %s, value = %d \n", key, *value); free(value); /* To match the malloc above*/ free(key); } #endif deleteMap(hashTable); printf("\nDeleted the table\n"); return 0; }
int main (int argc, const char * argv[]) { const char* filename; struct hashMap *hashTable; int tableSize = 10; clock_t timer; FILE *fileptr; void* key; /* this part is using command line arguments, you can use them if you wish but it is not required. DO NOT remove this code though, we will use it for testing your program. if you wish not to use command line arguments manually type in your filename and path in the else case. */ if(argc == 2) filename = argv[1]; else filename = "input1.txt"; /*specify your input text file here*/ printf("opening file: %s\n", filename); timer = clock(); hashTable = createMap(tableSize); printf("Successfully created hashtable\n"); /*... concordance code goes here ...*/ fileptr = fopen(filename, "r+"); char* wordNext = "starter"; while(wordNext != 0) { wordNext = getWord(fileptr); if(wordNext != 0) { if(containsKey(hashTable, wordNext, myCompare, hash2) == 0){ void* v = malloc(sizeof(void*)); *(int*)v = 1; insertMap(hashTable, wordNext, v, myCompare, hash2); } else { /*int newVal = *(int*)(atMap(hashTable, wordNext, myCompare, hash2)) + 1; void* newVal2 = &newVal; insertMap(hashTable, wordNext, newVal2, myCompare, hash2);*/ *(int*)(atMap(hashTable, wordNext, myCompare, hash2)) = *(int*)atMap(hashTable, wordNext, myCompare, hash2)+1; } } } /*... concordance code ends here ...*/ printMap(hashTable, keyPrint, valPrint); fclose(fileptr); timer = clock() - timer; printf("\nconcordance ran in %f seconds\n", (float)timer / (float)CLOCKS_PER_SEC); printf("Table emptyBuckets = %d\n", emptyBuckets(hashTable)); printf("Table count = %d\n", size(hashTable)); printf("Table capacity = %d\n", capacity(hashTable)); printf("Table load = %f\n", tableLoad(hashTable)); printf("Deleting keys\n"); assert(containsKey(hashTable, "and", myCompare, hash2)); removeKey(hashTable, "and", myCompare, hash2); removeKey(hashTable, "me", myCompare, hash2); removeKey(hashTable, "the", myCompare, hash2); /* printMap(hashTable); */ printKeyValues(hashTable, keyPrint, valPrint); /* For Tag Cloud */ generateTagCloudData(hashTable,"tag.csv"); /* Free up our keys and values using our iterator!! Also printing them as we go along */ struct mapItr *myItr = createMapIterator(hashTable); while(hasNextMap(myItr)) { key = nextMap(myItr); int *value = atMap(hashTable,key, myCompare, hash2); printf("Freeing ...Key = %s, value = %d \n", key, *value); free(value); /* To match the malloc above*/ free(key); } deleteMap(hashTable); printf("\nDeleted the table\n"); return 0; }