Skip to content

Commit

Permalink
Merge pull request #14 from openvenues/1.0
Browse files Browse the repository at this point in the history
Update to libpostal 1.0 API
  • Loading branch information
albarrentine authored Apr 6, 2017
2 parents 771d545 + abe3167 commit ee27201
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 57 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
language: python
branches:
only:
- master
python:
- "2.7"
- "3.4"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ Before using the Python bindings, you must install the libpostal C library. Make

**On Ubuntu/Debian**
```
sudo apt-get install libsnappy-dev autoconf automake libtool python-dev pkg-config
sudo apt-get install curl autoconf automake libtool python-dev pkg-config
```
**On CentOS/RHEL**
```
sudo yum install snappy snappy-devel autoconf automake libtool python-devel pkgconfig
sudo yum install curl autoconf automake libtool python-devel pkgconfig
```
**On Mac OSX**
```
sudo brew install snappy autoconf automake libtool pkg-config
sudo brew install curl autoconf automake libtool pkg-config
```

**Installing libpostal**
Expand Down
17 changes: 9 additions & 8 deletions postal/expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,18 @@ def expand_address(address, languages=None, **kw):
return _expand.expand_address(address, languages=languages, **kw)

# Constants for address components
ADDRESS_NONE = _expand.ADDRESS_NONE
ADDRESS_ANY = _expand.ADDRESS_ANY
ADDRESS_NAME = _expand.ADDRESS_NAME
ADDRESS_HOUSE_NUMBER = _expand.ADDRESS_HOUSE_NUMBER
ADDRESS_STREET = _expand.ADDRESS_STREET
ADDRESS_UNIT = _expand.ADDRESS_UNIT
ADDRESS_LOCALITY = _expand.ADDRESS_LOCALITY
ADDRESS_ADMIN1 = _expand.ADDRESS_ADMIN1
ADDRESS_ADMIN2 = _expand.ADDRESS_ADMIN2
ADDRESS_ADMIN3 = _expand.ADDRESS_ADMIN3
ADDRESS_ADMIN4 = _expand.ADDRESS_ADMIN4
ADDRESS_ADMIN_OTHER = _expand.ADDRESS_ADMIN_OTHER
ADDRESS_COUNTRY = _expand.ADDRESS_COUNTRY
ADDRESS_NEIGHBORHOOD = _expand.ADDRESS_NEIGHBORHOOD
ADDRESS_LEVEL = _expand.ADDRESS_LEVEL
ADDRESS_STAIRCASE = _expand.ADDRESS_STAIRCASE
ADDRESS_ENTRANCE = _expand.ADDRESS_ENTRANCE
ADDRESS_CATEGORY = _expand.ADDRESS_CATEGORY
ADDRESS_NEAR = _expand.ADDRESS_NEAR
ADDRESS_TOPONYM = _expand.ADDRESS_TOPONYM
ADDRESS_POSTAL_CODE = _expand.ADDRESS_POSTAL_CODE
ADDRESS_PO_BOX = _expand.ADDRESS_PO_BOX
ADDRESS_ALL = _expand.ADDRESS_ALL
42 changes: 21 additions & 21 deletions postal/pyexpand.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct module_state {
static PyObject *py_expand(PyObject *self, PyObject *args, PyObject *keywords) {
PyObject *arg_input;
PyObject *arg_languages;
normalize_options_t options = get_libpostal_default_options();
libpostal_normalize_options_t options = libpostal_get_default_options();

PyObject *result = NULL;

Expand Down Expand Up @@ -172,7 +172,7 @@ static PyObject *py_expand(PyObject *self, PyObject *args, PyObject *keywords) {
#endif

if (language != NULL && item != Py_None) {
if (strlen(language) >= MAX_LANGUAGE_LEN) {
if (strlen(language) >= LIBPOSTAL_MAX_LANGUAGE_LEN) {
PyErr_SetString(PyExc_TypeError, "language was longer than a language code");
free(languages);
Py_DECREF(seq);
Expand All @@ -198,7 +198,7 @@ static PyObject *py_expand(PyObject *self, PyObject *args, PyObject *keywords) {
}

size_t num_expansions = 0;
char **expansions = expand_address(input, options, &num_expansions);
char **expansions = libpostal_expand_address(input, options, &num_expansions);

if (languages != NULL) {
for (int i = 0; i < num_languages; i++) {
Expand Down Expand Up @@ -228,10 +228,7 @@ static PyObject *py_expand(PyObject *self, PyObject *args, PyObject *keywords) {
}

exit_free_expansions:
for (int i = 0; i < num_expansions; i++) {
free(expansions[i]);
}
free(expansions);
libpostal_expansion_array_destroy(expansions, num_expansions);
exit_decref_str:
#ifndef IS_PY3K
Py_XDECREF(str_input);
Expand Down Expand Up @@ -316,20 +313,23 @@ init_expand(void) {
"Error loading libpostal");
}

PyModule_AddIntConstant(module, "ADDRESS_ANY", ADDRESS_ANY);
PyModule_AddIntConstant(module, "ADDRESS_NAME", ADDRESS_NAME);
PyModule_AddIntConstant(module, "ADDRESS_HOUSE_NUMBER", ADDRESS_HOUSE_NUMBER);
PyModule_AddIntConstant(module, "ADDRESS_STREET", ADDRESS_STREET);
PyModule_AddIntConstant(module, "ADDRESS_UNIT", ADDRESS_UNIT);
PyModule_AddIntConstant(module, "ADDRESS_LOCALITY", ADDRESS_LOCALITY);
PyModule_AddIntConstant(module, "ADDRESS_ADMIN1", ADDRESS_ADMIN1);
PyModule_AddIntConstant(module, "ADDRESS_ADMIN2", ADDRESS_ADMIN2);
PyModule_AddIntConstant(module, "ADDRESS_ADMIN3", ADDRESS_ADMIN3);
PyModule_AddIntConstant(module, "ADDRESS_ADMIN4", ADDRESS_ADMIN4);
PyModule_AddIntConstant(module, "ADDRESS_ADMIN_OTHER", ADDRESS_ADMIN_OTHER);
PyModule_AddIntConstant(module, "ADDRESS_COUNTRY", ADDRESS_COUNTRY);
PyModule_AddIntConstant(module, "ADDRESS_NEIGHBORHOOD", ADDRESS_NEIGHBORHOOD);
PyModule_AddIntConstant(module, "ADDRESS_ALL", ADDRESS_ALL);
PyModule_AddIntConstant(module, "ADDRESS_NONE", LIBPOSTAL_ADDRESS_NONE);
PyModule_AddIntConstant(module, "ADDRESS_ANY", LIBPOSTAL_ADDRESS_ANY);
PyModule_AddIntConstant(module, "ADDRESS_NAME", LIBPOSTAL_ADDRESS_NAME);
PyModule_AddIntConstant(module, "ADDRESS_HOUSE_NUMBER", LIBPOSTAL_ADDRESS_HOUSE_NUMBER);
PyModule_AddIntConstant(module, "ADDRESS_STREET", LIBPOSTAL_ADDRESS_STREET);
PyModule_AddIntConstant(module, "ADDRESS_UNIT", LIBPOSTAL_ADDRESS_UNIT);
PyModule_AddIntConstant(module, "ADDRESS_LEVEL", LIBPOSTAL_ADDRESS_LEVEL);
PyModule_AddIntConstant(module, "ADDRESS_STAIRCASE", LIBPOSTAL_ADDRESS_STAIRCASE);
PyModule_AddIntConstant(module, "ADDRESS_ENTRANCE", LIBPOSTAL_ADDRESS_ENTRANCE);

PyModule_AddIntConstant(module, "ADDRESS_CATEGORY", LIBPOSTAL_ADDRESS_CATEGORY);
PyModule_AddIntConstant(module, "ADDRESS_NEAR", LIBPOSTAL_ADDRESS_NEAR);

PyModule_AddIntConstant(module, "ADDRESS_TOPONYM", LIBPOSTAL_ADDRESS_TOPONYM);
PyModule_AddIntConstant(module, "ADDRESS_POSTAL_CODE", LIBPOSTAL_ADDRESS_POSTAL_CODE);
PyModule_AddIntConstant(module, "ADDRESS_PO_BOX", LIBPOSTAL_ADDRESS_PO_BOX);
PyModule_AddIntConstant(module, "ADDRESS_ALL", LIBPOSTAL_ADDRESS_ALL);

#ifndef IS_PY3K
Py_AtExit(&cleanup_libpostal);
Expand Down
6 changes: 3 additions & 3 deletions postal/pyparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ static PyObject *py_parse_address(PyObject *self, PyObject *args, PyObject *keyw
}
}

address_parser_options_t options = get_libpostal_address_parser_default_options();
libpostal_address_parser_options_t options = libpostal_get_address_parser_default_options();
options.language = language;
options.country = country;

address_parser_response_t *parsed = parse_address(input, options);
libpostal_address_parser_response_t *parsed = libpostal_parse_address(input, options);
if (parsed == NULL) {
goto exit_decref_country_str;
}
Expand Down Expand Up @@ -184,7 +184,7 @@ static PyObject *py_parse_address(PyObject *self, PyObject *args, PyObject *keyw
}

exit_destroy_response:
address_parser_response_destroy(parsed);
libpostal_address_parser_response_destroy(parsed);
exit_decref_country_str:
#ifndef IS_PY3K
if (str_country != Py_None) {
Expand Down
2 changes: 1 addition & 1 deletion postal/tests/test_expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_expansions(self):

self.have_expansion_in_common('Thirty W 26th St Fl #7', '30 West Twenty-sixth Street Floor Number 7', languages=['en'])

self.contained_in_expansions('Friedrichstraße 128, Berlin, Germany', 'friedrich strasse 128 berlin germany')
self.contained_in_expansions('Friedrichstraße 128, Berlin, Germany', 'friedrich straße 128 berlin germany')

self.contained_in_expansions('MAPLE ST.', 'maple street')
self.contained_in_expansions('ST ISIDORE DR', 'saint isidore drive')
Expand Down
20 changes: 0 additions & 20 deletions postal/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,6 @@ def test_parses(self):
'country': 'usa'
})

self.contains_components('whole foods ny', {'house': 'whole foods', 'state': 'ny'})
self.contains_components('1917/2 Pike Drive', {
'house_number': '1917 / 2',
'road': 'pike drive'
})
self.contains_components('3437 warwickshire rd,pa', {
'house_number': '3437',
'road': 'warwickshire rd',
'state': 'pa'
})
self.contains_components('3437 warwickshire rd, pa', {
'house_number': '3437',
'road': 'warwickshire rd',
'state': 'pa'
})
self.contains_components('3437 warwickshire rd pa', {
'house_number': '3437',
'road': 'warwickshire rd',
'state': 'pa'
})

if __name__ == '__main__':
unittest.main()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
def main():
setup(
name='postal',
version='0.3',
version='1.0',
install_requires=[
'six',
],
Expand Down

0 comments on commit ee27201

Please sign in to comment.