| Total Complexity | 67 |
| Total Lines | 422 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Sentence often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Sentence, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Sentence { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Specify this flag with the split method to trim whitespace. |
||
| 19 | */ |
||
| 20 | const SPLIT_TRIM = 0x1; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * List of characters used to terminate sentences. |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | private $terminals = array('.', '!', '?'); |
||
| 27 | |||
| 28 | /** |
||
| 29 | * List of characters used for abbreviations. |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | private $abbreviators = array('.'); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Multibyte safe version of standard trim() function. |
||
| 36 | * @param string $string |
||
| 37 | * @return string |
||
| 38 | */ |
||
| 39 | private static function mbTrim($string) |
||
| 40 | { |
||
| 41 | return mb_ereg_replace('^\s*([\s\S]*?)\s*$', '\1', $string); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * A cross between mb_split and preg_split, adding the preg_split flags |
||
| 46 | * to mb_split. |
||
| 47 | * @param string $pattern |
||
| 48 | * @param string $string |
||
| 49 | * @param int $limit |
||
| 50 | * @param int $flags |
||
| 51 | * @return array |
||
| 52 | */ |
||
| 53 | private static function mbSplit($pattern, $string, $limit = -1, $flags = 0) |
||
| 54 | { |
||
| 55 | $strlen = strlen($string); // bytes! |
||
| 56 | mb_ereg_search_init($string); |
||
| 57 | |||
| 58 | $lengths = array(); |
||
| 59 | $position = 0; |
||
| 60 | while (($array = mb_ereg_search_pos($pattern, '')) !== false) { |
||
| 61 | // capture split |
||
| 62 | $lengths[] = array($array[0] - $position, false, null); |
||
| 63 | |||
| 64 | // move position |
||
| 65 | $position = $array[0] + $array[1]; |
||
| 66 | |||
| 67 | // capture delimiter |
||
| 68 | $regs = mb_ereg_search_getregs(); |
||
| 69 | $lengths[] = array($array[1], true, isset($regs[1]) && $regs[1]); |
||
| 70 | |||
| 71 | // Continue on? |
||
| 72 | if ($position >= $strlen) { |
||
| 73 | break; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | // Add last bit, if not ending with split |
||
| 78 | $lengths[] = array($strlen - $position, false, null); |
||
| 79 | |||
| 80 | // Substrings |
||
| 81 | $parts = array(); |
||
| 82 | $position = 0; |
||
| 83 | $count = 1; |
||
| 84 | foreach ($lengths as $length) { |
||
| 85 | $is_delimiter = $length[1]; |
||
| 86 | $is_captured = $length[2]; |
||
| 87 | |||
| 88 | if ($limit > 0 && !$is_delimiter && ($length[0] || ~$flags & PREG_SPLIT_NO_EMPTY) && ++$count > $limit) { |
||
| 89 | if ($length[0] > 0 || ~$flags & PREG_SPLIT_NO_EMPTY) { |
||
| 90 | $parts[] = $flags & PREG_SPLIT_OFFSET_CAPTURE ? array(mb_strcut($string, $position), $position) : mb_strcut($string, $position); |
||
| 91 | } |
||
| 92 | break; |
||
| 93 | } elseif ((!$is_delimiter || ($flags & PREG_SPLIT_DELIM_CAPTURE && $is_captured)) && ($length[0] || ~$flags & PREG_SPLIT_NO_EMPTY)) { |
||
| 94 | $parts[] = $flags & PREG_SPLIT_OFFSET_CAPTURE ? array(mb_strcut($string, $position, $length[0]), $position) : mb_strcut($string, $position, $length[0]); |
||
| 95 | } |
||
| 96 | |||
| 97 | $position += $length[0]; |
||
| 98 | } |
||
| 99 | |||
| 100 | return $parts; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Breaks a piece of text into lines by linebreak. |
||
| 105 | * Eats up any linebreak characters as if one. |
||
| 106 | * |
||
| 107 | * Multibyte safe |
||
| 108 | * |
||
| 109 | * @param string $text |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | private static function linebreakSplit($text) |
||
| 113 | { |
||
| 114 | $lines = array(); |
||
| 115 | $line = ''; |
||
| 116 | |||
| 117 | foreach (self::mbSplit('([\r\n]+)', $text, -1, PREG_SPLIT_DELIM_CAPTURE) as $part) { |
||
| 118 | $line .= $part; |
||
| 119 | if (self::mbTrim($part) === '') { |
||
| 120 | $lines[] = $line; |
||
| 121 | $line = ''; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | $lines[] = $line; |
||
| 125 | |||
| 126 | return $lines; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Replace |
||
| 131 | * @staticvar array $chr_map |
||
| 132 | * @param String $string |
||
| 133 | * @return String |
||
| 134 | */ |
||
| 135 | private static function cleanUnicode($string) |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Splits an array of lines by (consecutive sequences of) |
||
| 171 | * terminals, keeping terminals. |
||
| 172 | * |
||
| 173 | * Multibyte safe (atleast for UTF-8) |
||
| 174 | * |
||
| 175 | * For example: |
||
| 176 | * "There ... is. More!" |
||
| 177 | * ... becomes ... |
||
| 178 | * [ "There ", "...", " is", ".", " More", "!" ] |
||
| 179 | * |
||
| 180 | * @param array $lines |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | private function punctuationSplit($line) |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Appends each terminal item after it's preceding |
||
| 209 | * non-terminals. |
||
| 210 | * |
||
| 211 | * Multibyte safe (atleast for UTF-8) |
||
| 212 | * |
||
| 213 | * For example: |
||
| 214 | * [ "There ", "...", " is", ".", " More", "!" ] |
||
| 215 | * ... becomes ... |
||
| 216 | * [ "There ... is.", "More!" ] |
||
| 217 | * |
||
| 218 | * @param array $punctuations |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | private function punctuationMerge($punctuations) |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Looks for capitalized abbreviations & includes them with the following fragment. |
||
| 254 | * |
||
| 255 | * For example: |
||
| 256 | * [ "Last week, former director of the F.B.I. James B. Comey was fired. Mr. Comey was not available for comment." ] |
||
| 257 | * ... becomes ... |
||
| 258 | * [ "Last week, former director of the F.B.I. James B. Comey was fired." ] |
||
| 259 | * [ "Mr. Comey was not available for comment." ] |
||
| 260 | * |
||
| 261 | * @param array $fragments |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | private function abbreviationMerge($fragments) |
||
| 265 | { |
||
| 266 | $return_fragment = array(); |
||
| 267 | |||
| 268 | $previous_string = ''; |
||
| 269 | $previous_is_abbreviation = false; |
||
| 270 | $i = 0; |
||
| 271 | |||
| 272 | foreach ($fragments as $fragment) { |
||
| 273 | $current_string = $fragment; |
||
| 274 | $words = mb_split('\s+', self::mbTrim($fragment)); |
||
| 275 | |||
| 276 | $word_count = count($words); |
||
| 277 | |||
| 278 | // if last word of fragment starts with a Capital, ends in "." & has less than 3 characters, trigger "is abbreviation" |
||
| 279 | $last_word = trim($words[$word_count - 1]); |
||
| 280 | $last_is_capital = preg_match('#^\p{Lu}#u', $last_word); |
||
| 281 | $last_is_abbreviation = substr(trim($fragment), -1) == '.'; |
||
| 282 | if ($last_is_capital > 0 && $last_is_abbreviation > 0 && mb_strlen($last_word) <= 3) { |
||
| 283 | $is_abbreviation = true; |
||
| 284 | } else { |
||
| 285 | $is_abbreviation = false; |
||
| 286 | } |
||
| 287 | // merge previous fragment with this |
||
| 288 | if ($previous_is_abbreviation == true) { |
||
|
|
|||
| 289 | $current_string = $previous_string . $current_string; |
||
| 290 | } |
||
| 291 | $return_fragment[$i] = $current_string; |
||
| 292 | |||
| 293 | |||
| 294 | $previous_is_abbreviation = $is_abbreviation; |
||
| 295 | $previous_string = $current_string; |
||
| 296 | // only increment if this isn't an abbreviation |
||
| 297 | if ($is_abbreviation == false) { |
||
| 298 | $i++; |
||
| 299 | } |
||
| 300 | } |
||
| 301 | return $return_fragment; |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Merges any part starting with a closing parenthesis ')' to the previous |
||
| 306 | * part. |
||
| 307 | * @param type $parts |
||
| 308 | */ |
||
| 309 | private function parenthesesMerge($parts) |
||
| 310 | { |
||
| 311 | $subsentences = array(); |
||
| 312 | |||
| 313 | foreach ($parts as $part) { |
||
| 314 | if ($part[0] === ')') { |
||
| 315 | $subsentences[count($subsentences) - 1] .= $part; |
||
| 316 | } else { |
||
| 317 | $subsentences[] = $part; |
||
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 321 | return $subsentences; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | Looks for closing quotes to include them with the previous statement. |
||
| 326 | "That was very interesting," he said. |
||
| 327 | "That was very interesting." |
||
| 328 | */ |
||
| 329 | private function closeQuotesMerge($statements) |
||
| 330 | { |
||
| 331 | $i = 0; |
||
| 332 | $previous_statement = ""; |
||
| 333 | foreach ($statements as $statement) { |
||
| 334 | // detect end quote - if the entire string is a quotation mark, or it's [quote, space, lowercase] |
||
| 335 | if (trim($statement) == '"' || trim($statement) == "'" || |
||
| 336 | ( |
||
| 337 | ( substr($statement, 0, 1) == '"' || substr($statement, 0, 1) == "'" ) |
||
| 338 | and substr($statement, 1, 1) == " " |
||
| 339 | and ctype_lower(substr($statement, 2, 1)) == true |
||
| 340 | ) |
||
| 341 | ) { |
||
| 342 | $statement = $previous_statement . $statement; |
||
| 343 | } else { |
||
| 344 | $i++; |
||
| 345 | } |
||
| 346 | |||
| 347 | $return[$i] = $statement; |
||
| 348 | $previous_statement = $statement; |
||
| 349 | } |
||
| 350 | return($return); |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Merges items into larger sentences. |
||
| 355 | * |
||
| 356 | * Multibyte safe |
||
| 357 | * |
||
| 358 | * @param array $shorts |
||
| 359 | * @return array |
||
| 360 | */ |
||
| 361 | private function sentenceMerge($shorts) |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Return the sentences sentences detected in the provided text. |
||
| 394 | * Set the Sentence::SPLIT_TRIM flag to trim whitespace. |
||
| 395 | * @param string $text |
||
| 396 | * @param integer $flags |
||
| 397 | * @return array |
||
| 398 | */ |
||
| 399 | public function split($text, $flags = 0) |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Return the number of sentences detected in the provided text. |
||
| 431 | * @param string $text |
||
| 432 | * @return integer |
||
| 433 | */ |
||
| 434 | public function count($text) |
||
| 440 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.