This repository has been archived on 2019-03-01. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
paper-pi/app/Paper/Vocabulary.php

88 lines
2.8 KiB
PHP

<?php
namespace App\Paper;
use DiDom\Document;
use DiDom\Query;
class Vocabulary
{
const URL = 'https://www.vocabulary.com/';
const EXAMPLES_URL = 'https://corpus.vocabulary.com/api/1.0/';
public function getWordData($word)
{
return new Document($this->getWordUrl($word), true);
}
private function getWordUrl($word)
{
$word = trim($word);
return self::URL . 'dictionary/definition.ajax?search=' . $word . '&lang=en';
}
private function getWordExamplesUrl($word, $maxResult = 10, $offset = 0)
{
$word = trim($word);
return sprintf('%sexamples.json?query=%s&maxResults=%d&startOffset=%d', self::EXAMPLES_URL, $word, $maxResult, $offset);
}
public function getWord($word)
{
$document = $this->getWordData($word);
$wordData = new \stdClass();
$wordPageSelector = $document->first('.wordPage');
$wordData->lang = $wordPageSelector->attr('data-lang');
$wordData->word = $wordPageSelector->attr('data-word');
$wordData->next = $wordPageSelector->attr('data-next');
$wordData->prev = $wordPageSelector->attr('data-prev');
$wordData->short = trim($document->first('p.short')->text());
$wordData->long = trim($document->first('p.long')->text());
$wordData->instances = [];
foreach ($document->find('.ordinal ') as $ordinal) {
$definition = [];
$definition['type'] = $ordinal->first('h3')->first('a')->attr('title');
$definition['definition'] = trim($ordinal->first('//h3[@class="definition"][1]/text()[2]', Query::TYPE_XPATH));
$definition['example'] = ($example = $ordinal->first('//div[@class="example"]', Query::TYPE_XPATH)) ? $example->text() : '';
foreach ($ordinal->find('.instances') as $instance) {
$in = [];
$in['type'] = str_replace(' ', '_', trim(strtolower($instance->first('dt::text()')), ': '));
$in['words'] = $instance->find('dd a::text()');
$in['definition'] = $instance->first('div.definition::text()');
$definition[$in['type']] = $in;
}
$wordData->instances[] = $definition;
}
$wordData->examples = $this->getExamples($word, 3);
return $wordData;
}
private function getRandomWordUrl()
{
return self::URL . 'randomword.json';
}
public function getRandomWord()
{
$data = json_decode(file_get_contents($this->getRandomWordUrl()));
if ($data->status != 0) {
throw new Exception('error');
}
return $data->result->word;
}
public function getExamples($word, $maxResult = 10, $offset = 0)
{
return json_decode(file_get_contents($this->getWordExamplesUrl($word, $maxResult, $offset)), 1);
}
}