[parser] storing address_parser_context on the parser struct itself so it doesn't have to be allocated every time
This commit is contained in:
@@ -287,6 +287,11 @@ bool address_parser_load(char *dir) {
|
|||||||
|
|
||||||
fclose(postal_codes_file);
|
fclose(postal_codes_file);
|
||||||
|
|
||||||
|
parser->context = address_parser_context_new();
|
||||||
|
if (parser->context == NULL) {
|
||||||
|
goto exit_address_parser_created;
|
||||||
|
}
|
||||||
|
|
||||||
char_array_destroy(path);
|
char_array_destroy(path);
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
@@ -305,6 +310,10 @@ void address_parser_destroy(address_parser_t *self) {
|
|||||||
crf_destroy(self->model.crf);
|
crf_destroy(self->model.crf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (self->context != NULL) {
|
||||||
|
address_parser_context_destroy(self->context);
|
||||||
|
}
|
||||||
|
|
||||||
if (self->vocab != NULL) {
|
if (self->vocab != NULL) {
|
||||||
trie_destroy(self->vocab);
|
trie_destroy(self->vocab);
|
||||||
}
|
}
|
||||||
@@ -1642,15 +1651,17 @@ libpostal_address_parser_response_t *address_parser_response_new(void) {
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
libpostal_address_parser_response_t *address_parser_parse(char *address, char *language, char *country, address_parser_context_t *context) {
|
libpostal_address_parser_response_t *address_parser_parse(char *address, char *language, char *country) {
|
||||||
if (address == NULL || context == NULL) return NULL;
|
if (address == NULL) return NULL;
|
||||||
|
|
||||||
address_parser_t *parser = get_address_parser();
|
address_parser_t *parser = get_address_parser();
|
||||||
if (parser == NULL) {
|
if (parser == NULL || parser->context == NULL) {
|
||||||
log_error("parser is not setup, call libpostal_setup_address_parser()\n");
|
log_error("parser is not setup, call libpostal_setup_address_parser()\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
address_parser_context_t *context = parser->context;
|
||||||
|
|
||||||
char *normalized = address_parser_normalize_string(address);
|
char *normalized = address_parser_normalize_string(address);
|
||||||
bool is_normalized = normalized != NULL;
|
bool is_normalized = normalized != NULL;
|
||||||
if (!is_normalized) {
|
if (!is_normalized) {
|
||||||
|
|||||||
@@ -200,6 +200,7 @@ typedef struct address_parser {
|
|||||||
averaged_perceptron_t *ap;
|
averaged_perceptron_t *ap;
|
||||||
crf_t *crf;
|
crf_t *crf;
|
||||||
} model;
|
} model;
|
||||||
|
address_parser_context_t *context;
|
||||||
trie_t *vocab;
|
trie_t *vocab;
|
||||||
trie_t *phrases;
|
trie_t *phrases;
|
||||||
address_parser_types_array *phrase_types;
|
address_parser_types_array *phrase_types;
|
||||||
@@ -214,7 +215,7 @@ address_parser_t *address_parser_new_options(parser_options_t options);
|
|||||||
address_parser_t *get_address_parser(void);
|
address_parser_t *get_address_parser(void);
|
||||||
bool address_parser_load(char *dir);
|
bool address_parser_load(char *dir);
|
||||||
|
|
||||||
libpostal_address_parser_response_t *address_parser_parse(char *address, char *language, char *country, address_parser_context_t *context);
|
libpostal_address_parser_response_t *address_parser_parse(char *address, char *language, char *country);
|
||||||
void address_parser_destroy(address_parser_t *self);
|
void address_parser_destroy(address_parser_t *self);
|
||||||
|
|
||||||
char *address_parser_normalize_string(char *str);
|
char *address_parser_normalize_string(char *str);
|
||||||
|
|||||||
@@ -328,6 +328,13 @@ address_parser_t *address_parser_init(char *filename) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
address_parser_context_t *context = address_parser_context_new();
|
||||||
|
if (context == NULL) {
|
||||||
|
log_error("Error allocating context\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
parser->context = context;
|
||||||
|
|
||||||
khash_t(str_uint32) *vocab = kh_init(str_uint32);
|
khash_t(str_uint32) *vocab = kh_init(str_uint32);
|
||||||
if (vocab == NULL) {
|
if (vocab == NULL) {
|
||||||
log_error("Could not allocate vocab\n");
|
log_error("Could not allocate vocab\n");
|
||||||
@@ -1043,7 +1050,7 @@ bool address_parser_train_epoch(address_parser_t *self, void *trainer, char *fil
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
address_parser_context_t *context = address_parser_context_new();
|
address_parser_context_t *context = self->context;
|
||||||
|
|
||||||
size_t examples = 0;
|
size_t examples = 0;
|
||||||
uint64_t errors = address_parser_train_num_errors(self, trainer);
|
uint64_t errors = address_parser_train_num_errors(self, trainer);
|
||||||
@@ -1087,7 +1094,6 @@ bool address_parser_train_epoch(address_parser_t *self, void *trainer, char *fil
|
|||||||
|
|
||||||
exit_epoch_training_started:
|
exit_epoch_training_started:
|
||||||
address_parser_data_set_destroy(data_set);
|
address_parser_data_set_destroy(data_set);
|
||||||
address_parser_context_destroy(context);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1021,18 +1021,14 @@ inline libpostal_address_parser_options_t libpostal_get_address_parser_default_o
|
|||||||
}
|
}
|
||||||
|
|
||||||
libpostal_address_parser_response_t *libpostal_parse_address(char *address, libpostal_address_parser_options_t options) {
|
libpostal_address_parser_response_t *libpostal_parse_address(char *address, libpostal_address_parser_options_t options) {
|
||||||
address_parser_context_t *context = address_parser_context_new();
|
libpostal_address_parser_response_t *parsed = address_parser_parse(address, options.language, options.country);
|
||||||
libpostal_address_parser_response_t *parsed = address_parser_parse(address, options.language, options.country, context);
|
|
||||||
|
|
||||||
if (parsed == NULL) {
|
if (parsed == NULL) {
|
||||||
log_error("Parser returned NULL\n");
|
log_error("Parser returned NULL\n");
|
||||||
address_parser_context_destroy(context);
|
|
||||||
libpostal_address_parser_response_destroy(parsed);
|
libpostal_address_parser_response_destroy(parsed);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
address_parser_context_destroy(context);
|
|
||||||
|
|
||||||
return parsed;
|
return parsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user