Skip to content

Commit 13dc120

Browse files
authored
Merge pull request #4 from heathdutton/master
Prevent exceptions when different cased idioms / Fix infinite loops when encountering numbers.
2 parents 7cfd4bf + e7e238c commit 13dc120

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

src/Analyzer.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ public function getSentiment($text)
170170

171171
$text_no_emoji = '';
172172
$prev_space = true;
173-
174-
foreach($this->str_split_unicode($text) as $unichr ) {
173+
174+
foreach($this->str_split_unicode($text) as $unichr ) {
175175
if (array_key_exists($unichr, $this->emojis)) {
176176
$description = $this->emojis[$unichr];
177177
if (!($prev_space)) {
@@ -384,8 +384,9 @@ public function _idioms_check($wordInContext, $valence)
384384
$sequences = [$onezero, $twoonezero, $twoone, $threetwoone, $threetwo];
385385

386386
foreach ($sequences as $seq) {
387-
if (array_key_exists(strtolower($seq), Config::SPECIAL_CASE_IDIOMS)) {
388-
$valence = Config::SPECIAL_CASE_IDIOMS[$seq];
387+
$key = strtolower($seq);
388+
if (array_key_exists($key, Config::SPECIAL_CASE_IDIOMS)) {
389+
$valence = Config::SPECIAL_CASE_IDIOMS[$key];
389390
break;
390391
}
391392

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)