Import.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace addons\cms\controller;
  3. use addons\cms\library\VicDict;
  4. /**
  5. * 导入
  6. * Class Import
  7. * @package addons\cms\controller
  8. */
  9. class Import extends Base
  10. {
  11. protected $noNeedLogin = ["*"];
  12. protected $layout = 'default';
  13. public function _initialize()
  14. {
  15. parent::_initialize();
  16. if (!$this->request->isCli()) {
  17. $this->error('只允许在终端进行操作!');
  18. }
  19. }
  20. /**
  21. * 导入词典
  22. */
  23. public function dict()
  24. {
  25. define('_VIC_WORD_DICT_PATH_', ADDON_PATH . 'cms/data/dict.json');
  26. $dict = new VicDict('json');
  27. //添加词语词库 add(词语,词性) 可以是除保留字符(/,\ , \x ,\i),以外的utf-8编码的任何字符
  28. $lines = file(ADDON_PATH . 'cms/data/dict.txt', FILE_IGNORE_NEW_LINES);
  29. foreach ($lines as $index => $line) {
  30. $lineArr = explode(' ', $line);
  31. $dict->add($lineArr[0], 'n');
  32. }
  33. //保存词库
  34. $dict->save();
  35. echo "done";
  36. }
  37. }