Commit 044d110b authored by arnaudbey's avatar arnaudbey
Browse files

add getRandomDef and cleaing methods

parent d3302955
......@@ -4,23 +4,65 @@ namespace Innova\WiktionaryBundle\Manager;
class WiktionaryManager
{
public function __construct()
public function getDefinitions($lemma, $language)
{
$wiktionaryOutput = $this->requestWiktionary($lemma, $language);
if ($wiktionaryOutput) {
if (preg_match('/<span[\s\S]+?id="fr"[\s\S]*?(<ol>[\s\S]*?<\/ol>)/', $wiktionaryOutput, $matches)) {
return $matches[1];
}
}
return;
}
public function getDefinitions($lemma, $language)
public function getRandomDefinition($lemma, $language)
{
$definitions = $this->getDefinitions($lemma, $language);
if ($definitions) {
$definitions = $this->removeExamples($definitions);
if (preg_match_all('/<li>(.*?)<\/li>/s', $definitions, $matches)) {
$definitions = $matches[1];
$key = array_rand($definitions);
$definition = $definitions[$key];
return $definition;
}
}
return;
}
private function removeExamples($definitions)
{
$definitions = preg_replace("/<ul>.*?<\/ul>/s", '', $definitions);
return $definitions;
}
private function removeLink($definitions)
{
$definitions = preg_replace("/<a[^>]+>(.*?)<\/a>/s", '$1', $definitions);
return $definitions;
}
private function requestWiktionary($lemma, $language)
{
$url = 'https://'.$language.'.wiktionary.org/wiki/'.$lemma;
$handle = @fopen($url, 'r');
$def = 'Oups...';
$wiktionaryOutput = null;
if ($handle) {
while (!feof($handle)) {
$def = stream_get_contents($handle);
$wiktionaryOutput = stream_get_contents($handle);
$wiktionaryOutput = $this->removeLink($wiktionaryOutput);
}
fclose($handle);
}
return $def;
return $wiktionaryOutput;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment