Example #1
0
int main(int argc, char *argv[]){

	printf("====================== Testing Create BST ====================\n");
	char * businesses_path = "/home/pharaoh/ece264/solutions/ece264/PA10/businesses.tsv";
	char * reviews_path = "/home/pharaoh/ece264/solutions/ece264/PA10/reviews.tsv";
	struct YelpDataBST * bst = NULL;
	bst = create_business_bst(businesses_path, reviews_path);
	printf("%p\n", bst);

	printf("====================== Testing The Search ====================\n");
	get_business_reviews(bst, "Starbucks", NULL, NULL);


	return EXIT_SUCCESS;
}
Example #2
0
int Search(int argc, char *argv[], const char * filename, struct YelpDataBST * bst) {
	freopen(filename, "wb", stdout);

	

	// We will collect stats for easy summarization
	int num_locations = 0, num_address_chars = 0, num_reviews = 0, num_review_chars = 0, total_stars = 0;

	int arg_idx;
	for (arg_idx = 1; arg_idx < argc; arg_idx += 3) {

		// Extract the query
		char* name = argv[arg_idx];
		char* state = arg_idx + 1 < argc ? argv[arg_idx + 1] : "";
		char* zip_code = arg_idx + 2 < argc ? argv[arg_idx + 2] : "";

		// Print the query
		int query_num = (arg_idx + 2) / 3;
		printf("================================================================================\n");
		printf("QUERY #%d:  name=\"%s\"  state=\"%s\"  zip_code=\"%s\"\n\n", query_num, name, state, zip_code);

		// If state or zip_code are "", change them to NULL.   (The specification of get_business_reviews(..)
		// says to pass NULL to include all states and zip codes.)
		char* state_or_null = state[0] == '\0' ? NULL : state;    // change to NULL if ""
		char* zip_code_or_null = zip_code[0] == '\0' ? NULL : zip_code; // change to NULL if ""
		struct Business *b = get_business_reviews(bst, name, state_or_null, zip_code_or_null);

		// Print results
		if (b == NULL) {
			printf("RESULTS:  (none)\n\n");
		}
		else {
			printf("RESULTS:  \"%s\"  (%d locations)\n\n", b->name, b->num_locations);

			int location_idx;
			for (location_idx = 0; location_idx<b->num_locations; location_idx++) {

				// Print LOCATION
				struct Location loc = b->locations[location_idx];
				printf("LOCATION #%d:  address=\"%s\"  city=\"%s\"  state=\"%s\"  zip_code=\"%s\"  (%d reviews)\n\n",
					location_idx + 1, loc.address, loc.city, loc.state, loc.zip_code, loc.num_reviews);
				num_address_chars += strlen(loc.address) + strlen(loc.state) + strlen(loc.zip_code);
				num_locations++;

				// Print REVIEW
				int review_idx;
				for (review_idx = 0; review_idx<loc.num_reviews; review_idx++) {
					struct Review rvw = loc.reviews[review_idx];
					printf("REVIEW #%d:  %d stars  (%d chars)\n", review_idx + 1, rvw.stars, (int)strlen(rvw.text));
					printf("%s\n\n", rvw.text);
					num_reviews++;
					num_review_chars += strlen(rvw.text);
					total_stars += rvw.stars;
				}

				// Print a divider (unless this is the last time)
				if (location_idx + 1 < b->num_locations) {
					printf("................................................................................\n");
				}
			}
			printf("\n");

			// Destroy (free) the result object
			destroy_business_result(b);
		}
	}

	// Print summary
	float avg_stars = num_reviews > 0 ? 1.0 * total_stars / num_reviews : 0.0;
	printf("================================================================================\n");
	printf("SUMMARY:  %d locations (%d chars),  %d reviews (%d chars),  %.2f stars avg\n",
		num_locations, num_address_chars, num_reviews, num_review_chars, avg_stars);
	printf("================================================================================\n");

	freopen("CON", "wb", stdout);

	return EXIT_SUCCESS;
}
Example #3
0
int main(int argc, char *argv[]){
	int i;

	char businesses_path[] = "/home/pharaoh/ece264/solutions/ece264/PA10/businesses.tsv";
	char reviews_path[] = "/home/pharaoh/ece264/solutions/ece264/PA10/reviews.tsv";

	if(argc == 1) {
		fprintf(stderr, "=================================================Starting Program=============================================\n");
		fprintf(stderr, "Run this program with a 1 as the argument to procede.\nThis will start the building of the BST\n");
		fprintf(stderr, "It will take 1ish min to complete, and will only happen once before multiple searches can be made.\n\n");
		return EXIT_FAILURE;
	}
	if (argc == 2 && strcmp(argv[1], "1") == 0)
	{
		fprintf(stderr, "=================================================Starting Program=============================================\n");
		fprintf(stderr, "Starting to build the BST.\n");
		fprintf(stderr, "This will take approximately 1ish minutes to complete before you will be prompted for multiple searches\n");
	}
	else
	{
		printf("argv[1] must be 1 to continue.\n");
		return EXIT_FAILURE;
	}

	//create the BST
	struct YelpDataBST * bst = NULL;
	int total_businesses = 0;
	bst = create_business_bst(businesses_path, reviews_path, &total_businesses);

	//prompt user, and do the searches
	int continue_searching = 1;
	char * prompt1 = "Name: ";
	char * promptstate = "State: ";
	char * promptzip = "Zip: ";
	char * promptwords ="Words: ";
	int max_line_length = 2000;
	char * line = malloc(max_line_length * sizeof(char));
	while(continue_searching == 1)
	{
		printf("++++++\nStarting a Search\nEnter -1 at any time to exit\n+++++\n");
		char * name = malloc(MAXNAME * sizeof(char));
		char * state = malloc(MAXSTATE * sizeof(char));
		char * zip = malloc(MAXZIP * sizeof(char));
		char * word = malloc(MAXWORD * sizeof(char));
		int max_words_in_array = 20;
		char ** word_array = malloc(max_words_in_array * sizeof(char *));
		getLine(prompt1, name, MAXNAME);
		if (strcmp(name, "-1") == 0)
		{
			printf("I really hope this demonstrates my ability to use the C language and problemsolve while coding\n");
			free(name);
			break;
		}
		getLine(promptstate, state, MAXSTATE);
		if (strcmp(state, "-1") == 0)
		{
			printf("I really hope this demonstrates my ability to use the C language and problemsolve while coding\n");
			free(name);
			free(state);
			break;
		}
		getLine(promptzip, zip, MAXZIP);
		if (strcmp(zip, "-1") == 0)
		{
			printf("I really hope this demonstrates my ability to use the C language and problemsolve while coding\n");
			free(name);
			free(state);
			free(zip);
			break;
		}
		getLine(promptwords, line, 2000);
		if (strcmp(line, "-1") == 0)
		{
			printf("I really hope this demonstrates my ability to use the C language and problemsolve while coding\n");
			free(name);
			free(state);
			free(zip);
			free(word_array);
			free(line);
			break;
		}
		//take in the words to a string array
		int b = 0;
		int a = 0;
		int num_words = 0;
		while(line[a] != '\0')
		{
			if (line[a] == ' ') //test to see if we've moved to a new cell
			{
				b = 0;
				a++; //to move past the ' '
			}
			word[b] = line[a];
			b++;
			word[b] = '\0';
			//found an entire word, add it to the array
			if (line[a+1] == ' ' || line[a+1] == '\0' || line[a+1] == '\n')
			{
				//ensure the array can handle it
				if (num_words >= max_words_in_array-1)
				{
					max_words_in_array *= 2;
					word_array = realloc(word_array, max_words_in_array * sizeof(char));
				}
				word_array[num_words] = strdup(word);
				num_words++;
			}
			a++;
		}
		if (strlen(name) == 0)
		{
			name = NULL;
		}
		if (strlen(state)==0)
		{
			state = NULL;
		}
		if (strlen(zip) == 0)
		{
			zip = NULL;
		}
		printf("words:\n");
		for (i = 0; i < num_words; ++i)
		{
			printf("%s\n",word_array[i] );
		}
		printf("searched for: name: %s, state: %s, zip: %s, num words: %d\n",name ,state,zip, num_words );
		//using the user entered search, create the business structs
		if (name == NULL && state == NULL && zip == NULL && num_words != 0)
		{
			noNameYesWords(word_array, num_words, reviews_path);
		}
		else
		{
			struct Business ** businesses;
			int t_businesses = 0;
			businesses = get_business_reviews(bst, name, state, zip, word_array, num_words, &t_businesses);

			//print out the businesses
			if (t_businesses != 0)
			{
				for (i = 0; i < t_businesses; ++i)
				{
					printBusiness(businesses[i], i);
				}
			}
			else
			{
				printf("COULD NOT FIND THE BUSINESS\n");
			}
			destroy_business_result(businesses, t_businesses);
		}
		//free the memory
		free(word);
		free(name);
		free(state);
		free(zip);
		for (i = 0; i < num_words; ++i)
		{
			free(word_array[i]);
		}
		free(word_array);
	}

	//free memory
	destroy_business_bst(bst, total_businesses);

	return EXIT_SUCCESS;
}