geoalign 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
geoalign-0.1.0/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
geoalign-0.1.0/NOTICE ADDED
@@ -0,0 +1,56 @@
1
+ geoalign
2
+ 本包的代码按 Apache-2.0 许可发布(见 LICENSE)。本包带的数据文件里有第三方的内容,说明如下。
3
+
4
+ 1. geoalign/data/confusion.json 里的拼音("pinyin"、"pinyin_main" 两部分)
5
+ 由 pypinyin 0.55.0 生成。pypinyin 的单字拼音数据来自 pinyin-data 项目。两者都按 MIT 许可发布,版权声明和许可全文如下。
6
+
7
+ ----- pypinyin(https://github.com/mozillazg/python-pinyin) -----
8
+ The MIT License (MIT)
9
+
10
+ Copyright (c) 2016 mozillazg, 闲耘 <hotoo.cn@gmail.com>
11
+
12
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
13
+ this software and associated documentation files (the "Software"), to deal in
14
+ the Software without restriction, including without limitation the rights to
15
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
16
+ the Software, and to permit persons to whom the Software is furnished to do so,
17
+ subject to the following conditions:
18
+
19
+ The above copyright notice and this permission notice shall be included in all
20
+ copies or substantial portions of the Software.
21
+
22
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
24
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
25
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
26
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
+
29
+ ----- pinyin-data(https://github.com/mozillazg/pinyin-data) -----
30
+ The MIT License (MIT)
31
+
32
+ Copyright (c) 2016 mozillazg
33
+
34
+ Permission is hereby granted, free of charge, to any person obtaining a copy
35
+ of this software and associated documentation files (the "Software"), to deal
36
+ in the Software without restriction, including without limitation the rights
37
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
38
+ copies of the Software, and to permit persons to whom the Software is
39
+ furnished to do so, subject to the following conditions:
40
+
41
+ The above copyright notice and this permission notice shall be included in all
42
+ copies or substantial portions of the Software.
43
+
44
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
45
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
46
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
47
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
48
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
49
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
50
+ SOFTWARE.
51
+
52
+ 2. geoalign/data/confusion.json 里的形近字("shape" 部分)
53
+ 用 Noto Sans CJK SC 字体把字画成图、比较图的相似度得到(方法见 tools/build_confusion.py)。
54
+ 本包不带字体文件,只带计算出的字对。Noto Sans CJK 按 SIL Open Font License 1.1 发布(https://github.com/notofonts/noto-cjk)。
55
+
56
+ 3. geoalign/data/ 里的行政区划代码、名称和变更来自民政部公开发布的文件。出处和许可见数据集的说明。
@@ -0,0 +1,354 @@
1
+ Metadata-Version: 2.4
2
+ Name: geoalign
3
+ Version: 0.1.0
4
+ Summary: Map Chinese administrative-division names (county level and above) to official codes, and convert codes between years 2012-2025. 把中国县级以上行政区划的名称对到官方代码。
5
+ Author: World Insight Lab
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://huggingface.co/datasets/WorldInsightLab/china-admin-divisions
8
+ Project-URL: Documentation, https://huggingface.co/datasets/WorldInsightLab/china-admin-divisions/blob/main/geoalign/README.md
9
+ Project-URL: Source, https://huggingface.co/datasets/WorldInsightLab/china-admin-divisions/tree/main/geoalign
10
+ Project-URL: Dataset, https://huggingface.co/datasets/WorldInsightLab/china-admin-divisions
11
+ Keywords: china,administrative divisions,gazetteer,geocoding,entity linking,行政区划,地名
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Intended Audience :: Science/Research
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3 :: Only
17
+ Classifier: Natural Language :: Chinese (Simplified)
18
+ Classifier: Natural Language :: English
19
+ Classifier: Topic :: Scientific/Engineering :: GIS
20
+ Classifier: Topic :: Text Processing :: Linguistic
21
+ Requires-Python: >=3.8
22
+ Description-Content-Type: text/markdown
23
+ License-File: LICENSE
24
+ License-File: NOTICE
25
+ Provides-Extra: embed
26
+ Requires-Dist: sentence-transformers; extra == "embed"
27
+ Requires-Dist: torch; extra == "embed"
28
+ Dynamic: license-file
29
+
30
+ # geoalign
31
+
32
+ Map Chinese administrative-division names (county level and above) to official codes, and convert codes between years 2012–2025.
33
+
34
+ 把中国县级以上行政区划的名称写法对到官方代码,并在 2012 至 2025 年之间换算代码。
35
+
36
+ - Data / 数据: [WorldInsightLab/china-admin-divisions](https://huggingface.co/datasets/WorldInsightLab/china-admin-divisions)
37
+ - License / 许可: code Apache-2.0 (`LICENSE`); third-party notices in `NOTICE` / 代码 Apache-2.0,第三方声明见 `NOTICE`
38
+ - Version / 版本: 0.1.0
39
+
40
+ [English](#english) · [中文](#中文)
41
+
42
+ ---
43
+
44
+ ## English
45
+
46
+ ### What it does
47
+
48
+ 1. Turns a written place name into an administrative-division code. You can give a year and a parent name.
49
+ 2. Converts a code from one year to another. For splits and mergers it returns every destination.
50
+ 3. Shows the history of a code.
51
+
52
+ When the library is not sure, it returns no code and lists all candidates instead.
53
+
54
+ ### Install
55
+
56
+ From PyPI:
57
+
58
+ ```
59
+ pip install geoalign
60
+ ```
61
+
62
+ Or from the wheel in the dataset repository:
63
+
64
+ ```
65
+ pip install https://huggingface.co/datasets/WorldInsightLab/china-admin-divisions/resolve/main/geoalign/dist/geoalign-0.1.0-py3-none-any.whl
66
+ ```
67
+
68
+ From source (run in the `geoalign/` directory, where `pyproject.toml` is):
69
+
70
+ ```
71
+ pip install .
72
+ ```
73
+
74
+ - Lookup and the typo rules use only the Python standard library (Python 3.8+). No GPU.
75
+ - The embedding fallback is optional. It needs `sentence-transformers` and `torch` (from source: `pip install ".[embed]"`), and you need to supply a Chinese embedding model yourself. The public benchmark does not cover the embedding fallback.
76
+
77
+ ### Scope
78
+
79
+ 1. Year-end code tables for 2012–2025, county level and above, all from official Ministry of Civil Affairs (MCA) pages. The data and its sources are described in the dataset card.
80
+ 2. If the year is earlier than 2012, the library gives no code, and `note` says the data starts in 2012.
81
+ 3. If the year is later than 2025, the 2025 table is used.
82
+ 4. **County level and above only. Do not pass township names.** After removing generic suffixes such as 镇, 乡, 街道, 1,358 of 27,004 distinct township spellings (5.0%) are confidently matched to a county-level-or-above unit (e.g. “三台” → 三台县). Counted per township (a name that occurs in several places counts each time), about 12% of 38,742 townships (4,497) are matched this way. The matched unit is often not the county that contains the township.
83
+
84
+ ### Usage
85
+
86
+ The outputs below were produced by the wheel above, installed in a clean virtual environment.
87
+
88
+ ```python
89
+ from geoalign import Gazetteer
90
+ g = Gazetteer()
91
+
92
+ m = g.align("密云县") # no year: map to the latest code
93
+ (m.code, m.name, m.confidence, m.method, m.year)
94
+ # ('110118', '密云区', 'medium', 'history', 2025)
95
+
96
+ g.align("密云县", year=2013).code # '110228' — still 密云县 at the end of 2013
97
+ g.align("朝阳区", parent="长春市").code # '220104'
98
+
99
+ m = g.align("朝阳区") # duplicate name, no parent
100
+ (m.code, m.best, m.confidence, m.method)
101
+ # (None, '110105', 'low', 'exact') # not sure: no code
102
+ [(c.code, c.name, c.parent_name) for c in m.candidates]
103
+ # [('110105', '朝阳区', '北京市'), ('220104', '朝阳区', '吉林省长春市')]
104
+
105
+ m = g.align("天水市麦积区") # parent and name written together
106
+ (m.code, m.name, m.confidence, m.method)
107
+ # ('620503', '麦积区', 'medium', 'split')
108
+
109
+ m = g.align("密云县", year=1990) # earlier than 2012
110
+ (m.code, m.confidence)
111
+ # (None, 'low')
112
+
113
+ g.to_year("110228", 2025)
114
+ # YearResult(codes=['110118'], names=['密云区'], exact=True,
115
+ # steps=['2014→2015:110228 密云县 → 110118 密云区(撤县设区)'])
116
+
117
+ g.to_year("130103", 2016) # 石家庄 桥东区, split in 2014
118
+ # YearResult(codes=['130102', '130104'], names=['长安区', '桥西区'], exact=False,
119
+ # steps=['2014→2015:130103 桥东区 → 130102 长安区(拆分)',
120
+ # '2014→2015:130103 桥东区 → 130104 桥西区(拆分)'])
121
+
122
+ g.history("330103") # 杭州 下城区, merged into 拱墅区 in 2021
123
+ # {'code': '330103', 'names': [{'name': '下城区', 'from': 2012, 'to': 2020}],
124
+ # 'events': [{'year': 2021, 'type': '合并', 'relation': '多对一', 'partial': False,
125
+ # 'old_code': '330103', 'old_name': '下城区', 'new_code': '330105', 'new_name': '拱墅区',
126
+ # 'basis': '官方变更情况', 'source_url': 'http://xzqh.mca.gov.cn/description?dcpid=2021'}]}
127
+ ```
128
+
129
+ Fields returned by `align`:
130
+
131
+ | Field | Meaning |
132
+ |---|---|
133
+ | `code` | The code when the library is sure; otherwise `None` |
134
+ | `best` | The top candidate. Given even when not sure, for callers that must pick one |
135
+ | `name` | Standard name of `code` or `best` |
136
+ | `confidence` | `high`: unique full name. `medium`: unique after short form, former name, or splitting a parent+name string. `low`: typo rules, embedding, found only after dropping the parent, several candidates, generic name, or year before 2012. `none`: not found |
137
+ | `method` | `exact`, `alias`, `history`, `split`, `no_parent`, `fuzzy`, `embedding`, `none` |
138
+ | `candidates` | All candidates. For duplicate names, all units with that name |
139
+ | `year` | The year of the table used |
140
+ | `source_grade` | Source grade of that year's table. In this release always `A` (official page) |
141
+ | `rebuilt` | Whether that year's table was rebuilt. In this release always `False` |
142
+ | `note` | Extra explanation |
143
+
144
+ When `confidence` is `low` or `none`, `code` is `None`.
145
+
146
+ Generic names (“市区”, “新区”, “城区”, “郊区”, “矿区”, “东区”, “西区”, etc.) on their own get at most `low`. If a parent is given and it narrows the name to one unit (e.g. parent “长治市” + “城区”), a code is given as usual.
147
+
148
+ ### Lookup order
149
+
150
+ 1. Normalize: strip whitespace and leading/trailing punctuation, full-width to half-width. A parent such as “市辖区” is treated as not given.
151
+ 2. Choose the table year. Without a year, the latest year is used.
152
+ 3. In that year's table, look for the full name, then short forms (generic suffix removed; short forms of ethnic autonomous areas and banners; ethnic district names without the ethnic group). Filter by parent when given. A short form of one character is not used.
153
+ 4. If not in that year's table: search all years, then follow the official change table to that year. For splits, follow the main successor (`main = 是` in the change table). When the notice names no main successor, return all destinations and no code.
154
+ 5. If still not found: check whether parent and name are written together (天水市麦积区), split, and search again.
155
+ 6. If a parent was given and nothing is found: search again without the parent.
156
+ 7. Typo rules (`geoalign/fuzzy.py`): homophones and similar-looking characters. `low` only.
157
+ 8. Embedding model (optional, `geoalign/embed.py`). `low` only.
158
+
159
+ ### Public benchmark
160
+
161
+ The benchmark is in `benchmark/`: `make_questions.py` generates the questions and `run.py` scores them. Questions use only the official code tables and notice sentences in the dataset, built by the rules written at the top of `make_questions.py`. Answers follow the year-end convention.
162
+
163
+ Results (`benchmark/results.md`; lookup and typo rules only, no embedding model). We re-ran `run.py` against the installed wheel and got the same table.
164
+
165
+ | Question type | Questions | Share given a code | Accuracy when a code is given |
166
+ |---|---|---|---|
167
+ | Former name from a notice, with year | 224 | 100.0% | 100.0% |
168
+ | Abolished name from a notice, no year (answer: 2025 code) | 198 | 100.0% | 100.0% |
169
+ | “Parent + name” written together in a notice | 210 | 100.0% | 100.0% |
170
+ | Full name, with year | 299 | 100.0% | 100.0% |
171
+ | Full name, no year | 100 | 100.0% | 100.0% |
172
+ | Short form (trailing 市, 县, 区, 旗, 地区, 矿区 removed) | 323 | 100.0% | 100.0% |
173
+ | Short form of ethnic autonomous areas | 112 | 100.0% | 100.0% |
174
+ | Parent full name + full name | 210 | 100.0% | 100.0% |
175
+ | Duplicate name, parent given | 84 | 100.0% | 100.0% |
176
+ | All questions with an answer | 1,760 | 100.0% | 100.0% |
177
+
178
+ On the 180 questions whose correct answer is “give no code” (duplicate name without parent, short form matching several units, year before 2012, generic name, unit created in 2026), the library gave a code 0 times.
179
+
180
+ **This benchmark is easy and does not show performance on real data.** The rules that generate the questions (remove trailing 市/县/区/旗, remove the ethnic group name, parent + full name) are essentially the same as the library's short-form and split rules, so near-100% in these categories is expected. The benchmark mainly checks that the rules are implemented correctly and that the library does not guess on duplicate names. All spellings are official or derived by simple rules: there are no typos and no messy spellings. Real data has typos, truncated names and township names mixed in. On such data the share given a code will be much lower, and given codes can be wrong. The typo rules have no public benchmark.
181
+
182
+ ### Data files
183
+
184
+ The files in `geoalign/data/` are produced by two scripts:
185
+
186
+ 1. `python -m geoalign.build_data <dataset data directory>` packs the code tables and the change table. It checks the scope: code tables from 2012, change table from 2013.
187
+ 2. `python tools/build_confusion.py <years.csv> <NotoSansCJK-Regular.ttc>` builds the homophone and similar-character tables for the typo rules. Only this step needs Pillow, numpy, pypinyin and the Noto Sans CJK font.
188
+
189
+ ### Tests
190
+
191
+ ```
192
+ python -m unittest discover -s tests
193
+ ```
194
+
195
+ ### License
196
+
197
+ Code: Apache-2.0, see `LICENSE`. The pinyin in `geoalign/data/confusion.json` was generated with pypinyin (data from pinyin-data, both MIT). The similar-looking character pairs were computed by rendering characters with the Noto Sans CJK font (SIL OFL 1.1); the font is not included. Copyright notices and licenses are in `NOTICE`. The data in `geoalign/data/` comes from the dataset; see the dataset card for its license (CC BY 4.0 for the compilation and annotations).
198
+
199
+ ---
200
+
201
+ ## 中文
202
+
203
+ ### 做什么
204
+
205
+ 1. 把地名写法变成行政区划代码。可以给年份和上级地名。
206
+ 2. 把一年的代码换成另一年的代码。拆分、合并时给出全部去向。
207
+ 3. 查一个地方的沿革。
208
+
209
+ 没有把握时,工具库不给代码,给全部候选。
210
+
211
+ ### 安装
212
+
213
+ 从 PyPI 安装:
214
+
215
+ ```
216
+ pip install geoalign
217
+ ```
218
+
219
+ 也可以从数据集仓库里的 wheel 安装:
220
+
221
+ ```
222
+ pip install https://huggingface.co/datasets/WorldInsightLab/china-admin-divisions/resolve/main/geoalign/dist/geoalign-0.1.0-py3-none-any.whl
223
+ ```
224
+
225
+ 从源码安装(在 `pyproject.toml` 所在的 `geoalign/` 目录下运行):
226
+
227
+ ```
228
+ pip install .
229
+ ```
230
+
231
+ - 查表和错别字规则只用 Python 标准库(Python 3.8 以上),不用显卡。
232
+ - 向量模型后备是可选的。要装 `sentence-transformers` 和 `torch`(从源码装时可用 `pip install ".[embed]"`),还要自己准备一个中文向量模型。公开测试题不测向量模型后备。
233
+
234
+ ### 数据范围
235
+
236
+ 1. 2012 至 2025 年,每年年底的县级以上代码表,全部来自民政部官方页。数据和出处见数据集卡片。
237
+ 2. 给的年份早于 2012 年时,工具库不给代码,`note` 写明“数据只从 2012 年开始”。
238
+ 3. 给的年份晚于 2025 年时,用 2025 年的表。
239
+ 4. **只管县级以上。不要把乡镇名交给工具库。** 去掉“镇、乡、街道”等通名以后,27,004 个不同的乡镇写法里有 1,358 个(5.0%)会被有把握地认成县级以上单位(例:“三台”→三台县)。按乡镇个数算(同名乡镇按出现次数算),38,742 个乡镇里约 12%(4,497 个)会被这样认错。认出的单位常常不是这个乡镇所在的县。
240
+
241
+ ### 用法
242
+
243
+ 下面的输出由上面的 wheel 在干净的虚拟环境里实际运行得到。
244
+
245
+ ```python
246
+ from geoalign import Gazetteer
247
+ g = Gazetteer()
248
+
249
+ m = g.align("密云县") # 不给年份时,对到最新的代码
250
+ (m.code, m.name, m.confidence, m.method, m.year)
251
+ # ('110118', '密云区', 'medium', 'history', 2025)
252
+
253
+ g.align("密云县", year=2013).code # '110228':2013 年底还是密云县
254
+ g.align("朝阳区", parent="长春市").code # '220104'
255
+
256
+ m = g.align("朝阳区") # 重名,没有上级
257
+ (m.code, m.best, m.confidence, m.method)
258
+ # (None, '110105', 'low', 'exact') # 没有把握,不给代码
259
+ [(c.code, c.name, c.parent_name) for c in m.candidates]
260
+ # [('110105', '朝阳区', '北京市'), ('220104', '朝阳区', '吉林省长春市')]
261
+
262
+ m = g.align("天水市麦积区") # 上级和本级连写
263
+ (m.code, m.name, m.confidence, m.method)
264
+ # ('620503', '麦积区', 'medium', 'split')
265
+
266
+ m = g.align("密云县", year=1990) # 早于 2012 年
267
+ (m.code, m.confidence)
268
+ # (None, 'low')
269
+
270
+ g.to_year("110228", 2025)
271
+ # YearResult(codes=['110118'], names=['密云区'], exact=True,
272
+ # steps=['2014→2015:110228 密云县 → 110118 密云区(撤县设区)'])
273
+
274
+ g.to_year("130103", 2016) # 石家庄桥东区 2014 年撤销,分给两个区
275
+ # YearResult(codes=['130102', '130104'], names=['长安区', '桥西区'], exact=False,
276
+ # steps=['2014→2015:130103 桥东区 → 130102 长安区(拆分)',
277
+ # '2014→2015:130103 桥东区 → 130104 桥西区(拆分)'])
278
+
279
+ g.history("330103") # 杭州市下城区:2021 年并入拱墅区
280
+ # {'code': '330103', 'names': [{'name': '下城区', 'from': 2012, 'to': 2020}],
281
+ # 'events': [{'year': 2021, 'type': '合并', 'relation': '多对一', 'partial': False,
282
+ # 'old_code': '330103', 'old_name': '下城区', 'new_code': '330105', 'new_name': '拱墅区',
283
+ # 'basis': '官方变更情况', 'source_url': 'http://xzqh.mca.gov.cn/description?dcpid=2021'}]}
284
+ ```
285
+
286
+ `align` 返回的字段:
287
+
288
+ | 字段 | 意思 |
289
+ |---|---|
290
+ | `code` | 有把握时的代码。没有把握时是 `None` |
291
+ | `best` | 排第一的候选。没有把握时也给,供必须取一个的调用方用 |
292
+ | `name` | `code` 或 `best` 的标准名称 |
293
+ | `confidence` | `high`:全称唯一。`medium`:简称、旧名、连写拆开以后唯一。`low`:错别字规则、向量模型、去掉上级才找到、有多个候选、泛称、早于 2012 年。`none`:找不到 |
294
+ | `method` | `exact`、`alias`、`history`、`split`、`no_parent`、`fuzzy`、`embedding`、`none` |
295
+ | `candidates` | 全部候选。重名时是全部重名单位 |
296
+ | `year` | 用的哪一年的表 |
297
+ | `source_grade` | 这一年的表的来源等级。本版只有 `A`(官方原页) |
298
+ | `rebuilt` | 这一年的表是不是重建的。本版都是 `False` |
299
+ | `note` | 补充说明 |
300
+
301
+ `confidence` 是 `low` 或 `none` 时,`code` 是 `None`。
302
+
303
+ 泛称(“市区”“新区”“城区”“郊区”“矿区”“东区”“西区”等)单独出现时最多给 `low`。给了上级、上级把它确定成一个单位时(例:上级“长治市”+“城区”),照常给代码。
304
+
305
+ ### 查的顺序
306
+
307
+ 1. 规范写法:去空白、首尾标点,全角换半角。“市辖区”这类上级当作没给。
308
+ 2. 定用哪一年的表。不给年份时用最新一年。
309
+ 3. 在这一年的表里找全称,再找简称(去掉通名、民族自治地方的简称、旗的简称、民族区去掉民族名)。给了上级时用上级筛。去掉通名后只剩 1 个字的不作简称。
310
+ 4. 这一年的表里没有:到所有年份找,再沿官方变更表换成这一年的代码。拆分时沿主要承继走(变更表 `main = 是`)。公告没有说主要承继时,给全部去向,不给代码。
311
+ 5. 还没有:看是不是上级和本级连写(天水市麦积区),拆开再找。
312
+ 6. 给了上级还找不到:去掉上级再找。
313
+ 7. 错别字规则(`geoalign/fuzzy.py`):同音字、形近字。只给 `low`。
314
+ 8. 向量模型(可选,`geoalign/embed.py`)。只给 `low`。
315
+
316
+ ### 公开测试题
317
+
318
+ 公开测试题在 `benchmark/`:`make_questions.py` 生成题目,`run.py` 测。题目只用数据集里的官方代码表和公告原句,按写明的规则生成,答案按年底口径。生成规则写在 `make_questions.py` 开头。
319
+
320
+ 结果(`benchmark/results.md`,只查表和错别字规则,不用向量模型)。我们用安装好的 wheel 重跑 `run.py`,结果和这张表相同。
321
+
322
+ | 题的种类 | 题数 | 给代码的比例 | 给代码时的正确率 |
323
+ |---|---|---|---|
324
+ | 公告里的旧名称,给年份 | 224 | 100.0% | 100.0% |
325
+ | 公告里撤销的旧名称,不给年份(答案是 2025 年的代码) | 198 | 100.0% | 100.0% |
326
+ | 公告里的“上级 + 名称”连写 | 210 | 100.0% | 100.0% |
327
+ | 全称,给年份 | 299 | 100.0% | 100.0% |
328
+ | 全称,不给年份 | 100 | 100.0% | 100.0% |
329
+ | 简称(去掉末尾的市、县、区、旗、地区、矿区) | 323 | 100.0% | 100.0% |
330
+ | 民族自治地方的简称 | 112 | 100.0% | 100.0% |
331
+ | 上级全称 + 全称 | 210 | 100.0% | 100.0% |
332
+ | 重名,给了上级 | 84 | 100.0% | 100.0% |
333
+ | 有答案的题合计 | 1,760 | 100.0% | 100.0% |
334
+
335
+ 答案是“不应给代码”的 180 道题(重名没给上级、简称对得上几个单位、早于 2012 年、泛称、2026 年才设立的单位),错给代码 0 道。
336
+
337
+ **这套题容易,不代表真实数据上的成绩。** 出题的变形规则(去掉末尾的市、县、区、旗,去掉民族名,上级 + 全称)和工具库的简称、连写规则基本相同,所以这些类别接近 100% 是预期的。这套题主要检验规则的实现没有错、重名时不乱给代码。题里的写法都是官方写法或按简单规则变出来的,没有错别字,没有乱写法。真实数据里有错别字、残缺的名称、乡镇名混入。在这类数据上,给代码的比例会低得多,给代码时也可能出错。错别字规则没有公开测试题。
338
+
339
+ ### 数据文件
340
+
341
+ `geoalign/data/` 里的文件由下面两个脚本生成:
342
+
343
+ 1. `python -m geoalign.build_data <数据集的 data 目录>`:打包代码表和变更表。脚本检查范围:代码表只收 2012 年及以后,变更表只收 2013 年及以后。
344
+ 2. `python tools/build_confusion.py <years.csv> <NotoSansCJK-Regular.ttc>`:生成错别字规则用的同音字、形近字表。只在生成时要 Pillow、numpy、pypinyin 和 Noto Sans CJK 字体。
345
+
346
+ ### 测试
347
+
348
+ ```
349
+ python -m unittest discover -s tests
350
+ ```
351
+
352
+ ### 许可
353
+
354
+ 代码按 Apache-2.0 许可发布,见 `LICENSE`。错别字字表 `geoalign/data/confusion.json` 里的拼音由 pypinyin 生成(数据来自 pinyin-data,都是 MIT 许可),形近字用 Noto Sans CJK 字体(SIL OFL 1.1)画图算出,包里不带字体。版权声明和许可见 `NOTICE`。`geoalign/data/` 里的数据来自数据集,许可见数据集卡片(编排和标注按 CC BY 4.0)。