1 #include "place.h"
2 #include "address_parser.h"
3 
is_address_text_component(char * label)4 static inline bool is_address_text_component(char *label) {
5     return (string_equals(label, ADDRESS_PARSER_LABEL_HOUSE) ||
6             string_equals(label, ADDRESS_PARSER_LABEL_ROAD) ||
7             string_equals(label, ADDRESS_PARSER_LABEL_METRO_STATION) ||
8             string_equals(label, ADDRESS_PARSER_LABEL_SUBURB) ||
9             string_equals(label, ADDRESS_PARSER_LABEL_CITY_DISTRICT) ||
10             string_equals(label, ADDRESS_PARSER_LABEL_CITY) ||
11             string_equals(label, ADDRESS_PARSER_LABEL_STATE_DISTRICT) ||
12             string_equals(label, ADDRESS_PARSER_LABEL_ISLAND) ||
13             string_equals(label, ADDRESS_PARSER_LABEL_STATE) ||
14             string_equals(label, ADDRESS_PARSER_LABEL_COUNTRY_REGION) ||
15             string_equals(label, ADDRESS_PARSER_LABEL_COUNTRY) ||
16             string_equals(label, ADDRESS_PARSER_LABEL_WORLD_REGION)
17             );
18 }
19 
place_languages(size_t num_components,char ** labels,char ** values)20 language_classifier_response_t *place_languages(size_t num_components, char **labels, char **values) {
21     if (num_components == 0 || values == NULL || labels == NULL) return NULL;
22 
23     language_classifier_response_t *lang_response = NULL;
24 
25     char *label;
26     char *value;
27 
28     size_t total_size = 0;
29     for (size_t i = 0; i < num_components; i++) {
30         value = values[i];
31         label = labels[i];
32         if (is_address_text_component(label)) {
33             total_size += strlen(value);
34             // extra char for spaces
35             if (i < num_components - 1) {
36                 total_size++;
37             }
38         }
39     }
40 
41     char_array *combined = char_array_new_size(total_size);
42     if (combined == NULL) {
43         return NULL;
44     }
45 
46     for (size_t i = 0; i < num_components; i++) {
47         value = values[i];
48         label = labels[i];
49         if (is_address_text_component(label)) {
50             char_array_cat(combined, value);
51             if (i < num_components - 1) {
52                 char_array_cat(combined, " ");
53             }
54         }
55     }
56 
57     char *combined_input = char_array_get_string(combined);
58 
59     lang_response = classify_languages(combined_input);
60 
61     char_array_destroy(combined);
62     return lang_response;
63 }
64 
65 
66 
place_new(void)67 place_t *place_new(void) {
68     place_t *place = calloc(1, sizeof(place_t));
69     return place;
70 }
71 
place_destroy(place_t * place)72 void place_destroy(place_t *place) {
73     if (place == NULL) return;
74     free(place);
75 }
76 
77 
place_from_components(size_t num_components,char ** labels,char ** values)78 place_t *place_from_components(size_t num_components, char **labels, char **values) {
79     if (num_components == 0 || labels == NULL || values == NULL) {
80         return NULL;
81     }
82 
83     place_t *place = place_new();
84     if (place == NULL) return NULL;
85 
86     for (size_t i = 0; i < num_components; i++) {
87         char *value = values[i];
88         char *label = labels[i];
89         if (string_equals(label, ADDRESS_PARSER_LABEL_ROAD)) {
90             if (place->street == NULL) {
91                 place->street = value;
92             }
93         } else if (string_equals(label, ADDRESS_PARSER_LABEL_HOUSE)) {
94             if (place->name == NULL) {
95                 place->name = value;
96             }
97         } else if (string_equals(label, ADDRESS_PARSER_LABEL_HOUSE_NUMBER)) {
98             if (place->house_number == NULL) {
99                 place->house_number = value;
100             }
101         } else if (string_equals(label, ADDRESS_PARSER_LABEL_POSTAL_CODE)) {
102             if (place->postal_code == NULL) {
103                 place->postal_code = value;
104             }
105         } else if (string_equals(label, ADDRESS_PARSER_LABEL_CITY)) {
106             if (place->city == NULL) {
107                 place->city = value;
108             }
109         } else if (string_equals(label, ADDRESS_PARSER_LABEL_STATE)) {
110             if (place->state == NULL) {
111                 place->state = value;
112             }
113         } else if (string_equals(label, ADDRESS_PARSER_LABEL_COUNTRY)) {
114             if (place->country == NULL) {
115                 place->country = value;
116             }
117         } else if (string_equals(label, ADDRESS_PARSER_LABEL_SUBURB)) {
118             if (place->suburb == NULL) {
119                 place->suburb = value;
120             }
121         }  else if (string_equals(label, ADDRESS_PARSER_LABEL_CITY_DISTRICT)) {
122             if (place->city_district == NULL) {
123                 place->city_district = value;
124             }
125         } else if (string_equals(label, ADDRESS_PARSER_LABEL_STATE_DISTRICT)) {
126             if (place->state_district == NULL) {
127                 place->state_district = value;
128             }
129         } else if (string_equals(label, ADDRESS_PARSER_LABEL_COUNTRY_REGION)) {
130             if (place->country_region == NULL) {
131                 place->country_region = value;
132             }
133         } else if (string_equals(label, ADDRESS_PARSER_LABEL_ISLAND)) {
134             if (place->island == NULL) {
135                 place->island = value;
136             }
137         } else if (string_equals(label, ADDRESS_PARSER_LABEL_WORLD_REGION)) {
138             if (place->world_region == NULL) {
139                 place->world_region = value;
140             }
141         } else if (string_equals(label, ADDRESS_PARSER_LABEL_UNIT)) {
142             if (place->unit == NULL) {
143                 place->unit = value;
144             }
145         } else if (string_equals(label, ADDRESS_PARSER_LABEL_TELEPHONE)) {
146             if (place->telephone == NULL) {
147                 place->telephone = value;
148             }
149         } else if (string_equals(label, ADDRESS_PARSER_LABEL_WEBSITE)) {
150             if (place->website == NULL) {
151                 place->website = value;
152             }
153         } else if (string_equals(label, ADDRESS_PARSER_LABEL_LEVEL)) {
154             if (place->level == NULL) {
155                 place->level = value;
156             }
157         } else if (string_equals(label, ADDRESS_PARSER_LABEL_PO_BOX)) {
158             if (place->po_box == NULL) {
159                 place->po_box = value;
160             }
161         } else if (string_equals(label, ADDRESS_PARSER_LABEL_BUILDING)) {
162             if (place->building == NULL) {
163                 place->building = value;
164             }
165         } else if (string_equals(label, ADDRESS_PARSER_LABEL_STAIRCASE)) {
166             if (place->staircase == NULL) {
167                 place->staircase = value;
168             }
169         } else if (string_equals(label, ADDRESS_PARSER_LABEL_ENTRANCE)) {
170             if (place->entrance == NULL) {
171                 place->entrance = value;
172             }
173         } else if (string_equals(label, ADDRESS_PARSER_LABEL_METRO_STATION)) {
174             if (place->metro_station == NULL) {
175                 place->metro_station = value;
176             }
177         }
178     }
179 
180     return place;
181 }
182