house_service3.0.py
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
import json
|
||||
import os
|
||||
from house.house_operation import read_houses
|
||||
|
||||
# 地址数据文件
|
||||
ADDRESS_FILE = "./data/address_info.json"
|
||||
# 邻接矩阵数据文件
|
||||
GRAPH_FILE = "./data/adj_matrix.json"
|
||||
|
||||
|
||||
def init_address_db():
|
||||
"""初始化地址解析数据文件"""
|
||||
os.makedirs("./data", exist_ok=True)
|
||||
if not os.exists(ADDRESS_FILE):
|
||||
with open(ADDRESS_FILE, 'w', encoding='utf-8') as f:
|
||||
json.dump({}, f, ensure_ascii=False, indent=4)
|
||||
|
||||
|
||||
def init_graph_db():
|
||||
"""初始化小区邻接矩阵文件"""
|
||||
os.makedirs("./data", exist_ok=True)
|
||||
if not os.path.exists(GRAPH_FILE):
|
||||
init_data = {
|
||||
"communities": [],
|
||||
"adj_matrix": []
|
||||
}
|
||||
with open(GRAPH_FILE, 'w', encoding='utf-8') as f:
|
||||
json.dump(init_data, f, ensure_ascii=False, indent=4)
|
||||
|
||||
|
||||
# ==================== 串操作:地址解析 ====================
|
||||
def parse_address(address_str):
|
||||
"""
|
||||
串操作:拆分地址为 省/市/区/小区
|
||||
时间复杂度:O(L) L为字符串长度
|
||||
"""
|
||||
address_str = address_str.strip()
|
||||
# 按常见分隔符拆分
|
||||
split_chars = ["省", "市", "区", "县", "街道", "路", "号", "小区"]
|
||||
parts = []
|
||||
temp = address_str
|
||||
for char in split_chars:
|
||||
if char in temp:
|
||||
idx = temp.find(char)
|
||||
parts.append(temp[:idx + 1])
|
||||
temp = temp[idx + 1:]
|
||||
if temp:
|
||||
parts.append(temp)
|
||||
|
||||
# 标准化结构
|
||||
result = {
|
||||
"province": parts[0] if len(parts) >= 1 else "",
|
||||
"city": parts[1] if len(parts) >= 2 else "",
|
||||
"district": parts[2] if len(parts) >= 3 else "",
|
||||
"community": parts[3] if len(parts) >= 4 else parts[-1] if parts else ""
|
||||
}
|
||||
return result
|
||||
|
||||
|
||||
def save_address_info(house_id, address_info):
|
||||
"""保存解析后的地址信息"""
|
||||
init_address_db()
|
||||
with open(ADDRESS_FILE, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
data[str(house_id)] = address_info
|
||||
with open(ADDRESS_FILE, 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=4)
|
||||
|
||||
|
||||
def get_address_info(house_id):
|
||||
"""获取房源解析后的地址"""
|
||||
init_address_db()
|
||||
with open(ADDRESS_FILE, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
return data.get(str(house_id), {})
|
||||
|
||||
|
||||
# ==================== 数组操作:邻接矩阵 ====================
|
||||
def add_community(community_name):
|
||||
"""添加小区到图节点(数组维护)"""
|
||||
init_graph_db()
|
||||
with open(GRAPH_FILE, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
|
||||
if community_name in data["communities"]:
|
||||
return False
|
||||
|
||||
data["communities"].append(community_name)
|
||||
n = len(data["communities"])
|
||||
# 初始化新行新列(无穷大用 9999 表示)
|
||||
for row in data["adj_matrix"]:
|
||||
row.append(9999)
|
||||
data["adj_matrix"].append([9999] * n)
|
||||
# 自己到自己距离为0
|
||||
data["adj_matrix"][-1][-1] = 0
|
||||
|
||||
with open(GRAPH_FILE, 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=4)
|
||||
return True
|
||||
|
||||
|
||||
def set_distance(community1, community2, distance):
|
||||
"""设置两个小区之间的距离(邻接矩阵赋值)"""
|
||||
init_graph_db()
|
||||
with open(GRAPH_FILE, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
|
||||
if community1 not in data["communities"] or community2 not in data["communities"]:
|
||||
return False
|
||||
|
||||
i = data["communities"].index(community1)
|
||||
j = data["communities"].index(community2)
|
||||
data["adj_matrix"][i][j] = distance
|
||||
data["adj_matrix"][j][i] = distance
|
||||
|
||||
with open(GRAPH_FILE, 'w', encoding='utf-8') as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=4)
|
||||
return True
|
||||
|
||||
|
||||
def get_adj_matrix():
|
||||
"""获取邻接矩阵与小区列表"""
|
||||
init_graph_db()
|
||||
with open(GRAPH_FILE, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
Reference in New Issue
Block a user