forked from agrc/agrc.python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_address.py
executable file
·149 lines (127 loc) · 4.2 KB
/
parse_address.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""
parse_address
Jan 2013
stdavis@utah.gov
"""
import os
from csv import reader
# reference data
dirs = {
'N': ['N', 'NORTH', 'NO'],
'S': ['S', 'SOUTH', 'SO'],
'E': ['E', 'EAST', 'EA'],
'W': ['W', 'WEST', 'WE']
}
# searching states
searchStates = {
'houseNumber': 1,
'prefixDirection': 2,
'streetName': 3,
'suffixDirOrType': 4,
'end': 5
}
class NormalizedAddress:
houseNumber = None
prefixDirection = None
streetName = None
suffixType = None
suffixDirection = None
normalizedAddressString = None
originalAddressString = None
def __init__(self, original):
self.originalAddressString = original.strip()
def getPreviousWord(self, word):
words = self.originalAddressString.upper().split(' ')
return words[words.index(word.upper()) - 1]
def __getSuffixTypes():
global reader
types = {}
with open(os.path.join(os.path.dirname(__file__), 'data', 'USPS_Street_Suffixes.csv'), 'rb') as file:
reader = reader(file)
firstrow = True
for row in reader:
if firstrow:
firstrow = False
continue
try:
types[row[3].strip()].append(row[2].strip())
except KeyError:
types[row[3].strip()] = [row[2].strip()]
return types
def checkWord(word, d):
for key, value in d.iteritems():
if word in value:
return key
# if nothing is found
return False
def parseWord(word, state, add):
def appendStreetWord(appendWord):
if add.streetName is None:
add.streetName = appendWord
else:
add.streetName += ' {0}'.format(appendWord)
word = word.replace('.', '')
if word.strip() == '':
return state
if state == searchStates['houseNumber']:
add.houseNumber = word
return searchStates['prefixDirection']
elif state == searchStates['prefixDirection']:
pDir = checkWord(word, dirs)
if pDir is False:
appendStreetWord(word)
return searchStates['suffixDirOrType']
else:
add.prefixDirection = pDir
return searchStates['streetName']
elif state == searchStates['streetName']:
sType = checkWord(word, sTypes)
if not sType is False:
appendStreetWord(add.getPreviousWord(word))
add.prefixDirection = None
return searchStates['end']
appendStreetWord(word)
return searchStates['suffixDirOrType']
elif state == searchStates['suffixDirOrType']:
sType = checkWord(word, sTypes)
sDir = checkWord(word, dirs)
if sType is False and sDir is False:
appendStreetWord(word)
return searchStates['suffixDirOrType']
elif not sType is False:
add.suffixType = sType
return searchStates['end']
else: # sDir
add.suffixDirection = sDir
return searchStates['end']
elif state == searchStates['end']:
sType = checkWord(word, sTypes)
if not sType is False:
appendStreetWord(add.getPreviousWord(word))
add.suffixType = sType
sDir = checkWord(word, dirs)
if not sDir is False:
appendStreetWord(add.getPreviousWord(word))
add.suffixDirection = sDir
return searchStates['end']
def parse(address):
nAdd = NormalizedAddress(address)
state = searchStates['houseNumber']
for word in address.strip().split(' '):
state = parseWord(word.upper(), state, nAdd)
# Build normalized address string
if nAdd.suffixType is not None:
suffixDirOrType = nAdd.suffixType
elif nAdd.suffixDirection is not None:
suffixDirOrType = nAdd.suffixDirection
else:
suffixDirOrType = ''
nAdd.normalizedAddressString = nAdd.houseNumber
if nAdd.prefixDirection is None:
nAdd.normalizedAddressString += " {0} {1}".format(nAdd.streetName, suffixDirOrType)
else:
nAdd.normalizedAddressString += " {0} {1} {2}".format(
nAdd.prefixDirection, nAdd.streetName, suffixDirOrType)
nAdd.normalizedAddressString = nAdd.normalizedAddressString.strip()
return nAdd
sTypes = __getSuffixTypes()