Skip to content

Commit

Permalink
[api/compat] PR #465 redefined the language classifier response struc…
Browse files Browse the repository at this point in the history
…t in the API and was casting between incompatible pointer types. Using the exported struct throughout.
  • Loading branch information
albarrentine committed Jan 30, 2025
1 parent 7245a17 commit 0540d7c
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/expand.c
Original file line number Diff line number Diff line change
Expand Up @@ -1567,7 +1567,7 @@ cstring_array *expand_address_phrase_option(char *input, libpostal_normalize_opt

size_t len = strlen(input);

language_classifier_response_t *lang_response = NULL;
libpostal_language_classifier_response_t *lang_response = NULL;

if (options.num_languages == 0) {
lang_response = classify_languages(input);
Expand Down Expand Up @@ -1627,7 +1627,7 @@ cstring_array *expand_address_phrase_option(char *input, libpostal_normalize_opt
kh_destroy(str_set, unique_strings);

if (lang_response != NULL) {
language_classifier_response_destroy(lang_response);
libpostal_language_classifier_response_destroy(lang_response);
}

char_array_destroy(temp_string);
Expand Down
8 changes: 4 additions & 4 deletions src/language_classifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ language_classifier_t *get_language_classifier(void) {
return language_classifier;
}

void language_classifier_response_destroy(language_classifier_response_t *self) {
void language_classifier_response_destroy(libpostal_language_classifier_response_t *self) {
if (self == NULL) return;
if (self->languages != NULL) {
free(self->languages);
Expand All @@ -59,7 +59,7 @@ void language_classifier_response_destroy(language_classifier_response_t *self)
free(self);
}

language_classifier_response_t *classify_languages(char *address) {
libpostal_language_classifier_response_t *classify_languages(char *address) {
language_classifier_t *classifier = get_language_classifier();

if (classifier == NULL) {
Expand Down Expand Up @@ -88,7 +88,7 @@ language_classifier_response_t *classify_languages(char *address) {
size_t n = classifier->num_labels;
double_matrix_t *p_y = double_matrix_new_zeros(1, n);

language_classifier_response_t *response = NULL;
libpostal_language_classifier_response_t *response = NULL;
bool model_exp = false;
if (classifier->weights_type == MATRIX_DENSE) {
model_exp = logistic_regression_model_expectation(classifier->weights.dense, x, p_y);
Expand Down Expand Up @@ -129,7 +129,7 @@ language_classifier_response_t *classify_languages(char *address) {

free(indices);

response = malloc(sizeof(language_classifier_response_t));
response = malloc(sizeof(libpostal_language_classifier_response_t));
response->num_languages = num_languages;
response->languages = languages;
response->probs = probs;
Expand Down
15 changes: 5 additions & 10 deletions src/language_classifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <stdint.h>
#include <stdbool.h>

#include "libpostal.h"

#include "collections.h"
#include "language_features.h"
#include "logistic_regression.h"
Expand All @@ -29,21 +31,14 @@ typedef struct language_classifier {
} weights;
} language_classifier_t;


typedef struct language_classifier_response {
size_t num_languages;
char **languages;
double *probs;
} language_classifier_response_t;

// General usage

language_classifier_t *language_classifier_new(void);
language_classifier_t *get_language_classifier(void);
language_classifier_t *get_language_classifier_country(void);

language_classifier_response_t *classify_languages(char *address);
void language_classifier_response_destroy(language_classifier_response_t *self);
libpostal_language_classifier_response_t *classify_languages(char *address);
void language_classifier_response_destroy(libpostal_language_classifier_response_t *self);

void language_classifier_destroy(language_classifier_t *self);

Expand All @@ -58,4 +53,4 @@ bool language_classifier_module_setup(char *dir);
void language_classifier_module_teardown(void);


#endif
#endif
2 changes: 1 addition & 1 deletion src/language_classifier_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ int main(int argc, char **argv) {
}


language_classifier_response_t *response = classify_languages(address);
libpostal_language_classifier_response_t *response = classify_languages(address);
if (response == NULL) {
printf("Could not classify language\n");
exit(EXIT_FAILURE);
Expand Down
2 changes: 1 addition & 1 deletion src/language_classifier_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ double test_accuracy(char *filename) {
continue;
}

language_classifier_response_t *response = classify_languages(address);
libpostal_language_classifier_response_t *response = classify_languages(address);
if (response == NULL || response->num_languages == 0) {
printf("%s\tNULL\t%s\n", language, address);
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/libpostal.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ char **libpostal_near_dupe_hashes_languages(size_t num_components, char **labels


char **libpostal_place_languages(size_t num_components, char **labels, char **values, size_t *num_languages) {
language_classifier_response_t *lang_response = place_languages(num_components, labels, values);
libpostal_language_classifier_response_t *lang_response = place_languages(num_components, labels, values);
if (lang_response == NULL) {
*num_languages = 0;
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/near_dupe.c
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ cstring_array *near_dupe_hashes_languages(size_t num_components, char **labels,

libpostal_normalize_options_t normalize_options = libpostal_get_default_options();

language_classifier_response_t *lang_response = NULL;
libpostal_language_classifier_response_t *lang_response = NULL;

if (num_languages == 0) {
lang_response = place_languages(num_components, labels, values);
Expand Down
4 changes: 2 additions & 2 deletions src/place.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ static inline bool is_address_text_component(char *label) {
);
}

language_classifier_response_t *place_languages(size_t num_components, char **labels, char **values) {
libpostal_language_classifier_response_t *place_languages(size_t num_components, char **labels, char **values) {
if (num_components == 0 || values == NULL || labels == NULL) return NULL;

language_classifier_response_t *lang_response = NULL;
libpostal_language_classifier_response_t *lang_response = NULL;

char *label;
char *value;
Expand Down
4 changes: 2 additions & 2 deletions src/place.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ typedef struct place {
char *website;
} place_t;

language_classifier_response_t *place_languages(size_t num_components, char **labels, char **values);
libpostal_language_classifier_response_t *place_languages(size_t num_components, char **labels, char **values);

place_t *place_new(void);

place_t *place_from_components(size_t num_components, char **labels, char **values);

void place_destroy(place_t *place);

#endif
#endif

0 comments on commit 0540d7c

Please sign in to comment.