Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

143 righe
3.0 KiB

  1. <?php
  2. /*
  3. * This file is part of the overtrue/pinyin.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Overtrue\Pinyin;
  11. use Closure;
  12. use SplFileObject;
  13. use Generator;
  14. /**
  15. * Generator syntax(yield) Dict File loader.
  16. */
  17. class GeneratorFileDictLoader implements DictLoaderInterface
  18. {
  19. /**
  20. * Data directory.
  21. *
  22. * @var string
  23. */
  24. protected $path;
  25. /**
  26. * Words segment name.
  27. *
  28. * @var string
  29. */
  30. protected $segmentName = 'words_%s';
  31. /**
  32. * SplFileObjects.
  33. *
  34. * @var array
  35. */
  36. protected static $handles = [];
  37. /**
  38. * surnames.
  39. *
  40. * @var SplFileObject
  41. */
  42. protected static $surnamesHandle;
  43. /**
  44. * Constructor.
  45. *
  46. * @param string $path
  47. */
  48. public function __construct($path)
  49. {
  50. $this->path = $path;
  51. for ($i = 0; $i < 100; ++$i) {
  52. $segment = $this->path.'/'.sprintf($this->segmentName, $i);
  53. if (file_exists($segment) && is_file($segment)) {
  54. array_push(static::$handles, $this->openFile($segment));
  55. }
  56. }
  57. }
  58. /**
  59. * Construct a new file object.
  60. *
  61. * @param string $filename file path
  62. *
  63. * @return SplFileObject
  64. */
  65. protected function openFile($filename, $mode = 'r')
  66. {
  67. return new SplFileObject($filename, $mode);
  68. }
  69. /**
  70. * get Generator syntax.
  71. *
  72. * @param array $handles SplFileObjects
  73. */
  74. protected function getGenerator(array $handles)
  75. {
  76. foreach ($handles as $handle) {
  77. $handle->seek(0);
  78. while ($handle->eof() === false) {
  79. $string = str_replace(['\'', ' ', PHP_EOL, ','], '', $handle->fgets());
  80. if (strpos($string, '=>') === false) {
  81. continue;
  82. }
  83. list($string, $pinyin) = explode('=>', $string);
  84. yield $string => $pinyin;
  85. }
  86. }
  87. }
  88. /**
  89. * Traverse the stream.
  90. *
  91. * @param Generator $generator
  92. * @param Closure $callback
  93. *
  94. * @author Seven Du <shiweidu@outlook.com>
  95. */
  96. protected function traversing(Generator $generator, Closure $callback)
  97. {
  98. foreach ($generator as $string => $pinyin) {
  99. $callback([$string => $pinyin]);
  100. }
  101. }
  102. /**
  103. * Load dict.
  104. *
  105. * @param Closure $callback
  106. */
  107. public function map(Closure $callback)
  108. {
  109. $this->traversing($this->getGenerator(static::$handles), $callback);
  110. }
  111. /**
  112. * Load surname dict.
  113. *
  114. * @param Closure $callback
  115. */
  116. public function mapSurname(Closure $callback)
  117. {
  118. if (!static::$surnamesHandle instanceof SplFileObject) {
  119. static::$surnamesHandle = $this->openFile($this->path.'/surnames');
  120. }
  121. $this->traversing($this->getGenerator([static::$surnamesHandle]), $callback);
  122. }
  123. }