public LocationIdMap <ILocation> ParseLocationsWithIds() { var stops = JsonConvert.DeserializeObject <List <TrafiApiStopJSON> >(_locationsJSON); var locations = new LocationIdMap <ILocation>(); foreach (var stop in stops) { locations.Add(stop.Id, new Location(stop.Id, stop.Name, stop.Coordinate.Lng, stop.Coordinate.Lat)); } return(locations); }
// Should probably find a nice library that will do a better job than this quick and messy thing. public string ReadStopID(LocationIdMap <ILocation> availableStops) { var stopName = ReadStop().ToLower(); while (true) { var similarStops = new LocationIdMap <ILocation>(); var containedStops = new LocationIdMap <ILocation>(); foreach (var availableStop in availableStops) { var distance = Fastenshtein.Levenshtein.Distance(stopName, availableStop.Value.Alias.ToLower()); //Levenshtein distance is zero, thus the strings are equal if (distance == 0) { return(availableStop.Key); } if (availableStop.Value.Alias.ToLower().Contains(stopName) || stopName.Contains(availableStop.Value.Alias.ToLower())) { containedStops.Add(availableStop.Key, availableStop.Value); } else if (distance <= 5) { similarStops.Add(availableStop.Key, availableStop.Value); } } var potentialStopIDs = new List <string>(containedStops.Keys.ToList()); potentialStopIDs.AddRange(similarStops .OrderBy(x => Fastenshtein.Levenshtein.Distance(stopName, x.Value.Alias)) .Select(x => x.Key) .ToList()); System.Console.WriteLine("Couldn't find the stop you entered."); System.Console.WriteLine("Here are some similar sounding stops."); for (int i = 0; i < potentialStopIDs.Count && i < STOP_CHOICE_AMOUNT_LIMIT; i++) { System.Console.WriteLine(i + ". " + potentialStopIDs[i] + " " + availableStops[potentialStopIDs[i]].Alias); } System.Console.Write("Enter a number from the list above, or try again with a new stop name: "); stopName = System.Console.ReadLine(); int stopNumber; if (Int32.TryParse(stopName, out stopNumber)) { return(potentialStopIDs[stopNumber]); } } }