pub struct CityData { /* fields omitted */ }
CityData holds all city data, each city is stored vertically across fields at a given index
Instantiates a new instance of CityData that is empty. Usually populated with
populate_from_file
method
Takes in a geonames file and matches countries and regions to our custom enum variants
It then adds each city in line by line to our CityData instance.
Note that geonames file comes from: http://download.geonames.org/export/dump
Nicer formatted version is available in the github project for this crate under
the data folder:
city-spellcheck github
get_city
is a function to get a city back from our CityData
struct.
Helps us keep our data stored in a DoD fashion and provide
a higher level abstraction to retrieve each city based on index
Supply an index and get back the city at that index.
use city_spellcheck::*;
let mut cities = CityData::new();
cities
.populate_from_file("data/cities_canada-usa-filtered.csv")
.unwrap();
assert_eq!(
format!("{:?}", cities.get_city(0)),
"City { name: \"Abbotsford\", country: CA, region: Province(BC), latitude: 49.05798, longitude: -122.25257 }"
);
total_score
takes into account location as well as
string distance using sift4 algorithm. Supply the index of the city
and optionally a Coordinate instance representing your current location.
total_score
will then return a weighted fuzzy match score.
use city_spellcheck::*;
let mut cities = CityData::new();
cities
.populate_from_file("data/cities_canada-usa-filtered.csv")
.unwrap();
assert_eq!(cities.total_score("Abbotsfor", 0, None), 0.88888896);
Finds circular distance from two gps coordinates using haversine formula.
Just supply two locations as Coordinate instances and get back distance
in kilometers.
use city_spellcheck::*;
let sf = Coordinate::new(37.774929, -122.419416);
let nyc = Coordinate::new(40.730610, -73.935242);
assert_eq!(CityData::find_distance_earth(sf, nyc), 4135.694);
search
will search through all the cities stored in CityData, optionally
including a set of coordinates for distance weighting, and return a vector
of FuzzyResult's (instances that include the city name in a human readable format,
the lat and long of the city, and the fuzzy score).
search
sorts results from highest score (1.0 max) to lowest score. search
only
shows results with a score greater than 0.5.
use city_spellcheck::*;
let mut cities = CityData::new();
cities
.populate_from_file("data/cities_canada-usa-filtered.csv")
.unwrap();
let london = Coordinate::new(42.98339, -81.23304);
let results = cities.search("London", Some(london));
assert_eq!(
format!("{:?}", results),
"[FuzzyResult { city: \"London, ON, CA\", latitude: 42.98339, longitude: -81.23304, score: 1.0 }, FuzzyResult { city: \"London, OH, US\", latitude: 39.88645, longitude: -83.44825, score: 0.6252391 }, FuzzyResult { city: \"London, KY, US\", latitude: 37.12898, longitude: -84.08326, score: 0.6250727 }, FuzzyResult { city: \"Lemont, IL, US\", latitude: 41.67364, longitude: -88.00173, score: 0.52094036 }, FuzzyResult { city: \"Brant, ON, CA\", latitude: 43.1334, longitude: -80.34967, score: 0.5208334 }]");
Formats the value using the given formatter. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Mutably borrows from an owned value. Read more