8
8
9
9
class SentiText
10
10
{
11
-
11
+
12
12
private $ text = "" ;
13
13
public $ words_and_emoticons = null ;
14
14
public $ is_cap_diff = null ;
15
15
16
16
const PUNC_LIST = [". " , "! " , "? " , ", " , "; " , ": " , "- " , "' " , "\"" ,
17
17
"!! " , "!!! " , "?? " , "??? " , "?!? " , "!?! " , "?!?! " , "!?!? " ];
18
-
19
-
18
+
19
+
20
20
function __construct ($ text )
21
21
{
22
22
//checking that is string
@@ -29,7 +29,7 @@ function __construct($text)
29
29
// adjacent punctuation (keeps emoticons & contractions)
30
30
$ this ->is_cap_diff = $ this ->allcap_differential ($ this ->words_and_emoticons );
31
31
}
32
-
32
+
33
33
/*
34
34
Remove all punctation from a string
35
35
*/
@@ -38,16 +38,16 @@ function strip_punctuation($string)
38
38
//$string = strtolower($string);
39
39
return preg_replace ("/[[:punct:]]+/ " , "" , $ string );
40
40
}
41
-
41
+
42
42
function array_count_values_of ($ haystack , $ needle )
43
43
{
44
- if (!in_array ($ needle , $ haystack )) {
44
+ if (!in_array ($ needle , $ haystack, true )) {
45
45
return 0 ;
46
46
}
47
47
$ counts = array_count_values ($ haystack );
48
48
return $ counts [$ needle ];
49
49
}
50
-
50
+
51
51
/*
52
52
Check whether just some words in the input are ALL CAPS
53
53
@@ -71,7 +71,7 @@ private function allcap_differential($words)
71
71
}
72
72
return $ is_different ;
73
73
}
74
-
74
+
75
75
function _words_only ()
76
76
{
77
77
$ text_mod = $ this ->strip_punctuation ($ this ->text );
@@ -86,26 +86,26 @@ function _words_only()
86
86
87
87
function _words_and_emoticons ()
88
88
{
89
-
89
+
90
90
$ wes = preg_split ('/\s+/ ' , $ this ->text );
91
-
91
+
92
92
# get rid of residual empty items or single letter words
93
93
$ wes = array_filter ($ wes , function ($ word ) {
94
94
return strlen ($ word ) > 1 ;
95
95
});
96
96
//Need to remap the indexes of the array
97
97
$ wes = array_values ($ wes );
98
98
$ words_only = $ this ->_words_only ();
99
-
99
+
100
100
foreach ($ words_only as $ word ) {
101
101
foreach (self ::PUNC_LIST as $ punct ) {
102
102
//replace all punct + word combinations with word
103
103
$ pword = $ punct .$ word ;
104
-
105
-
104
+
105
+
106
106
$ x1 = $ this ->array_count_values_of ($ wes , $ pword );
107
107
while ($ x1 > 0 ) {
108
- $ i = array_search ($ pword , $ wes );
108
+ $ i = array_search ($ pword , $ wes, true );
109
109
unset($ wes [$ i ]);
110
110
array_splice ($ wes , $ i , 0 , $ word );
111
111
$ x1 = $ this ->array_count_values_of ($ wes , $ pword );
@@ -114,7 +114,7 @@ function _words_and_emoticons()
114
114
$ wordp = $ word . $ punct ;
115
115
$ x2 = $ this ->array_count_values_of ($ wes , $ wordp );
116
116
while ($ x2 > 0 ) {
117
- $ i = array_search ($ wordp , $ wes );
117
+ $ i = array_search ($ wordp , $ wes, true );
118
118
unset($ wes [$ i ]);
119
119
array_splice ($ wes , $ i , 0 , $ word );
120
120
$ x2 = $ this ->array_count_values_of ($ wes , $ wordp );
0 commit comments