前言:
如何從文章內提取地址?方法有很多種,我這邊提一個我的思路。
1、整理一份標準地址庫,至少包括以下幾列:省、市、鎮、街道、路。
有人會說整理很難,沒錯,很難找到成熟的地址庫(這種都是能賣Q的),通過政府官網可以獲得的數據也僅僅是省、市、鎮、街道/居委之類的,如果要整理成一份詳盡的,難度很高。
以上海爲例,我主要是通過百度文庫獲得了一份相對來說比較全的地址庫,但也僅僅是路和區的對應表,無法整理到鎮或街道。所以我又在上海統計局官網中找到了居委會和區的對應表,經過多次整理獲得一份相對比較準確的清單。
2、獲得地址庫後,就僅需要從最大級別開始依次遍歷獲取可能的地址字符串,
比如以下文本
可以很清楚看到就是辦理地點後面的內容,是一個標準化的格式。
我們也可以先找南安市,找到後再找後面是否有“號”之類的字,根據我手上的數據統計,90%以上的地址最後是以“號”、“室”之類結尾的,加上正則表達式即可完成提取。
3、提取後的內容可能會有雜音,比如上例中的“著名的”,當然一般來說都是比較乾淨的。
代碼:
代碼比較爛,沒有特別整理,僅供參考
def possible_address(possible_position, html_text):
possible_text = re.split(r'[,,\s]', html_text[possible_position:], 1)[0]
re_value = re.compile('.*[0-9a-zA-Z/之號室棟樓幢座號巷對面米側旁出口路東西南北上鋪站]')
for keyword in ['室', '棟', '樓','幢', '座', '號', '巷', '對面', '米', '側', '旁','出口', '路東', '路西', '路南', '路北',
'樓上', '鋪','路','大道']:
keyword_position = possible_text.find(keyword)
if keyword in possible_text and (4 <=keyword_position<=30):
right_text = re.search(re_value, possible_text).group(0)
return right_text
def get_right_address(html_text):
'''通過省和城市來提取正確的地址'''
temp_address = ''
#先看省
province_count, city_count = 0, 0
for province in new_province_list:
province_position = html_text.find(province)
if province_position != -1:
right_address = possible_address(province_position, html_text)
if right_address:
return right_address
else:
province_count += 1
if province_count == 1:
temp_address = province
else:
temp_address = ''
else:
#再看市
for city in new_city_list:
city_position = html_text.find(city)
if city_position != -1:
right_address = possible_address(city_position, html_text)
if temp_address != '':
if right_address:
if temp_address not in right_address:
right_address = temp_address + right_address
return right_address
else:
temp_address = temp_address + city
else:
if right_address:
return right_address
else:
temp_address = temp_address + city
else:
#再看區
for district in district_list:
district_position = html_text.find(district)
if district_position != -1:
right_address = possible_address(district_position, html_text)
if temp_address != '':
if right_address:
if temp_address not in right_address:
right_address = temp_address + right_address
return right_address
else:
temp_address = temp_address + district
else:
if right_address:
return right_address
else:
temp_address = temp_address + district
else:
#再看鎮
for township in township_list:
township_position = html_text.find(township)
if township_position != -1:
# print(township)
right_address = possible_address(township_position, html_text)
if temp_address != '':
# print(temp_address)
if right_address:
if temp_address not in right_address:
right_address = temp_address + right_address
return right_address
else:
temp_address = temp_address + township
# print(temp_address)
else:
if right_address:
return right_address
else:
temp_address = temp_address + township
else:
#再看街
for street in street_list:
street_position = html_text.find(street)
if street_position != -1:
right_address = possible_address(street_position, html_text)
if temp_address != '':
if right_address:
if temp_address not in right_address:
right_address = temp_address + right_address
return right_address
else:
temp_address = temp_address + street
else:
if right_address:
return right_address
else:
temp_address = temp_address + street
else:
return temp_address