Exemplo n.º 1
0
/* Startup */
void _PG_init(void)
{
	if (!libpostal_setup() || 
	    !libpostal_setup_parser() ||
	    !libpostal_setup_language_classifier())
	{
		elog(ERROR, "Failed to initialize libpostal.");
	}
}
int main(int argc, char **argv) {
    /*
     From http://codereview.stackexchange.com/questions/71970/creating-a-string-from-command-line-arguments
    */
    if(argc == 1) {
        printf("ERROR: Expected at least 1 argument\n");
        return 0;
    }

    int i, v = 1, size = argc - 1;
    int newsize  = 0;

    char *str = (char *)malloc(v);
    strcpy(str,"");

    for(i = 1; i <= size; i++) {
        newsize = newsize + 1 + sizeof(*str) +strlen(argv[i]);
        str = (char *)realloc(str, (newsize));
        strcat(str, argv[i]);
        strcat(str, " ");
    }


    // Setup (only called once at the beginning of your program)
    if (!libpostal_setup() || !libpostal_setup_parser()) {
        exit(EXIT_FAILURE);
    }

    /* 
        From https://github.com/openvenues/libpostal
     */
    address_parser_options_t options = get_libpostal_address_parser_default_options();
    address_parser_response_t *parsed = parse_address(str, options);

    for (size_t i = 0; i < parsed->num_components; i++) {
        printf("%s: %s\n", parsed->labels[i], parsed->components[i]);
    }

    // Free parse result
    address_parser_response_destroy(parsed);

    // Teardown (only called once at the end of your program)
    libpostal_teardown();
    libpostal_teardown_parser();
}
Exemplo n.º 3
0
int main(int argc, char **argv) {
    char *address_parser_dir = LIBPOSTAL_ADDRESS_PARSER_DIR;
    char *history_file = "address_parser.history";

    if (argc > 1) {
        address_parser_dir = argv[1];
    }

    printf("Loading models...\n");

    if (!libpostal_setup() || !libpostal_setup_parser()) {
        exit(EXIT_FAILURE);
    }

    printf("\n");

    printf("Welcome to libpostal's address parser.\n\n");
    printf("Type in any address to parse and print the result.\n\n");
    printf("Special commands:\n\n");
    printf(".language [code] to specify a language\n");
    printf(".country [code] to specify a country\n");
    printf(".exit to quit the program\n\n");

    char *language = NULL;
    char *country = NULL;

    char *input = NULL;

    while((input = linenoise("> ")) != NULL) {

        if (input[0] != '\0') {
            linenoiseHistoryAdd(input); /* Add to the history. */
            linenoiseHistorySave(history_file); /* Save the history on disk. */
        }

        if (strcmp(input, ".exit") == 0) {
            printf("Fin!\n");
            free(input);
            break;
        } else if (strncmp(input, ".language", 9) == 0) {
            size_t num_tokens = 0;
            cstring_array *command = cstring_array_split(input, " ", 1, &num_tokens);
            if (num_tokens > 1) {
                if (language != NULL) {
                    free(language);
                }
                language = strdup(cstring_array_get_string(command, 1));
            } else {
                printf("Must specify language code\n");
            }

            cstring_array_destroy(command);
            goto next_input;
        } else if (strncmp(input, ".country", 8) == 0) {
            size_t num_tokens = 0;
            cstring_array *command = cstring_array_split(input, " ", 1, &num_tokens);
            if (cstring_array_num_strings(command) > 1) {
                if (country != NULL) {
                    free(country);
                }
                country = strdup(cstring_array_get_string(command, 1));
            } else {
                printf("Must specify country code\n");
            }

            cstring_array_destroy(command);
            goto next_input;
        } else if (strlen(input) == 0) {
            goto next_input;
        }

        address_parser_response_t *parsed;
        address_parser_options_t options = get_libpostal_address_parser_default_options();

        if ((parsed = parse_address(input, options))) {
            printf("\n");
            printf("Result:\n\n");
            printf("{\n");
            for (int i = 0; i < parsed->num_components; i++) {
                char *json_string = json_encode_string(parsed->components[i]);
                printf("  \"%s\": %s%s\n", parsed->labels[i], json_string, i < parsed->num_components - 1 ? "," : "");
            }
            printf("}\n");
            printf("\n");

            address_parser_response_destroy(parsed);
        } else {
            printf("Error parsing address\n");
        }

next_input:
        free(input);
    }

    libpostal_teardown();
    libpostal_teardown_parser();
}