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 |
|
|
|
|
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) |
|
|
|
|
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) |
|
|
|
|
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. |
|
|
|
|
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function SubjectivityAnalysis($text) |
|
|
|
|
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. |
|
|
|
|
134
|
|
|
*/ |
135
|
|
View Code Duplication |
public function TopicClassification($text) |
|
|
|
|
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) |
|
|
|
|
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) |
|
|
|
|
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) |
|
|
|
|
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) |
|
|
|
|
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) |
|
|
|
|
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) |
|
|
|
|
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) |
|
|
|
|
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) |
|
|
|
|
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
|
|
|
|
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.