moskytw/zipcodetw — pull request #3 by uranusjr (2014-02-24 04:27:02+00:00)
Title: Implement compensation for addresses without a level-1 token

Take 2 of #2, rebased to the current HEAD (`dev` branch).

One thing I forgot to mention in the previous PR: This implementation requires a change of database schema. The current installation, if I understand correctly, rebuilds the database every time there's an upgrade, so this shouldn't be a big problem. Please correct me if I'm wrong.

Following is the original PR message:

--------

When an address lacks a level-1 token, e.g.

高雄市 大仁路 6 號
====== ====== ====
t[0] t[1] t[2]

* "t" is the token list of the compiled Address object.

the second token in the list (大仁路) will contain a level-2 unit (路 in this case), instead of a level-1 one.

Steps are taken when compensating for an address as follows:

1. When looking up rules from the database.

`t[1]` is checked whether it is really a level-1 token. If it is instead a level-2 one, subsequent tokens are shifted back so that they can be matched with the correct column.

2. When an address is matched with the rule.

`t[1]` is again checked. An additional check on `t[0]` is also performed here because addresses such as "信義路一段2號" are compiled like:

信義路 1 段 2 號
====== ==== ====
t[0] t[1] t[2]

causing it to match the above criteria. If a level-1 token is truely missing, The level-1 token of the current Rule object is used to fill it in.

3. When an address is matched.

If a given address matches only one rule without ambiguity, we can accept the compensation. Otherwise (two or more rules are possible), the compensation won't work, and we fall back to gradual matching as before.