DatumboxAPI::SubjectivityAnalysis()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 10
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Integrations\Connectors\Twitter;
4
5
use Log;
6
use App\Models\User;
7
8
class DatumboxAPI
9
{
10
    const version='1.0';
11
    
12
    protected $api_key;
13
    
14
    /**
15
     * Constructor
16
     * 
17
     * @param  string $api_key
18
     * @return DatumboxAPI
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
19
     */
20
    public function __construct($api_key)
21
    {
22
        $this->api_key=$api_key;
23
    }
24
    
25
    /**
26
     * Calls the Web Service of Datumbox
27
     * 
28
     * @param string $api_method
29
     * @param array  $POSTparameters
30
     * 
31
     * @return string $jsonreply
32
     */
33
    protected function CallWebService($api_method,$POSTparameters)
34
    {
35
        $POSTparameters['api_key']=$this->api_key;
36
        
37
        $ch = curl_init();
38
        curl_setopt($ch, CURLOPT_URL, 'http://api.datumbox.com/'.self::version.'/'.$api_method.'.json');
39
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
40
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
41
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
42
        curl_setopt($ch, CURLOPT_POST, true);
43
        curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTparameters);
44
45
        $jsonreply = curl_exec($ch);
46
        curl_close($ch);
47
        unset($ch);
48
49
        return $jsonreply;
50
    }
51
    
52
    /**
53
     * Parses the API Reply
54
     * 
55
     * @param mixed $jsonreply
56
     * 
57
     * @return mixed
58
     */
59
    protected function ParseReply($jsonreply)
60
    {
61
        $jsonreply=json_decode($jsonreply, true);
62
        
63
        if(isset($jsonreply['output']['status']) && $jsonreply['output']['status']==1) {
64
            return $jsonreply['output']['result'];
65
        }
66
        
67
        if(isset($jsonreply['error']['ErrorCode']) && isset($jsonreply['error']['ErrorMessage'])) {
68
            echo $jsonreply['error']['ErrorMessage'].' (ErrorCode: '.$jsonreply['error']['ErrorCode'].')';
69
        }
70
        
71
        return false;
72
    }
73
    
74
    /**
75
     * Performs Sentiment Analysis.
76
     * 
77
     * @param string $text The clear text (no HTML tags) that we evaluate.
78
     * 
79
     * @return string|false It returns "positive", "negative" or "neutral" on success and false on fail.
80
     */
81 View Code Duplication
    public function SentimentAnalysis($text)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
    {
83
        $parameters=array(
84
            'text'=>$text,
85
        );
86
        
87
        $jsonreply=$this->CallWebService('SentimentAnalysis', $parameters);
88
        
89
        return $this->ParseReply($jsonreply);
90
    }
91
    
92
    /**
93
     * Performs Sentiment Analysis on Twitter.
94
     * 
95
     * @param string $text The text of the tweet that we evaluate.
96
     * 
97
     * @return string|false It returns "positive", "negative" or "neutral" on success and false on fail.
98
     */
99 View Code Duplication
    public function TwitterSentimentAnalysis($text)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        $parameters=array(
102
            'text'=>$text,
103
        );
104
        
105
        $jsonreply=$this->CallWebService('TwitterSentimentAnalysis', $parameters);
106
        
107
        return $this->ParseReply($jsonreply);
108
    }
109
    
110
    /**
111
     * Performs Subjectivity Analysis.
112
     * 
113
     * @param string $text The clear text (no HTML tags) that we evaluate.
114
     * 
115
     * @return string|false. It returns "objective" or "subjective" on success and false on fail.
0 ignored issues
show
Documentation introduced by
The doc-type string|false. could not be parsed: Unknown type name "false." at position 7. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
116
     */
117 View Code Duplication
    public function SubjectivityAnalysis($text)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
    {
119
        $parameters=array(
120
            'text'=>$text,
121
        );
122
        
123
        $jsonreply=$this->CallWebService('SubjectivityAnalysis', $parameters);
124
        
125
        return $this->ParseReply($jsonreply);
126
    }
127
    
128
    /**
129
     * Performs Topic Classification. 
130
     * 
131
     * @param string $text The clear text (no HTML tags) that we evaluate.
132
     * 
133
     * @return string|false. It returns "Arts", "Business & Economy", "Computers & Technology", "Health", "Home & Domestic Life", "News", "Recreation & Activities", "Reference & Education", "Science", "Shopping", "Society", "Sports" on success and false on fail.
0 ignored issues
show
Documentation introduced by
The doc-type string|false. could not be parsed: Unknown type name "false." at position 7. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
134
     */
135 View Code Duplication
    public function TopicClassification($text)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
    {
137
        $parameters=array(
138
            'text'=>$text,
139
        );
140
        
141
        $jsonreply=$this->CallWebService('TopicClassification', $parameters);
142
        
143
        return $this->ParseReply($jsonreply);
144
    }
145
    
146
    /**
147
     * Performs Spam Detection.
148
     * 
149
     * @param string $text The clear text (no HTML tags) that we evaluate.
150
     * 
151
     * @return string|false It returns "spam" or "nospam" on success and false on fail.
152
     */
153 View Code Duplication
    public function SpamDetection($text)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
    {
155
        $parameters=array(
156
            'text'=>$text,
157
        );
158
        
159
        $jsonreply=$this->CallWebService('SpamDetection', $parameters);
160
        
161
        return $this->ParseReply($jsonreply);
162
    }
163
    
