-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.py
More file actions
65 lines (57 loc) · 1.84 KB
/
Parser.py
File metadata and controls
65 lines (57 loc) · 1.84 KB
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
from Address import Address
import utils
import re
import sys
def getLinesOfFile(path):
try:
file = open(path, 'r')
Lines = file.readlines()
return Lines
except FileNotFoundError:
utils.quitWithError("Invalid argument")
def trimWhitesSpaces(string):
i = 0
res1 = ""
res2 = ""
lgth = string.__len__()
while i < lgth and string[i] == ' ':
i += 1
while i < lgth and string[i]:
res1 += string[i]
i += 1
tmp = reversed(res1).__str__()
i = 0
while i < lgth and tmp[i] == ' ':
i += 1
while i < lgth and tmp[i]:
res2 += string[i]
i += 1
return res2
def processLine(line):
if line == "ABORT\n" or line == "ABORT":
sys.exit(0)
match = re.findall(r"(?i)^(\s?)+((?![×Þß÷þø])[ \-\'a-zÀ-ÿ]+,)(\s?)+([\d]+)(\s?)+(impasse|quai|rue|square|allée|place|boulevard|rue|chemin|avenue)(\s?)+((?![×Þß÷þø])[ \-\'a-zÀ-ÿ]+)$", line)
address = Address()
if (match and len(match[0]) == 8):
match = match[0]
address.city = trimWhitesSpaces(match[1].replace(",", ""))
address.number = match[3]
address.streetType = match[5]
address.streetName = match[7]
address.value = address.city + ", " + address.number + " " + address.streetType + " " + address.streetName
address.city = address.city.replace("'", "").replace("-", "").replace(",", "").lower()
address.streetName = address.streetName.replace("'", "").replace("-", "").lower()
else:
address.isKnown = False
address.value = line
if not address.isKnown:
utils.eprintbis(address.value)
return address
def parse(path):
res = []
lines = getLinesOfFile(path)
for line in lines:
tmp = processLine(line)
if tmp.isKnown:
res.append(tmp)
return res