<?php namespace Faker\Provider; abstract class Text extends Base { protected static $baseText = ''; protected static $separator = ' '; protected static $separatorLen = 1; protected $explodedText; protected $consecutiveWords = []; protected static $textStartsWithUppercase = true; /** * Generate a text string by the Markov chain algorithm. * * Depending on the $maxNbChars, returns a random valid looking text. The algorithm * generates a weighted table with the specified number of words as the index and the * possible following words as the value. * * @example 'Alice, swallowing down her flamingo, and began by taking the little golden key' * * @param int $maxNbChars Maximum number of characters the text should contain (minimum: 10) * @param int $indexSize Determines how many words are considered for the generation of the next word. * The minimum is 1, and it produces a higher level of randomness, although the * generated text usually doesn't make sense. Higher index sizes (up to 5) * produce more correct text, at the price of less randomness. * * @return string */ public function realText($maxNbChars = 200, $indexSize = 2) { return $this->realTextBetween((int) round($maxNbChars * 0.8), $maxNbChars, $indexSize); } /** * Generate a text string by the Markov chain algorithm. * * Depending on the $maxNbChars, returns a random valid looking text. The algorithm * generates a weighted table with the specified number of words as the index and the * possible following words as the value. * * @example 'Alice, swallowing down her flamingo, and began by taking the little golden key' * * @param int $minNbChars Minimum number of characters the text should contain (maximum: 8) * @param int $maxNbChars Maximum number of characters the text should contain (minimum: 10) * @param int $indexSize Determines how many words are considered for the generation of the next word. * The minimum is 1, and it produces a higher level of randomness, although the * generated text usually doesn't make sense. Higher index sizes (up to 5) * produce more correct text, at the price of less randomness. * * @return string */ public function realTextBetween($minNbChars = 160, $maxNbChars = 200, $indexSize = 2) { if ($minNbChars < 1) { throw new \InvalidArgumentException('minNbChars must be at least 1'); } if ($maxNbChars < 10) { throw new \InvalidArgumentException('maxNbChars must be at least 10'); } if ($indexSize < 1) { throw new \InvalidArgumentException('indexSize must be at least 1'); } if ($indexSize > 5) { throw new \InvalidArgumentException('indexSize must be at most 5'); } if ($minNbChars >= $maxNbChars) { throw new \InvalidArgumentException('minNbChars must be smaller than maxNbChars'); } $words = $this->getConsecutiveWords($indexSize); $iterations = 0; do { ++$iterations; if ($iterations >= 100) { throw new \OverflowException(sprintf('Maximum retries of %d reached without finding a valid real text', $iterations)); } $result = $this->generateText($maxNbChars, $words); } while (static::strlen($result) <= $minNbChars); return $result; } /** * @param int $maxNbChars * @param array $words * * @return string */ protected function generateText($maxNbChars, $words) { $result = []; $resultLength = 0; // take a random starting point $next = static::randomKey($words); while ($resultLength < $maxNbChars && isset($words[$next])) { // fetch a random word to append $word = static::randomElement($words[$next]); // calculate next index $currentWords = static::explode($next); $currentWords[] = $word; array_shift($currentWords); $next = static::implode($currentWords); // ensure text starts with an uppercase letter if ($resultLength == 0 && !static::validStart($word)) { continue; } // append the element $result[] = $word; $resultLength += static::strlen($word) + static::$separatorLen; } // remove the element that caused the text to overflow array_pop($result); // build result $result = static::implode($result); return static::appendEnd($result); } protected function getConsecutiveWords($indexSize) { if (!isset($this->consecutiveWords[$indexSize])) { $parts = $this->getExplodedText(); $words = []; $index = []; for ($i = 0; $i < $indexSize; ++$i) { $index[] = array_shift($parts); } for ($i = 0, $count = count($parts); $i < $count; ++$i) { $stringIndex = static::implode($index); if (!isset($words[$stringIndex])) { $words[$stringIndex] = []; } $word = $parts[$i]; $words[$stringIndex][] = $word; array_shift($index); $index[] = $word; } // cache look up words for performance $this->consecutiveWords[$indexSize] = $words; } return $this->consecutiveWords[$indexSize]; } protected function getExplodedText() { if ($this->explodedText === null) { $this->explodedText = static::explode(preg_replace('/\s+/u', ' ', static::$baseText)); } return $this->explodedText; } protected static function explode($text) { return explode(static::$separator, $text); } protected static function implode($words) { return implode(static::$separator, $words); } protected static function strlen($text) { return function_exists('mb_strlen') ? mb_strlen($text, 'UTF-8') : strlen($text); } protected static function validStart($word) { $isValid = true; if (static::$textStartsWithUppercase) { $isValid = preg_match('/^\p{Lu}/u', $word); } return $isValid; } protected static function appendEnd($text) { return preg_replace("/([ ,-:;\x{2013}\x{2014}]+$)/us", '', $text) . '.'; } }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
ar_EG | Folder | 0755 |
|
|
ar_JO | Folder | 0755 |
|
|
ar_SA | Folder | 0755 |
|
|
at_AT | Folder | 0755 |
|
|
bg_BG | Folder | 0755 |
|
|
bn_BD | Folder | 0755 |
|
|
cs_CZ | Folder | 0755 |
|
|
da_DK | Folder | 0755 |
|
|
de_AT | Folder | 0755 |
|
|
de_CH | Folder | 0755 |
|
|
de_DE | Folder | 0755 |
|
|
el_CY | Folder | 0755 |
|
|
el_GR | Folder | 0755 |
|
|
en_AU | Folder | 0755 |
|
|
en_CA | Folder | 0755 |
|
|
en_GB | Folder | 0755 |
|
|
en_HK | Folder | 0755 |
|
|
en_IN | Folder | 0755 |
|
|
en_NG | Folder | 0755 |
|
|
en_NZ | Folder | 0755 |
|
|
en_PH | Folder | 0755 |
|
|
en_SG | Folder | 0755 |
|
|
en_UG | Folder | 0755 |
|
|
en_US | Folder | 0755 |
|
|
en_ZA | Folder | 0755 |
|
|
es_AR | Folder | 0755 |
|
|
es_ES | Folder | 0755 |
|
|
es_PE | Folder | 0755 |
|
|
es_VE | Folder | 0755 |
|
|
et_EE | Folder | 0755 |
|
|
fa_IR | Folder | 0755 |
|
|
fi_FI | Folder | 0755 |
|
|
fr_BE | Folder | 0755 |
|
|
fr_CA | Folder | 0755 |
|
|
fr_CH | Folder | 0755 |
|
|
fr_FR | Folder | 0755 |
|
|
he_IL | Folder | 0755 |
|
|
hr_HR | Folder | 0755 |
|
|
hu_HU | Folder | 0755 |
|
|
hy_AM | Folder | 0755 |
|
|
id_ID | Folder | 0755 |
|
|
is_IS | Folder | 0755 |
|
|
it_CH | Folder | 0755 |
|
|
it_IT | Folder | 0755 |
|
|
ja_JP | Folder | 0755 |
|
|
ka_GE | Folder | 0755 |
|
|
kk_KZ | Folder | 0755 |
|
|
ko_KR | Folder | 0755 |
|
|
lt_LT | Folder | 0755 |
|
|
lv_LV | Folder | 0755 |
|
|
me_ME | Folder | 0755 |
|
|
mn_MN | Folder | 0755 |
|
|
ms_MY | Folder | 0755 |
|
|
nb_NO | Folder | 0755 |
|
|
ne_NP | Folder | 0755 |
|
|
nl_BE | Folder | 0755 |
|
|
nl_NL | Folder | 0755 |
|
|
pl_PL | Folder | 0755 |
|
|
pt_BR | Folder | 0755 |
|
|
pt_PT | Folder | 0755 |
|
|
ro_MD | Folder | 0755 |
|
|
ro_RO | Folder | 0755 |
|
|
ru_RU | Folder | 0755 |
|
|
sk_SK | Folder | 0755 |
|
|
sl_SI | Folder | 0755 |
|
|
sr_Cyrl_RS | Folder | 0755 |
|
|
sr_Latn_RS | Folder | 0755 |
|
|
sr_RS | Folder | 0755 |
|
|
sv_SE | Folder | 0755 |
|
|
th_TH | Folder | 0755 |
|
|
tr_TR | Folder | 0755 |
|
|
uk_UA | Folder | 0755 |
|
|
vi_VN | Folder | 0755 |
|
|
zh_CN | Folder | 0755 |
|
|
zh_TW | Folder | 0755 |
|
|
Address.php | File | 3.49 KB | 0644 |
|
Barcode.php | File | 2.19 KB | 0644 |
|
Base.php | File | 20.44 KB | 0644 |
|
Biased.php | File | 1.79 KB | 0644 |
|
Color.php | File | 4.64 KB | 0644 |
|
Company.php | File | 901 B | 0644 |
|
DateTime.php | File | 11.88 KB | 0644 |
|
File.php | File | 25.1 KB | 0644 |
|
HtmlLorem.php | File | 9.98 KB | 0644 |
|
Image.php | File | 6 KB | 0644 |
|
Internet.php | File | 17.17 KB | 0644 |
|
Lorem.php | File | 7.62 KB | 0644 |
|
Medical.php | File | 648 B | 0644 |
|
Miscellaneous.php | File | 13.13 KB | 0644 |
|
Payment.php | File | 10.42 KB | 0644 |
|
Person.php | File | 3.23 KB | 0644 |
|
PhoneNumber.php | File | 6.45 KB | 0644 |
|
Text.php | File | 6.65 KB | 0644 |
|
UserAgent.php | File | 8.46 KB | 0644 |
|
Uuid.php | File | 1.78 KB | 0644 |
|