[api] Adding a --json option to expand cli
This commit is contained in:
@@ -22,7 +22,7 @@ libscanner_la_SOURCES = scanner.c
|
|||||||
libscanner_la_CFLAGS = $(CFLAGS_O0)
|
libscanner_la_CFLAGS = $(CFLAGS_O0)
|
||||||
|
|
||||||
noinst_PROGRAMS = libpostal bench build_address_dictionary build_geodb build_numex_table build_trans_table address_parser_train address_parser_test address_parser
|
noinst_PROGRAMS = libpostal bench build_address_dictionary build_geodb build_numex_table build_trans_table address_parser_train address_parser_test address_parser
|
||||||
libpostal_SOURCES = main.c
|
libpostal_SOURCES = main.c json_encode.c
|
||||||
libpostal_LDADD = libpostal.la
|
libpostal_LDADD = libpostal.la
|
||||||
libpostal_CFLAGS = $(CFLAGS_O3)
|
libpostal_CFLAGS = $(CFLAGS_O3)
|
||||||
bench_SOURCES = bench.c
|
bench_SOURCES = bench.c
|
||||||
|
|||||||
63
src/main.c
63
src/main.c
@@ -1,57 +1,82 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
|
||||||
#include "libpostal.h"
|
#include "libpostal.h"
|
||||||
#include "log/log.h"
|
#include "log/log.h"
|
||||||
|
#include "json_encode.h"
|
||||||
#include "string_utils.h"
|
#include "string_utils.h"
|
||||||
|
|
||||||
#define LIBPOSTAL_USAGE "Usage: ./libpostal address [language ...]\n"
|
#define LIBPOSTAL_USAGE "Usage: ./libpostal address language [--json]\n"
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
int i;
|
uint64_t i;
|
||||||
char *arg;
|
char *arg;
|
||||||
|
|
||||||
|
char *address = NULL;
|
||||||
|
char *language = NULL;
|
||||||
|
|
||||||
|
bool use_json = false;
|
||||||
|
|
||||||
|
string_array *languages = NULL;
|
||||||
|
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
arg = argv[i];
|
arg = argv[i];
|
||||||
if (string_equals(arg, "-h") || string_equals(arg, "--help")) {
|
if (string_equals(arg, "-h") || string_equals(arg, "--help")) {
|
||||||
printf(LIBPOSTAL_USAGE);
|
printf(LIBPOSTAL_USAGE);
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
|
} else if (string_equals(arg, "--json")) {
|
||||||
|
use_json = true;
|
||||||
|
} else if (address == NULL) {
|
||||||
|
address = arg;
|
||||||
|
} else if (!string_starts_with(arg, "-")) {
|
||||||
|
if (languages == NULL) {
|
||||||
|
languages = string_array_new();
|
||||||
|
}
|
||||||
|
string_array_push(languages, arg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (argc < 3) {
|
if (address == NULL || languages == NULL) {
|
||||||
log_error(LIBPOSTAL_USAGE);
|
log_error(LIBPOSTAL_USAGE);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *str = argv[1];
|
|
||||||
char *languages[argc - 2];
|
|
||||||
for (i = 0; i < argc - 2; i++) {
|
|
||||||
arg = argv[i + 2];
|
|
||||||
if (strlen(arg) >= MAX_LANGUAGE_LEN) {
|
|
||||||
printf("arg %d was longer than a language code (%d chars). Make sure to quote the input string\n", i + 2, MAX_LANGUAGE_LEN - 1);
|
|
||||||
}
|
|
||||||
languages[i] = arg;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!libpostal_setup()) {
|
if (!libpostal_setup()) {
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
normalize_options_t options = LIBPOSTAL_DEFAULT_OPTIONS;
|
normalize_options_t options = LIBPOSTAL_DEFAULT_OPTIONS;
|
||||||
options.languages = languages;
|
options.languages = languages->a;
|
||||||
|
options.num_languages = languages->n;
|
||||||
|
|
||||||
size_t num_expansions;
|
size_t num_expansions;
|
||||||
|
|
||||||
char **strings = expand_address(str, options, &num_expansions);
|
char **strings = expand_address(address, options, &num_expansions);
|
||||||
|
|
||||||
|
string_array_destroy(languages);
|
||||||
|
|
||||||
char *normalized;
|
char *normalized;
|
||||||
for (uint64_t i = 0; i < num_expansions; i++) {
|
|
||||||
normalized = strings[i];
|
if (!use_json) {
|
||||||
printf("%s\n", normalized);
|
for (i = 0; i < num_expansions; i++) {
|
||||||
free(normalized);
|
normalized = strings[i];
|
||||||
|
printf("%s\n", normalized);
|
||||||
|
free(normalized);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
printf("{\"expansions\": [");
|
||||||
|
for (i = 0; i < num_expansions; i++) {
|
||||||
|
normalized = strings[i];
|
||||||
|
char *json_string = json_encode_string(normalized);
|
||||||
|
printf("%s%s", json_string, i < num_expansions - 1 ? ", ": "");
|
||||||
|
free(normalized);
|
||||||
|
free(json_string);
|
||||||
|
}
|
||||||
|
printf("]}\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free(strings);
|
free(strings);
|
||||||
|
|||||||
Reference in New Issue
Block a user