Created
January 15, 2021 13:51
-
-
Save sedera-tax/bf10e637558a83c4e92847c722ce1ca2 to your computer and use it in GitHub Desktop.
5. Thesaurus A thesaurus contains words and synonyms for each word. Below is an example of a data structure that defines a thesaurus: array("buy" => array("purchase"), "big" => array("great", "large")) Implement the function getSynonyms, which accepts a word as a string and returns all synonyms for that word in JSON format, as in the example be…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Thesaurus | |
{ | |
private $thesaurus; | |
function __construct(array $thesaurus) | |
{ | |
$this->thesaurus = $thesaurus; | |
} | |
public function getSynonyms(string $word) : string | |
{ | |
return (array_key_exists($word, $this->thesaurus)) ? '{"word":'.json_encode($word).',"synonyms":'.json_encode($this->thesaurus[$word]).'}' : '{"word":'.json_encode($word).',"synonyms":[]}'; | |
} | |
} | |
$thesaurus = new Thesaurus( | |
[ | |
"buy" => array("purchase"), | |
"big" => array("great", "large") | |
] | |
); | |
echo $thesaurus->getSynonyms("big"); | |
echo "\n"; | |
echo $thesaurus->getSynonyms("agelast"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment