dictionary)) $this->initDictionary(); } private function initDictionary() { $this->log("start load english dictionary ..."); $file = __DIR__ . '/files/cedict_ts.u8'; $dict_file = __DIR__ . '/files/dict.json'; if (file_exists($dict_file)) { $this->dictionary = json_decode(file_get_contents($dict_file), true); $this->log("loaded dictionary!"); return; } $parser = new Parser(); $parser->setOptions([ Entry::F_SIMPLIFIED, Entry::F_PINYIN_DIACRITIC, Entry::F_ENGLISH_EXPANDED, // Entry::F_PINYIN_DIACRITIC_EXPANDED, // Entry::F_ORIGINAL, ]); $parser->setFilePath($file); $dict = []; $this->log("start parse dictionary ..."); foreach ($parser->parse() as $output) { foreach ($output['parsedLines'] as $line) { if(!isset($dict[$line['simplified']])){ $dict[$line['simplified']] = []; } $dict[$line['simplified']][] = $line; } } file_put_contents($dict_file, json_encode($dict, JSON_UNESCAPED_UNICODE)); $this->dictionary = $dict; $this->log("load english dictionary finish!"); } /** * @param Request $request * @return Response * @throws \Exception */ public function handle(Request $request) { $word = $request->get('word'); if (empty($word)) return $this->error('need query word'); if (isset($this->dictionary[$word])) { return $this->success($this->dictionary[$word]); } return $this->json([]); } }