undeadyetii/datamuse-php-api-wrapper

datamuse.com API 的 PHP 封装。

此包的规范存储库似乎已丢失,因此该包已被冻结。

v1.0 2018-02-23 03:41 UTC

This package is not auto-updated.

Last update: 2020-04-16 02:29:10 UTC


README

Latest Version on Packagist Software License Total Downloads

datamuse.com API 的 PHP 封装。

安装

通过 Composer

$ composer require undeadyetii/datamuse-php-api-wrapper

用法

选项

有 4 种不同的方法来调用一个选项/关系。下面是一个表格,显示了每种方法。

命令名称 API 代码 RhymeOpt 常量 设置方法
意味着 ml RhymeOpt::MEANS_LIKE meansLike()
听起来像 sl RhymeOpt::SOUNDS_LIKE soundsLike()
拼写像 sp RhymeOpt::SPELLED_LIKE spelledLike()
形容词的名词 rel_jja RhymeOpt::NOUNS_BY_ADJECTIVE nounsByAdjective()
名词的形容词 rel_jjb RhymeOpt::ADJECTIVES_BY_NOUN adjectivesByNoun()
同义词 rel_syn RhymeOpt::SYNONYMS_OF synonymsOf()
触发器 rel_trg RhymeOpt::TRIGGERS_OF triggersOf()
反义词 rel_ant RhymeOpt::ANTONYMS_OF antonymsOf()
比...更具体 rel_spc RhymeOpt::MORE_SPECIFIC_THAN moreSpecificThan()
比...更一般 rel_gen RhymeOpt::MORE_GENERAL_THAN moreGeneralThan()
包含 rel_com RhymeOpt::COMPRISES_OF comprisesOf()
部分 rel_par RhymeOpt::PART_OF partOf()
跟随的单词 rel_bga RhymeOpt::WORDS_FOLLOWING wordsFollowing()
先前的单词 rel_bgb RhymeOpt::WORDS_PRECEDING wordsPreceding()
完美的押韵 rel_rhy RhymeOpt::PERFECT_RHYMES perfectRhymes()
近似押韵 rel_nry RhymeOpt::APPROX_RHYMES approximateRhymes()
同音异义词 rel_hom RhymeOpt::HOMOPHONES_OF homophonesOf()
匹配辅音 rel_cns RhymeOpt::MATCHES_CONSONANT matchesConsonants()
主题 topics RhymeOpt::OF_TOPIC ofTopic()
左侧上下文 lc RhymeOpt::LEFT_CONTEXT leftContext()
右侧上下文 rc RhymeOpt::RIGHT_CONTEXT rightContext()

它们用于指定一个关系以构建您的响应(更多信息请参阅 这里)。
下面展示了如何使用这些代码。

PHP 代码示例
use \YeTii\DatamuseApi\RhymeOpt;
use \YeTii\DatamuseApi\ApiClient;
$client = new ApiClient();

// Set option (following 4 lines produce same result)
$client->setOpt('spelled like', 'elepant');          // uses Command Name      | passes 'elepant' as the word
$client->setOpt('sp', 'elepant');                    // uses API Code          | passes 'elepant' as the word
$client->setOpt(RhymeOpt::SPELLED_LIKE, 'elepant');  // uses RhymeOpt Constant | passes 'elepant' as the word
$client->spelledLike('elepant');                     // uses Setter Method     | passes 'elepant' as the word

// Get the words (returns ApiClient instance still)
$client->getWords();
// Get the result array:
$result = $client->result;
// Get where the result is from (cache or fresh from api):
$result_from = $client->result_from; // `cache` or `api`

// Setting multiple options (you can mix an match Command Names / Code / RhymeOpt Constants)
$client->setOpts([
	RhymeOpt::SPELLED_LIKE => 'elepant',
	'of topic' => 'animals'
]);

// You can chain the commands (again, you can mix and match):
$result = $client->setOpt(RhymeOpt::EXACT, 'bake')->ofTopic('food')->getWords()->result;
// Find exact rhymes of the word "bake" that relate to the topic "food", get the words and give me the results

// Result: Now you have your cake.
缓存

缓存使用 inouet/file-cache 包(更多信息请参阅 https://github.com/inouet/file-cache

use \YeTii\DatamuseApi\ApiClient;
$time = 86400; // time in seconds before cache should expire; default:86400; should be no less than 86400
$dir = __DIR__.'/cache'; // absolute path of folder to store cache in
$client = new ApiClient([
	'cache_lifetime'=>$time,
	'cache_dir'=>$dir
]);

$result = $client->setOpt(RhymeOpt::EXACT, 'bake')->ofTopic('food')->getWords()->result; // not cached
// now exact_rhyme:bake|of_topic:food is now cached.
$result = $client->setOpt(RhymeOpt::EXACT, 'bake')->ofTopic('food')->getWords()->result; // cached

// DISABLING CACHE (not recommended -- if you do, add your own caching methods)
$client = new ApiClient([
	'cache_enable'=>false, // to disable
]);
// alternatively, setting cache_lifetime to 0 should work too, but cache_enable=>false will completely stop caching

测试

$ composer test

致谢

许可

MIT 许可(MIT)。有关更多信息,请参阅 许可文件