You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.3 KiB

7 years ago
7 years ago
7 years ago
  1. from editdistance import eval as editdistance
  2. import re
  3. import json
  4. class Search:
  5. def __init__(self,query):
  6. self.raw_lower = query.lower()
  7. def __call__(self,arg):
  8. arg = arg.lower()
  9. return editdistance(self.raw_lower,arg)
  10. def __str__(self):
  11. print(self.raw_lower)
  12. def __repr__(self):
  13. return str(self)
  14. class StopSearch(Search):
  15. def __init__(self,query):
  16. super().__init__(query)
  17. query = query.lower()
  18. parts = re.split(r' ?(?:(?<!\w)and(?!\w)|&) ?',query)
  19. self.query = ' & '.join(parts)
  20. self.query_reversed = ' & '.join(reversed(parts))
  21. def __call__(self,stop):
  22. stop = stop.lower()
  23. paren = re.search(r'\((?P<data>[^\)]+)\)',stop)
  24. ret= [
  25. editdistance(self.query,stop),
  26. editdistance(self.query_reversed,stop),
  27. ]
  28. if paren:
  29. paren = paren.group('data')
  30. ret.append(editdistance(self.query,paren))
  31. return min(
  32. ret
  33. )
  34. def __str__(self):
  35. return '{}|{}'.format(self.query,self.query_reversed)
  36. if __name__ == "__main__":
  37. with open('stops_out.json') as file:
  38. data = json.load(file)
  39. names = [stop['stpnm'] for stop in data['stops']]
  40. while True:
  41. q = StopSearch(input('Search: '))
  42. print('\n'.join(sorted(names,key=q)))