52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# File 'ext/expand/expand.c', line 52
VALUE rb_expand_address(int argc, VALUE *argv, VALUE self) {
if (argc < 2) { // there should only be 1 or 2 arguments
rb_raise(rb_eArgError, "Usage: expand_address(address, options = {})");
return Qnil;
}
VALUE input;
VALUE rb_options;
rb_scan_args(argc, argv, "2", &input, &rb_options);
if (TYPE(input) != T_STRING) {
rb_raise(rb_eArgError, "input must be a string");
return Qnil;
}
if (TYPE(rb_options) != T_HASH) {
rb_raise(rb_eArgError, "options must be a hash");
return Qnil;
}
VALUE rb_languages = hash_get_symbol_or_string(rb_options, "languages");
if (rb_languages != Qnil && (TYPE(rb_languages) != T_ARRAY || RARRAY_LEN(rb_languages) == 0)) {
rb_raise(rb_eArgError, "options['languages'] must be a non-empty array");
return Qnil;
}
size_t len_languages = 0;
if (rb_languages != Qnil) {
len_languages = (size_t)RARRAY_LEN(rb_languages);
}
libpostal_normalize_options_t options = libpostal_get_default_options();
size_t i;
char **languages = NULL;
size_t num_languages = 0;
if (len_languages > 0) {
languages = malloc(sizeof(char *) * len_languages);
for (i = 0; i < len_languages; i++) {
VALUE rb_lang = rb_ary_entry(rb_languages, (int)i);
if (rb_lang != Qnil && TYPE(rb_lang) == T_STRING) {
size_t rb_lang_len = RSTRING_LEN(rb_lang);
if (rb_lang_len > 0 && rb_lang_len < LIBPOSTAL_MAX_LANGUAGE_LEN) {
char *lang = RSTRING_PTR(rb_lang);
languages[num_languages++] = lang;
}
}
}
}
options.num_languages = num_languages;
options.languages = languages;
char *address = RSTRING_PTR(input);
VALUE address_components = hash_get_symbol_or_string(rb_options, "languages");
if (address_components != Qnil && TYPE(address_components) == T_FIXNUM) {
options.address_components = (uint16_t) NUM2UINT(address_components);
}
VALUE latin_ascii = hash_get_symbol_or_string(rb_options, "latin_ascii");
if (latin_ascii != Qnil) {
options.latin_ascii = RTEST(latin_ascii);
}
VALUE transliterate = hash_get_symbol_or_string(rb_options, "transliterate");
if (transliterate != Qnil) {
options.transliterate = RTEST(transliterate);
}
VALUE strip_accents = hash_get_symbol_or_string(rb_options, "strip_accents");
if (strip_accents != Qnil) {
options.strip_accents = RTEST(strip_accents);
}
VALUE decompose = hash_get_symbol_or_string(rb_options, "decompose");
if (decompose != Qnil) {
options.decompose = RTEST(decompose);
}
VALUE lowercase = hash_get_symbol_or_string(rb_options, "lowercase");
if (lowercase != Qnil) {
options.lowercase = RTEST(lowercase);
}
VALUE trim_string = hash_get_symbol_or_string(rb_options, "trim_string");
if (trim_string != Qnil) {
options.trim_string = RTEST(trim_string);
}
VALUE drop_parentheticals = hash_get_symbol_or_string(rb_options, "drop_parentheticals");
if (drop_parentheticals != Qnil) {
options.drop_parentheticals = RTEST(drop_parentheticals);
}
VALUE replace_numeric_hyphens = hash_get_symbol_or_string(rb_options, "replace_numeric_hyphens");
if (replace_numeric_hyphens != Qnil) {
options.replace_numeric_hyphens = RTEST(replace_numeric_hyphens);
}
VALUE delete_numeric_hyphens = hash_get_symbol_or_string(rb_options, "delete_numeric_hyphens");
if (delete_numeric_hyphens != Qnil) {
options.delete_numeric_hyphens = RTEST(delete_numeric_hyphens);
}
VALUE split_alpha_from_numeric = hash_get_symbol_or_string(rb_options, "split_alpha_from_numeric");
if (split_alpha_from_numeric != Qnil) {
options.split_alpha_from_numeric = RTEST(split_alpha_from_numeric);
}
VALUE replace_word_hyphens = hash_get_symbol_or_string(rb_options, "replace_word_hyphens");
if (replace_word_hyphens != Qnil) {
options.replace_word_hyphens = RTEST(replace_word_hyphens);
}
VALUE delete_word_hyphens = hash_get_symbol_or_string(rb_options, "delete_word_hyphens");
if (delete_word_hyphens != Qnil) {
options.delete_word_hyphens = RTEST(delete_word_hyphens);
}
VALUE delete_final_periods = hash_get_symbol_or_string(rb_options, "delete_final_periods");
if (delete_final_periods != Qnil) {
options.delete_final_periods = RTEST(delete_final_periods);
}
VALUE delete_acronym_periods = hash_get_symbol_or_string(rb_options, "delete_acronym_periods");
if (delete_acronym_periods != Qnil) {
options.delete_acronym_periods = RTEST(delete_acronym_periods);
}
VALUE drop_english_possessives = hash_get_symbol_or_string(rb_options, "drop_english_possessives");
if (drop_english_possessives != Qnil) {
options.drop_english_possessives = RTEST(drop_english_possessives);
}
VALUE delete_apostrophes = hash_get_symbol_or_string(rb_options, "delete_apostrophes");
if (delete_apostrophes != Qnil) {
options.delete_apostrophes = RTEST(delete_apostrophes);
}
VALUE expand_numex = hash_get_symbol_or_string(rb_options, "expand_numex");
if (expand_numex != Qnil) {
options.expand_numex = RTEST(expand_numex);
}
VALUE roman_numerals = hash_get_symbol_or_string(rb_options, "roman_numerals");
if (roman_numerals != Qnil) {
options.roman_numerals = RTEST(roman_numerals);
}
size_t num_expansions = 0;
char **expansions = libpostal_expand_address(address, options, &num_expansions);
VALUE rb_expansions = rb_ary_new2(num_expansions);
for (size_t i = 0; i < num_expansions; i++) {
char *expansion = expansions[i];
VALUE rb_expansion = rb_str_new2(expansion);
rb_ary_store(rb_expansions, i, rb_expansion);
free(expansion);
}
free(expansions);
return rb_expansions;
}
|