Script to grab stop codes from allstops.xml and sort
[ottawa-travel-planner.git] / LandmarkMatcher.py
blob51ea0aa62ff6ec76c69d9a4b22b1ea12db09740b
2 # vi: set softtabstop=4 shiftwidth=4 tabstop=8 expandtab:
4 """Matches partial landmark names to full landmark names using a
5 prioritized list.
7 It turns out that you can send a partial name to the travel planner
8 web app and it'll figure out what you mean, but there's no
9 prioritization of partial matches: BAYSHORE maps to
10 BAYSHORE CATHOLIC SCHOOL before it maps to BAYSHORE SHOPPING CENTRE.
12 We do our own prioritization by using landmarkLeech.py to grab
13 an offline copy of the landmark list in landmarks.txt, and
14 listing the categories (transit station, shopping centre, etc.) in
15 priority order in landmarkCategoryPriority.txt."""
17 from PlannerExceptions import *
19 class LandmarkMatcher:
20 def __init__(self, listfile="landmarks.txt",
21 priofile="landmarkCategoryPriority.txt"):
23 self.all = set()
25 # List of categories; each one contains a list of landmarks
26 self.categories = []
28 # Map of category to landmark list.
29 self.categoryMap = {}
31 self._loadLandmarks(listfile)
32 self._loadCategoryPrio(priofile)
34 def _loadLandmarks(self, listfile):
35 listfp = open(listfile, "r")
36 for line in listfp:
37 parts = line.rstrip().split("|")
38 landmark = parts[1].upper()
39 self.all.add(landmark)
41 if not parts[0] in self.categoryMap:
42 self.categoryMap[parts[0]] = []
43 self.categoryMap[parts[0]].append(landmark)
45 def _loadCategoryPrio(self, priofile):
46 priofp = open(priofile, "r")
47 for line in priofp:
48 self.categories.append(self.categoryMap[line.rstrip()])
50 def match(self, str):
51 str = str.upper()
52 if str in self.all:
53 return str
55 for catlist in self.categories:
56 for landmark in catlist:
57 if landmark.startswith(str):
58 return landmark
60 raise InvalidLandmarkException("Couldn't find landmark '%s' "
61 "in local database." % str)