language-distribution
language-distribution is a Rust crate that integrates with the country and language_enum crates to provide language distribution data for each country. Given a Country enum variant (from the country crate), the crate returns a HashMap<Language, f64> indicating the relative prevalence of each language spoken within that country. Additionally, it provides a method to obtain a random language selection weighted by these distributions.
Features
-
Country-to-Language Mappings:
For eachCountryenum variant, a corresponding set of(Language, f64)pairs is provided, representing the proportion of speakers in that country. -
Integration with
language_enum:
Uses theLanguageenum (fromlanguage_enum) as keys in the resultingHashMap, ensuring consistency with other language-related functionality. -
Probability-Based Random Language Selection:
Implements aRandomLanguagetrait forCountry, allowing selection of a randomLanguageaccording to the weighted distribution obtained fromlanguage_distribution(). -
Flexible Data Structures:
Returns aHashMap<Language, f64>for easy integration with downstream code for analytics, UI display, or further processing.
Usage
Add this to your Cargo.toml:
[]
= "0.1"
= "0.1"
= "0.1"
= "0.8"
Then in your code:
use Country;
use ;
Data Sources and Accuracy
The language distributions are approximations, not meant to be authoritative. Adjust values as needed for more accuracy or updated data.
Testing
An exhaustive test suite is included. Run tests using:
The tests verify:
- The correctness of distributions for a sample of countries.
- The presence of expected languages for given countries.
- The random language selection method to ensure it returns valid languages from the distribution.
License
This project is licensed under the MIT license. See LICENSE for details.
Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.