Skip to content

Commit b81dc26

Browse files
authored
Prevent infinite loops when encountering numbers.
1 parent 7cfd4bf commit b81dc26

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

src/Procedures/SentiText.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
class SentiText
1010
{
11-
11+
1212
private $text = "";
1313
public $words_and_emoticons = null;
1414
public $is_cap_diff = null;
1515

1616
const PUNC_LIST = [".", "!", "?", ",", ";", ":", "-", "'", "\"",
1717
"!!", "!!!", "??", "???", "?!?", "!?!", "?!?!", "!?!?"];
18-
19-
18+
19+
2020
function __construct($text)
2121
{
2222
//checking that is string
@@ -29,7 +29,7 @@ function __construct($text)
2929
// adjacent punctuation (keeps emoticons & contractions)
3030
$this->is_cap_diff = $this->allcap_differential($this->words_and_emoticons);
3131
}
32-
32+
3333
/*
3434
Remove all punctation from a string
3535
*/
@@ -38,16 +38,16 @@ function strip_punctuation($string)
3838
//$string = strtolower($string);
3939
return preg_replace("/[[:punct:]]+/", "", $string);
4040
}
41-
41+
4242
function array_count_values_of($haystack, $needle)
4343
{
44-
if (!in_array($needle, $haystack)) {
44+
if (!in_array($needle, $haystack, true)) {
4545
return 0;
4646
}
4747
$counts = array_count_values($haystack);
4848
return $counts[$needle];
4949
}
50-
50+
5151
/*
5252
Check whether just some words in the input are ALL CAPS
5353
@@ -71,7 +71,7 @@ private function allcap_differential($words)
7171
}
7272
return $is_different;
7373
}
74-
74+
7575
function _words_only()
7676
{
7777
$text_mod = $this->strip_punctuation($this->text);
@@ -86,26 +86,26 @@ function _words_only()
8686

8787
function _words_and_emoticons()
8888
{
89-
89+
9090
$wes = preg_split('/\s+/', $this->text);
91-
91+
9292
# get rid of residual empty items or single letter words
9393
$wes = array_filter($wes, function ($word) {
9494
return strlen($word) > 1;
9595
});
9696
//Need to remap the indexes of the array
9797
$wes = array_values($wes);
9898
$words_only = $this->_words_only();
99-
99+
100100
foreach ($words_only as $word) {
101101
foreach (self::PUNC_LIST as $punct) {
102102
//replace all punct + word combinations with word
103103
$pword = $punct .$word;
104-
105-
104+
105+
106106
$x1 = $this->array_count_values_of($wes, $pword);
107107
while ($x1 > 0) {
108-
$i = array_search($pword, $wes);
108+
$i = array_search($pword, $wes, true);
109109
unset($wes[$i]);
110110
array_splice($wes, $i, 0, $word);
111111
$x1 = $this->array_count_values_of($wes, $pword);
@@ -114,7 +114,7 @@ function _words_and_emoticons()
114114
$wordp = $word . $punct;
115115
$x2 = $this->array_count_values_of($wes, $wordp);
116116
while ($x2 > 0) {
117-
$i = array_search($wordp, $wes);
117+
$i = array_search($wordp, $wes, true);
118118
unset($wes[$i]);
119119
array_splice($wes, $i, 0, $word);
120120
$x2 = $this->array_count_values_of($wes, $wordp);

0 commit comments

Comments
 (0)