/* * Returns an empty CityResult object if a city is found, * if not, potential next letters and cities are returned if a partial * match is found. */ public ICityResult Search(String key) { int level; int index; int length = key.Length; TrieNode pCrawl = root; CityResult result = new CityResult(); for (level = 0; level < length; level++) { if (pCrawl == null) { return(result); } index = GetIndex(key, level); pCrawl = pCrawl.children[index]; } /* * If there is an exact match, the object lists will remain empty and the CityFound flag will be set to true. */ if (ExactMatch(pCrawl)) { result.CityFound = true; } /* * Indicates the prefix does not match any city and there are no next character suggestions * so just return the empty result */ if (pCrawl == null) { return(result); } /* * Have not been able to find a city so return the next * three characters and cities */ bool isLast = IsLastNode(pCrawl); if (!isLast) { result.NextCities = AutocompleteCities(pCrawl, key); } result.NextLetters = AutocompleteCharacters(pCrawl); return(result); }
/// <summary> /// Convinience method to Get, Deserialize and Parse a JSON Object. /// </summary> /// <param name="URL">The URL of the API</param> /// <param name="searchString">The inputted search string so far</param> /// <returns>A List of City objects</returns> public virtual CityResult GetDeserializeParse(string searchString, string url) { try { var deserializedJSON = Deserialize(GetJSON(url)); CityResult cityResult = ParseToCityResult(deserializedJSON, searchString); return(cityResult); } catch (Exception e) { return(null); } }
public ICityResult Search(string searchString) { searchString = searchString.ToUpper(); ICollection <string> cities = new HashSet <string>(); for (int i = 0; i < 1000000; i++) { cities.Add(i.ToString()); } cities.Add("BANDUNG"); cities.Add("BANGUI"); cities.Add("BANGKOK"); cities.Add("BANGALORE"); cities.Add("LA PAZ"); cities.Add("LA PLATA"); cities.Add("LAGOS"); cities.Add("LEEDS"); cities.Add("ZARIA"); cities.Add("ZHUGHAI"); cities.Add("ZIBO"); ICityResult cityResult = new CityResult(); foreach (string c in cities) { if (c.Contains(searchString)) { cityResult.NextCities.Add(c); if (searchString != c) { string letter = c.Substring(searchString.Length, 1); if (!cityResult.NextLetters.Contains(letter)) { cityResult.NextLetters.Add(letter); } } } } return(cityResult); }
public static void Main() { CityResult result = new CityResult(); InitDatabase db = new InitDatabase(); UserInput cityName = new UserInput(); string input; input = cityName.InputName(); result = (CityResult)db.Search(input); //Set up to cycle through both collections within CityResult var namesAndLetters = result.NextCities.Zip(result.NextLetters, (first, second) => first + ", Next Char: " + second); foreach (var item in namesAndLetters) { Console.WriteLine("City: {0}", item); } }
/// <summary> /// Searches the specified search string. /// </summary> /// <param name="searchString">The search string.</param> /// <returns> /// A city result that contains a list of all matching cities and a list of allowable next characters. /// </returns> public ICityResult Search(string searchString) { if (!_validator.IsValid(searchString)) { throw new ArgumentOutOfRangeException(nameof(searchString), InvalidInputErrorMessage); } var cityList = _cityListRepository.GetCities(); var cities = _cityNameFinder.FindAllCitiesBeginningWithSubstring(cityList, searchString); var validNextLetters = _cityNextLetterHelper.GetNextLetters(cities, searchString); var cityResult = new CityResult { NextCities = cities.ToList(), NextLetters = validNextLetters.ToList(), }; return(cityResult); }
/// <summary> /// Queries the API, given a search-string, returning a CityResult object. /// </summary> /// <param name="searchString">The partially completed string to be search</param> /// <returns>CityResult object, containing the next letters, and next city names as lists of strings</returns> public ICityResult Search(string searchString) { CityResult deserializedJSON = new CityResult(); while (1 == 1) { try { string url = baseURL + searchString + types + "&key=" + apiKey.keyString; JSONHandler jsonHandler = new JSONHandler(); deserializedJSON = jsonHandler.GetDeserializeParse(searchString, url); return(deserializedJSON); } catch (Exception e) { Debug.Write("Request unsuccessful. Retrying & Printing e.Source: \n" + e.Source); Console.WriteLine(e.GetType().FullName, "\n", e.Message); } } return(null); }