getSuggestion method

MunicipalityModel getSuggestion (
  1. String municipalityQuery
)

Method that returns the best match of the given municipality with the cuban municipalities. The best match is calculated using the Damerau-Levenshtein distance.

Implementation

MunicipalityModel getSuggestion(String municipalityQuery) {
  municipalityQuery = curateMunicipality(municipalityQuery);
  var bestMunicipality = municipalities[0];
  var bestDistance = distance(municipalityQuery, bestMunicipality.nameCured);
  for (var i = 1; i < municipalities.length; ++i) {
    var tmpMunicipality = municipalities[i];
    var tmpDistance = distance(municipalityQuery, tmpMunicipality.nameCured);
    if (tmpDistance < bestDistance) {
      bestMunicipality = tmpMunicipality;
      bestDistance = tmpDistance;
    }
  }
  return bestMunicipality;
}