Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
LAE
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
文靖昊
LAE
Commits
38752315
Commit
38752315
authored
Jan 07, 2025
by
tinywell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
areacode 补充全国所有行政区划代码;优化 find_code 方法,适配自治区、特别行政区等名称
parent
0c8dc23f
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
210 additions
and
2 deletions
+210
-2
area_code.csv
src/agent/area_code.csv
+0
-0
code.py
src/agent/code.py
+33
-2
tool_warn.py
src/agent/tool_warn.py
+5
-0
test_code.py
test/test_code.py
+172
-0
No files found.
src/agent/area_code.csv
View file @
38752315
This diff is collapsed.
Click to expand it.
src/agent/code.py
View file @
38752315
...
...
@@ -36,10 +36,39 @@ class AreaCodeTool:
# 构建三级映射
self
.
third_map
=
{}
def
get_separator
(
name
):
if
'省'
in
name
:
return
'省'
elif
'自治区'
in
name
:
return
'自治区'
elif
'特别行政区'
in
name
:
return
'特别行政区'
elif
'市'
in
name
:
return
'市'
elif
'自治州'
in
name
:
return
'自治州'
elif
'盟'
in
name
:
return
'盟'
elif
'地区'
in
name
:
return
'地区'
elif
'县'
in
name
:
return
'县'
elif
'区'
in
name
:
return
'区'
elif
'市辖区'
in
name
:
return
'市辖区'
else
:
return
None
# 或者返回其他默认值
for
_
,
row
in
self
.
df
.
iterrows
():
name
=
row
[
'name'
]
.
strip
()
code
=
row
[
'code'
]
parts
=
name
.
split
(
'省'
if
'省'
in
name
else
'市'
)
separator
=
get_separator
(
name
)
if
separator
:
parts
=
name
.
split
(
separator
)
else
:
parts
=
[
name
]
# 如果没有找到分隔符,就把整个名称作为一个部分
if
'省'
in
name
:
if
len
(
parts
)
>
1
and
parts
[
1
]:
...
...
@@ -73,7 +102,9 @@ class AreaCodeTool:
List[Tuple[str, str]]: 返回匹配的(区域名称, 代码)列表
"""
results
=
[]
if
area_name
==
""
:
return
results
area_name
=
area_name
.
strip
()
# 尝试完整匹配
if
area_name
in
self
.
full_name_map
:
results
.
append
((
area_name
,
self
.
full_name_map
[
area_name
]))
...
...
src/agent/tool_warn.py
View file @
38752315
...
...
@@ -77,6 +77,11 @@ class WarningTool(BaseTool):
orangeper
=
int
(
response
.
resultdata
[
"orangenum"
])
/
num
yellowper
=
int
(
response
.
resultdata
[
"yellownum"
])
/
num
blueper
=
int
(
response
.
resultdata
[
"bluenum"
])
/
num
else
:
redper
=
0
orangeper
=
0
yellowper
=
0
blueper
=
0
# 计算处置率
if
int
(
response
.
resultdata
[
"rednum"
])
!=
0
:
...
...
test/test_code.py
0 → 100644
View file @
38752315
import
pytest
from
src.agent.code
import
AreaCodeTool
@pytest.fixture
def
code_tool
():
"""创建 AreaCodeTool 实例的 fixture"""
return
AreaCodeTool
()
def
test_exact_province_match
(
code_tool
):
"""测试省级完整匹配"""
# 普通省份
result
=
code_tool
.
find_code
(
"贵州省"
)
assert
len
(
result
)
==
1
assert
result
[
0
][
0
]
==
"贵州省"
assert
result
[
0
][
1
]
==
"520000"
# 自治区
result
=
code_tool
.
find_code
(
"内蒙古自治区"
)
assert
len
(
result
)
==
1
assert
result
[
0
][
0
]
==
"内蒙古自治区"
assert
result
[
0
][
1
]
==
"150000"
def
test_exact_city_match
(
code_tool
):
"""测试市级完整匹配"""
# 普通地级市
result
=
code_tool
.
find_code
(
"贵阳市"
)
assert
len
(
result
)
==
1
assert
result
[
0
][
0
]
==
"贵阳市"
assert
result
[
0
][
1
]
==
"520100"
# 自治州
result
=
code_tool
.
find_code
(
"黔东南苗族侗族自治州"
)
assert
len
(
result
)
==
1
assert
"自治州"
in
result
[
0
][
0
]
assert
result
[
0
][
1
]
==
"522600"
def
test_exact_district_match
(
code_tool
):
"""测试区县级完整匹配"""
# 普通区
result
=
code_tool
.
find_code
(
"南明区"
)
assert
len
(
result
)
==
1
assert
"南明区"
in
result
[
0
][
0
]
assert
result
[
0
][
1
]
==
"520102"
# 自治县
result
=
code_tool
.
find_code
(
"道真仡佬族苗族自治县"
)
assert
len
(
result
)
==
1
assert
"自治县"
in
result
[
0
][
0
]
assert
result
[
0
][
1
]
==
"520325"
def
test_special_regions
(
code_tool
):
"""测试特殊行政区划"""
# 直辖市
result
=
code_tool
.
find_code
(
"北京市"
)
assert
len
(
result
)
==
1
assert
result
[
0
][
1
]
==
"110000"
# 少数民族自治区
result
=
code_tool
.
find_code
(
"西藏自治区"
)
assert
len
(
result
)
==
1
assert
result
[
0
][
1
]
==
"540000"
result
=
code_tool
.
find_code
(
"内蒙古自治区"
)
assert
len
(
result
)
==
1
assert
result
[
0
][
1
]
==
"150000"
def
test_partial_match
(
code_tool
):
"""测试部分名称匹配"""
# 省会城市,不带"市"字
result
=
code_tool
.
find_code
(
"贵阳"
)
assert
len
(
result
)
==
1
assert
"贵阳"
in
result
[
0
][
0
]
assert
result
[
0
][
1
]
==
"520100"
# 自治区,不带"自治区"
result
=
code_tool
.
find_code
(
"内蒙古"
)
assert
len
(
result
)
==
1
assert
result
[
0
][
1
]
==
"150000"
# 自治州,不带"自治州"
result
=
code_tool
.
find_code
(
"黔东南"
)
assert
len
(
result
)
==
1
assert
result
[
0
][
1
]
==
"522600"
def
test_no_match
(
code_tool
):
"""测试无匹配结果的情况"""
result
=
code_tool
.
find_code
(
"不存在的地区"
)
assert
len
(
result
)
==
0
result
=
code_tool
.
find_code
(
"123"
)
assert
len
(
result
)
==
0
def
test_combined_names
(
code_tool
):
"""测试组合名称"""
# 省市组合
result
=
code_tool
.
find_code
(
"贵州省贵阳市"
)
assert
len
(
result
)
==
1
assert
result
[
0
][
1
]
==
"520100"
# 省市区组合
result
=
code_tool
.
find_code
(
"贵州省贵阳市南明区"
)
assert
len
(
result
)
==
1
assert
result
[
0
][
1
]
==
"520102"
# 自治区组合
result
=
code_tool
.
find_code
(
"内蒙古自治区呼和浩特市"
)
assert
len
(
result
)
==
1
assert
result
[
0
][
1
]
==
"150100"
# 自治区下辖
result
=
code_tool
.
find_code
(
"日喀则"
)
assert
len
(
result
)
==
1
assert
result
[
0
][
1
]
==
"540200"
# 自治区下辖
result
=
code_tool
.
find_code
(
"阿里地区"
)
assert
len
(
result
)
==
1
assert
result
[
0
][
1
]
==
"542500"
# 特别行政区
result
=
code_tool
.
find_code
(
"澳门"
)
assert
len
(
result
)
==
1
assert
result
[
0
][
1
]
==
"820000"
def
test_edge_cases
(
code_tool
):
"""测试边界情况"""
# 空字符串
result
=
code_tool
.
find_code
(
""
)
assert
len
(
result
)
==
0
# 包含空格
result
=
code_tool
.
find_code
(
" 贵阳市 "
)
assert
len
(
result
)
==
1
assert
"贵阳市"
in
result
[
0
][
0
]
# 特殊字符
result
=
code_tool
.
find_code
(
"贵阳市。"
)
assert
len
(
result
)
==
0
def
test_subsequence_match
(
code_tool
):
"""测试子序列匹配功能"""
# 测试正确的子序列
assert
code_tool
.
is_subsequence
(
"贵州省贵阳市"
,
"贵州贵阳"
)
assert
code_tool
.
is_subsequence
(
"贵州省贵阳市南明区"
,
"贵州南明"
)
assert
code_tool
.
is_subsequence
(
"内蒙古自治区呼和浩特市"
,
"内蒙古呼和浩特"
)
# 测试错误的子序列
assert
not
code_tool
.
is_subsequence
(
"贵州省贵阳市"
,
"贵州南明"
)
assert
not
code_tool
.
is_subsequence
(
"贵阳市"
,
"贵州"
)
assert
not
code_tool
.
is_subsequence
(
"内蒙古自治区"
,
"新疆"
)
def
test_get_full_name
(
code_tool
):
"""测试代码反查功能"""
# 省级代码
result
=
code_tool
.
get_full_name
(
"520000"
)
# 贵州省
assert
result
==
"贵州省"
# 市级代码
result
=
code_tool
.
get_full_name
(
"520100"
)
# 贵阳市
assert
result
==
"贵州省贵阳市"
# 区级代码
result
=
code_tool
.
get_full_name
(
"520102"
)
# 南明区
assert
result
==
"贵州省贵阳市南明区"
# 自治区代码
result
=
code_tool
.
get_full_name
(
"150000"
)
# 内蒙古自治区
assert
result
==
"内蒙古自治区"
# 无效的区域代码
result
=
code_tool
.
get_full_name
(
"999999"
)
assert
result
is
None
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment