|
4 | 4 |
|
5 | 5 | class Text extends \Faker\Provider\Text
|
6 | 6 | {
|
7 |
| - protected $explodedText = null; |
8 |
| - protected $consecutiveWords = array(); |
| 7 | + protected static $separator = ''; |
| 8 | + protected static $separatorLen = 0; |
| 9 | + protected static $punct = array('、', '。', '」', '』', '!', '?', 'ー', ',', ':', ';'); |
9 | 10 |
|
10 | 11 | /**
|
11 | 12 | * Title: 三國演義 Romance of the Three Kingdoms
|
@@ -77,98 +78,23 @@ class Text extends \Faker\Provider\Text
|
77 | 78 | 三人飛馬引軍而出。張角正殺敗董卓,乘勢趕來,忽遇三人衝殺,角軍大亂,敗走五十餘里。三人救了董卓回寨。卓問三人現居何職。玄德曰:「白身。」卓甚輕之,不為禮。玄德出,張飛大怒曰:「我等親赴血戰,救了這廝,他卻如此無禮;若不殺之,難消我氣!」便要提刀入帳來殺董卓。正是:人情勢利古猶今,誰識英雄是白身?安得快人如翼德,盡誅世上負心人!畢竟董卓性命如何,且看下文分解。
|
78 | 79 | EOT;
|
79 | 80 |
|
80 |
| - public function realText($maxNbChars = 200, $indexSize = 2) |
| 81 | + protected static function explode($text) |
81 | 82 | {
|
82 |
| - if ($maxNbChars < 10) { |
83 |
| - throw new \InvalidArgumentException('maxNbChars must be at least 10'); |
84 |
| - } |
85 |
| - if ($indexSize < 1) { |
86 |
| - throw new \InvalidArgumentException('indexSize must be at least 1'); |
87 |
| - } |
88 |
| - if ($indexSize > 5) { |
89 |
| - throw new \InvalidArgumentException('indexSize must be at most 5'); |
90 |
| - } |
91 |
| - |
92 |
| - $words = $this->getConsecutiveWords($indexSize); |
93 |
| - $result = array(); |
94 |
| - $resultLength = 0; |
95 |
| - // take a random starting point |
96 |
| - $punct = array('、', '。', '」', '』', '!', '?', 'ー', ',', ':', ';'); |
97 |
| - $next = static::randomKey($words); |
98 |
| - while ($resultLength < $maxNbChars && isset($words[$next])) { |
99 |
| - // fetch a random word to append |
100 |
| - $word = static::randomElement($words[$next]); |
101 |
| - |
102 |
| - // calculate next index |
103 |
| - $currentWords = static::split($next); |
104 |
| - $currentWords[] = $word; |
105 |
| - array_shift($currentWords); |
106 |
| - $next = implode('', $currentWords); |
107 |
| - |
108 |
| - // ensure the first word is not punctuation |
109 |
| - if ($resultLength === 0 and in_array($word, $punct)) { |
110 |
| - continue; |
111 |
| - } |
112 |
| - |
113 |
| - // append the element |
114 |
| - $result[] = $word; |
115 |
| - $resultLength += static::strlen($word); |
116 |
| - } |
117 |
| - |
118 |
| - // remove the element that caused the text to overflow |
119 |
| - array_pop($result); |
120 |
| - |
121 |
| - // build result |
122 |
| - $result = implode('', $result); |
123 |
| - |
124 |
| - return $result.static::randomElement(array('。', '!', '?',)); |
125 |
| - } |
126 |
| - |
127 |
| - protected function getConsecutiveWords($indexSize) |
128 |
| - { |
129 |
| - if (!isset($this->consecutiveWords[$indexSize])) { |
130 |
| - $parts = $this->getExplodedText(); |
131 |
| - $words = array(); |
132 |
| - $index = array(); |
133 |
| - for ($i = 0; $i < $indexSize; $i++) { |
134 |
| - $index[] = array_shift($parts); |
135 |
| - } |
136 |
| - |
137 |
| - for ($i = 0, $count = count($parts); $i < $count; $i++) { |
138 |
| - $stringIndex = implode('', $index); |
139 |
| - if (!isset($words[$stringIndex])) { |
140 |
| - $words[$stringIndex] = array(); |
141 |
| - } |
142 |
| - $word = $parts[$i]; |
143 |
| - $words[$stringIndex][] = $word; |
144 |
| - array_shift($index); |
145 |
| - $index[] = $word; |
146 |
| - } |
147 |
| - // cache look up words for performance |
148 |
| - $this->consecutiveWords[$indexSize] = $words; |
149 |
| - } |
150 |
| - |
151 |
| - return $this->consecutiveWords[$indexSize]; |
| 83 | + return array_values(array_filter(preg_split('//u', preg_replace('/\s+/', '', $text)))); |
152 | 84 | }
|
153 | 85 |
|
154 |
| - protected function getExplodedText() |
| 86 | + protected static function strlen($text) |
155 | 87 | {
|
156 |
| - if ($this->explodedText === null) { |
157 |
| - $this->explodedText = static::split(static::$baseText); |
158 |
| - } |
159 |
| - return $this->explodedText; |
| 88 | + return function_exists('mb_get_info') ? mb_strlen($text) : count(static::split($text)); |
160 | 89 | }
|
161 | 90 |
|
162 |
| - public static function split($text) |
| 91 | + protected static function validStart($word) |
163 | 92 | {
|
164 |
| - return array_values(array_filter(preg_split('//u', preg_replace('/\s+/', '', $text)))); |
| 93 | + return !in_array($word, static::$punct); |
165 | 94 | }
|
166 | 95 |
|
167 |
| - public static function strlen($text) |
| 96 | + protected static function appendEnd($text) |
168 | 97 | {
|
169 |
| - if (function_exists('mb_get_info')) { |
170 |
| - return mb_strlen($text); |
171 |
| - } |
172 |
| - return count(static::split($text)); |
| 98 | + return $text.static::randomElement(array('。', '!', '?',)); |
173 | 99 | }
|
174 | 100 | }
|
0 commit comments