|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TextAnalysis\Corpus; |
| 4 | + |
| 5 | +use TextAnalysis\Tokenizers\GeneralTokenizer; |
| 6 | +use TextAnalysis\LexicalDiversity\Naive; |
| 7 | + |
| 8 | +/** |
| 9 | + * Explore the text corpus |
| 10 | + * @author yooper |
| 11 | + */ |
| 12 | +class TextCorpus |
| 13 | +{ |
| 14 | + /** |
| 15 | + * |
| 16 | + * @var string |
| 17 | + */ |
| 18 | + protected $text; |
| 19 | + |
| 20 | + /** |
| 21 | + * |
| 22 | + * @var array |
| 23 | + */ |
| 24 | + protected $tokens = []; |
| 25 | + |
| 26 | + public function __construct(string $text) |
| 27 | + { |
| 28 | + $this->text = $text; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Returns the original text |
| 33 | + * @return string |
| 34 | + */ |
| 35 | + public function getText() : string |
| 36 | + { |
| 37 | + return $this->text; |
| 38 | + } |
| 39 | + |
| 40 | + public function getTokens(string $tokenizerClassName = GeneralTokenizer::class) : array |
| 41 | + { |
| 42 | + if(empty($this->tokens)) { |
| 43 | + $this->tokens = tokenize($this->getText(), $tokenizerClassName); |
| 44 | + } |
| 45 | + return $this->tokens; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Return a list of positions that the needs were found in the text |
| 50 | + * @param array $needles |
| 51 | + * @return int[] |
| 52 | + */ |
| 53 | + public function getDispersion(array $needles) : array |
| 54 | + { |
| 55 | + $found = array_fill_keys($needles, []); |
| 56 | + foreach(array_keys($needles) as $needle) |
| 57 | + { |
| 58 | + $found[$needle] = $this->findAll($needle); |
| 59 | + } |
| 60 | + return $found; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Compute the lexical diversity, the default uses a naive algorithm |
| 65 | + * @param string $lexicalDiversityClassName |
| 66 | + * @return float |
| 67 | + */ |
| 68 | + public function getLexicalDiversity(string $lexicalDiversityClassName = Naive::class) : float |
| 69 | + { |
| 70 | + return lexical_diversity($this->getTokens(), $lexicalDiversityClassName); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * See https://stackoverflow.com/questions/15737408/php-find-all-occurrences-of-a-substring-in-a-string |
| 75 | + * @param string $needle |
| 76 | + * @param int $spacing The amount of space left and right of the found needle |
| 77 | + * @return array |
| 78 | + */ |
| 79 | + public function concordance(string $needle, int $spacing = 20) : array |
| 80 | + { |
| 81 | + $position = 0; |
| 82 | + $found = []; |
| 83 | + $text = trim(preg_replace('/[\s\t\n\r\s]+/', ' ', $this->text)); |
| 84 | + $needleLength = strlen($needle); |
| 85 | + $textLength = strlen($text); |
| 86 | + $bufferLength = $needleLength + 2 * $spacing; |
| 87 | + |
| 88 | + while (($position = stripos($text, $needle, $position))!== false) |
| 89 | + { |
| 90 | + $left = max($position - $spacing, 0); |
| 91 | + if($needleLength + $spacing + $position > $textLength) { |
| 92 | + $tmp = substr($text, $left); |
| 93 | + } else { |
| 94 | + $tmp = substr($text, $left, $bufferLength); |
| 95 | + } |
| 96 | + $found[] = $tmp; |
| 97 | + $position += $needleLength; |
| 98 | + } |
| 99 | + return $found; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Get percentage of times the needle shows up in the text |
| 104 | + * @param string $needle |
| 105 | + * @return float |
| 106 | + */ |
| 107 | + public function percentage(string $needle) : float |
| 108 | + { |
| 109 | + $freqDist = freq_dist($this->getTokens()); |
| 110 | + return $freqDist->getKeyValuesByFrequency()[$needle] / $freqDist->getTotalTokens(); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Performs a case insensitive search for the needle |
| 115 | + * @param string $needle |
| 116 | + * @return int |
| 117 | + */ |
| 118 | + public function count(string $needle) : int |
| 119 | + { |
| 120 | + return substr_count(strtolower($this->getText()), strtolower($needle)); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Return all the position of the needle found in the text |
| 125 | + * @param string $needle |
| 126 | + * @return array |
| 127 | + */ |
| 128 | + public function findAll(string $needle) : array |
| 129 | + { |
| 130 | + $lastPos = 0; |
| 131 | + $positions = []; |
| 132 | + $needle = strtolower($needle); |
| 133 | + $text = strtolower($this->getText()); |
| 134 | + $needleLength = strlen($needle); |
| 135 | + while (($lastPos = stripos($text, $needle, $lastPos))!== false) |
| 136 | + { |
| 137 | + $positions[] = $lastPos; |
| 138 | + $lastPos += $needleLength; |
| 139 | + } |
| 140 | + return $positions; |
| 141 | + } |
| 142 | + public function toString() |
| 143 | + { |
| 144 | + return $this->text; |
| 145 | + } |
| 146 | + |
| 147 | + public function __destruct() |
| 148 | + { |
| 149 | + unset($this->text); |
| 150 | + unset($this->tokens); |
| 151 | + } |
| 152 | +} |
0 commit comments