[][src]Struct city_spellcheck::CityData

pub struct CityData { /* fields omitted */ }

CityData holds all city data, each city is stored vertically across fields at a given index

Methods

impl CityData
[src]

pub fn new() -> Self
[src]

Instantiates a new instance of CityData that is empty. Usually populated with populate_from_file method

pub fn populate_from_file(
    &mut self,
    filename: &str
) -> Result<(), Box<dyn Error>>
[src]

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

pub fn get_city(&self, idx: usize) -> City
[src]

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.

Example

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 }"
    );

pub fn total_score(
    &self,
    term: &str,
    idx: usize,
    loc: Option<Coordinate>
) -> f32
[src]

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.

Example

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);

pub fn find_distance_earth(loc1: Coordinate, loc2: Coordinate) -> f32
[src]

Finds circular distance from two gps coordinates using haversine formula. Just supply two locations as Coordinate instances and get back distance in kilometers.

Example

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);

pub fn search(&self, term: &str, loc: Option<Coordinate>) -> Vec<FuzzyResult>
[src]

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.

Example

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 }]");

Trait Implementations

impl Debug for CityData
[src]

Auto Trait Implementations

impl Send for CityData

impl Sync for CityData

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> From for T
[src]

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T> Erased for T