1 个不稳定版本
0.1.0 | 2020 年 12 月 28 日 |
---|
#25 在 #autocomplete
45KB
849 行
Datamuse Api 包装器
本库为 Rust 项目提供了一个针对 Datamuse api 的非官方包装库。Datamuse api 允许用户根据大量不同参数从不同的词汇表中检索单词列表。此外,它还可以根据字母串检索单词,从而实现自动完成功能。目前,此 API 不需要 API 密钥,每天可以最多使用 100,000 次请求,之后可能会受到速率限制(关于更高使用量,请参阅网站)。如果您使用此 API,应在项目中引用 Datamuse api。更多信息请参阅官方文档:https://www.datamuse.com/api/
示例
单词端点
extern crate tokio;
extern crate datamuse_api_wrapper;
use datamuse_api_wrapper::{ DatamuseClient, Vocabulary, EndPoint, RelatedType };
#[tokio::main]
async fn main() -> datamuse_api_wrapper::Result<()> {
let client = DatamuseClient::new();
let request = client.new_query(Vocabulary::English, EndPoint::Words)
.means_like("breakfast") // The words we're looking for are related to "breakfast"
.related(RelatedType::Rhyme, "grape"); // and rhyme with "grape"
let word_list = request.list().await?; // Get the list of words from the api
assert_eq!("crepe", word_list[0].word); // "crepe" is the only result as of writing this
Ok(())
}
建议端点
extern crate tokio;
extern crate datamuse_api_wrapper;
use datamuse_api_wrapper::{ DatamuseClient, Vocabulary, EndPoint };
#[tokio::main]
async fn main() -> datamuse_api_wrapper::Result<()> {
let client = DatamuseClient::new();
let request = client.new_query(Vocabulary::English, EndPoint::Suggest)
.hint_string("hello wor") // The user has alread typed in "hello wor"
.max_results(2); // We only want the first 2 results to be returned
let request = request.build()?; // Build the request
let response = request.send().await?; // Send the request
let word_list = response.list()?; // Parse the response into a word_list
assert_eq!("hello world", word_list[0].word); // "hello world" is the first result as of writing this
Ok(())
}
依赖项
~3.5–8MB
~186K SLoC