-
Notifications
You must be signed in to change notification settings - Fork 1
/
HarvestAlchemyApi.php
37 lines (26 loc) · 1018 Bytes
/
HarvestAlchemyApi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
require_once 'vendor/autoload.php';
$text = file_get_contents($argv[1]);
$chunkSize = (isset($argv[2]) && $argv[2] > 0)
? intval($argv[2]) : -1;
if (!$text) {
die('no text');
}
$api = new AlchemyAPI();
$dir = __DIR__ . '/alchemyapi-out/';
if (!file_exists($dir)) {
mkdir($dir);
}
$chunks = $chunkSize > 0
? \DimeExtraction\Chunker::getChunks($text, $chunkSize) : [$text];
foreach ($chunks as $i => $text) {
$suffix = $i . '-' . basename($argv[1]);
$ent = $api->entities('text', $text, ['maxRetrieve' => 1000]);
file_put_contents($dir . 'entities-' . $suffix, serialize($ent));
$key = $api->keywords('text', $text, ['maxRetrieve' => 1000]);
file_put_contents($dir . 'keywords-' . $suffix, serialize($key));
$con = $api->concepts('text', $text, ['maxRetrieve' => 1000]);
file_put_contents($dir . 'concepts-' . $suffix, serialize($con));
$sent = $api->sentiment('text', $text, []);
file_put_contents($dir . 'sentiment-' . $suffix, serialize($sent));
}