-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaddress_module.py
59 lines (50 loc) · 1.99 KB
/
address_module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from flask import Blueprint, render_template, request
import spacy
import re
# Load spaCy's English language model
nlp = spacy.load('en_core_web_sm')
# Define Blueprint for the submodule
address_bp = Blueprint('address_bp', __name__)
# Function to extract address components using spaCy
def extract_address_components(address):
doc = nlp(address)
components = {
'Door No.': '',
'Office or House': '',
'Street or Landmark': '',
'City': '',
'District': '',
'Pincode': ''
}
for ent in doc.ents:
if ent.label_ == "CARDINAL":
components['Door No.'] = ent.text
elif ent.label_ == "GPE":
if not components['City']:
components['City'] = ent.text
elif not components['District']:
components['District'] = ent.text
elif ent.label_ == "LOC":
if not components['Street or Landmark']:
components['Street or Landmark'] = ent.text
elif ent.label_ == "ORG":
components['Office or House'] = ent.text
elif ent.label_ == "FAC":
if not components['Office or House']:
components['Office or House'] = ent.text
pincode_match = re.search(r'\b\d{6}\b', address)
if pincode_match:
components['Pincode'] = pincode_match.group(0)
return components
# Route for handling map retrieval (on clicking "Find")
@address_bp.route('/get_map', methods=['POST'])
def get_map():
address = request.form['address']
map_url = f"https://maps.google.com/?q={address.replace(' ', '+')}&output=embed" # Example placeholder
return render_template('index.html', map=map_url)
# Route for handling address arrangement using NLP (on clicking "Arrange")
@address_bp.route('/arrange_address', methods=['POST'])
def arrange_address():
address = request.form['address']
arranged_address = extract_address_components(address)
return render_template('index.html', arranged_address=arranged_address)