Comments (4)
Easiest route from my experience is:
{k: v for (v, k) in parse_address('330 11th Ave Apt 1')}
from pypostal.
Yes, that's what I did. What I first did not see was that the order of the output tuple was always the same. The issue is solved, thank you!
from pypostal.
Hi @antimirov, yes, the dictionary comprehension should do the trick.
As a quick background on why it's formatted this way, the underlying sequence model takes an array of tokens like this:
['1010', 'easy', 'st', 'ottawa', 'k1a', '0b1']
and predicts an equal-length array of labels, one per token, like this:
['house_number', 'road', 'road', 'city', 'postcode', 'postcode']
For convenience, we roll those up into phrases wherever there's a contiguous sequence of the same label. However, a dictionary would make an implicit assumption that there's only one of each address component.
Most of the time, and for most components, this is true. The output roughly corresponds to fields that one might store in a database or search index. It's usually a parser mistake when there are duplicate components, but in, say, India, the UK, Ireland, etc. it's possible to see something like "{venue/company name} {road} {house name}" in which case there might be two different legitimate components tagged house
. While a dictionary is more convenient to work with, we try not to lose any information from the response, so it was either a dictionary of lists or a list of tuples. Decided on the latter as it used less memory and can be converted easily to a dict as needed.
Note: the address parser client from the C library prints something that looks like JSON, but it's not necessarily valid, as there can be duplicate keys. It's just easier to visualize the output as pseudo-JSON on the command line.
from pypostal.
Would it be possible to return the tuples in the opposite order? If the keys come first you can do dict(parse_address('330 11th Ave Apt 1'))
which is both more succinct and performant.
On a related topic, it might be nice to think of this as a multi-dict. This is pretty common in the web world, e.g. for headers.
It's essentially a data structure with both references to the dictionary and list of tuples. When accessed as a dictionary duplicate keys are either dropped or merged. When iterated over duplicates are preserved.
from pypostal.
Related Issues (20)
- Cannot install pypostal on Google Colab HOT 2
- Dockerized CentOS Application - ImportError: libpostal.so.1: cannot open shared object file
- Install on AWS Lambda cannot import name '_parser' HOT 1
- "pip install postal" HOT 2
- Symbol not found: _libpostal_address_parser_response_destroy on macos M1 HOT 4
- Unable to run setup.py build_ext --inplace
- ImportError: DLL load failed while importing _parser: The specified module could not be found HOT 1
- libpostal/libpostal.h: No such file or directory HOT 1
- SystemError: initialization of _parser raised unreported exception HOT 2
- ERROR: hashes = libpostal_near_dupe_name_hashes(input, options, &num_hashes) HOT 2
- pip installation with Python 3.10 HOT 3
- Install pypostal on python3.6
- No such file or directory: 'README.rst' HOT 1
- Error building on Windows HOT 8
- ModuleNotFoundError: No module named 'postal' HOT 1
- It seems there is a memory leak in pyexpand.c
- Cannot install the package using pip HOT 3
- Feature Request Publish this library as a wheel
- Installing pypostal on OSX M1, Python 3.10 error HOT 1
- pypostal is not installing properly on M1 Mac with or w/o Rosetta for python 3.7 or 3.8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pypostal.