164
    /**
165
     * Performs Adult Content Detection. 
166
     * 
167
     * @param string $text The clear text (no HTML tags) that we evaluate.
168
     * 
169
     * @return string|false It returns "adult" or "noadult" on success and false on fail.
170
     */
171 View Code Duplication
    public function AdultContentDetection($text)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
172
    {
173
        $parameters=array(
174
            'text'=>$text,
175
        );
176
        
177
        $jsonreply=$this->CallWebService('AdultContentDetection', $parameters);
178
        
179
        return $this->ParseReply($jsonreply);
180
    }
181
    
182
    /**
183
     * Performs Readability Assessment. 
184
     * 
185
     * @param string $text The clear text (no HTML tags) that we evaluate.
186
     * 
187
     * @return string|false It returns "basic", "intermediate" or "advanced" on success and false on fail.
188
     */
189 View Code Duplication
    public function ReadabilityAssessment($text)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
190
    {
191
        $parameters=array(
192
            'text'=>$text,
193
        );
194
        
195
        $jsonreply=$this->CallWebService('ReadabilityAssessment', $parameters);
196
        
197
        return $this->ParseReply($jsonreply);
198
    }
199
    
200
    /**
201
     * Performs Language Detection. 
202
     * 
203
     * @param string $text The clear text (no HTML tags) that we evaluate.
204
     * 
205
     * @return string|false It returns the ISO639-1 two-letter language code (http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) on success and false on fail.
206
     */
207 View Code Duplication
    public function LanguageDetection($text)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
208
    {
209
        $parameters=array(
210
            'text'=>$text,
211
        );
212
        
213
        $jsonreply=$this->CallWebService('LanguageDetection', $parameters);
214
        
215
        return $this->ParseReply($jsonreply);
216
    }
217
    
218
    /**
219
     * Performs Commercial Detection. 
220
     * 
221
     * @param string $text The clear text (no HTML tags) that we evaluate.
222
     * 
223
     * @return string|false It returns "commercial" or "noncommercial" on success and false on fail.
224
     */
225 View Code Duplication
    public function CommercialDetection($text)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
226
    {
227
        $parameters=array(
228
            'text'=>$text,
229
        );
230
        
231
        $jsonreply=$this->CallWebService('CommercialDetection', $parameters);
232
        
233
        return $this->ParseReply($jsonreply);
234
    }
235
    
236
    /**
237
     * Performs Educational Detection. 
238
     * 
239
     * @param string $text The clear text (no HTML tags) that we evaluate.
240
     * 
241
     * @return string|false It returns "educational" or "noneducational" on success and false on fail.
242
     */
243 View Code Duplication
    public function EducationalDetection($text)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
244
    {
245
        $parameters=array(
246
            'text'=>$text,
247
        );
248
        
249
        $jsonreply=$this->CallWebService('EducationalDetection', $parameters);
250
        
251
        return $this->ParseReply($jsonreply);
252
    }
253
    
254
    /**
255
     * Performs Gender Detection. 
256
     * 
257
     * @param string $text The clear text (no HTML tags) that we evaluate.
258
     * 
259
     * @return string|false It returns "male" or "female" on success and false on fail.
260
     */
261 View Code Duplication
    public function GenderDetection($text)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
262
    {
263
        $parameters=array(
264
            'text'=>$text,
265
        );
266
        
267
        $jsonreply=$this->CallWebService('GenderDetection', $parameters);
268
        
269
        return $this->ParseReply($jsonreply);
270
    }
271
    
272
    /**
273
     * Performs Text Extraction. It extracts the important information (clear text) from a given webpage.
274
     * 
275
     * @param string $text The HTML of the webpage.
276
     * 
277
     * @return string|false It returns the clear text of the document on success and false on fail.
278
     */
279 View Code Duplication
    public function TextExtraction($text)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
280
    {
281
        $parameters=array(
282
            'text'=>$text,
283
        );
284
        
285
        $jsonreply=$this->CallWebService('TextExtraction', $parameters);
286
        
287
        return $this->ParseReply($jsonreply);
288
    }
289
    
290
    /**
291
     * Performs Keyword Extraction. It extracts the keywords and keywords combinations from a text.
292
     * 
293
     * @param string  $text The clear text (no HTML tags) that we analyze.
294
     * @param integer $n    It is a number from 1 to 5 which denotes the number of Keyword combinations that we want to get.
295
     * 
296
     * @return array|false It returns an array with the keywords of the document on success and false on fail.
297
     */
298
    public function KeywordExtraction($text,$n)
299
    {
300
        $parameters=array(
301
            'text'=>$text,
302
            'n'=>$n,
303
        );
304
        
305
        $jsonreply=$this->CallWebService('KeywordExtraction', $parameters);
306
        
307
        return $this->ParseReply($jsonreply);
308
    }
309
    
310
    /**
311
     * Evaluates the Document Similarity between 2 documents.
312
     * 
313
     * @param string $original The first clear text (no HTML tags) that we compare.
314
     * @param string $copy     The second clear text (no HTML tags) that we compare.
315
     * 
316
     * @return array|false It returns an array with similarity metrics for the two documents on success and false on fail.
317
     */
318
    public function DocumentSimilarity($original,$copy)
319
    {
320
        $parameters=array(
321
            'original'=>$original,
322
            'copy'=>$copy,
323
        );
324
        
325
        $jsonreply=$this->CallWebService('DocumentSimilarity', $parameters);
326
        
327
        return $this->ParseReply($jsonreply);
328
    }
329
    
330
    
331
}
332
333