-
Notifications
You must be signed in to change notification settings - Fork 10
/
example.php
114 lines (79 loc) · 3.58 KB
/
example.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
require_once 'TextRazor.php';
TextRazorSettings::setApiKey('YOUR_API_KEY_HERE');
function testAccount()
{
$accountManager = new AccountManager();
print_r($accountManager->getAccount());
}
function testAnalysis()
{
$textrazor = new TextRazor();
$textrazor->addExtractor('entities');
$textrazor->addExtractor('words');
$textrazor->addExtractor('spelling');
$text = 'LONDON - Barclays misled shareholders and the public about one of the biggest investments in the banks history, a BBC Panorama investigation has found.';
$response = $textrazor->analyze($text);
if (isset($response['response']['entities'])) {
foreach ($response['response']['entities'] as $entity) {
print('Entity ID: ' . $entity['entityId']);
print(PHP_EOL);
}
}
}
function testClassifier()
{
$textrazorClassifier = new ClassifierManager();
$classifierId = 'test_cats_php';
try {
print_r($textrazorClassifier->deleteClassifier($classifierId));
} catch (Exception $e) {
// Silently ignore missing classifier for now.
}
// Define some new categories and upload them as a new classifier.
$newCategories = [];
array_push($newCategories, ['categoryId' => '1', 'query' => 'concept("banking")']);
array_push($newCategories, ['categoryId' => '2', 'query' => 'concept("health")']);
$textrazorClassifier->createClassifier($classifierId, $newCategories);
// Test the new classifier out with an analysis request.
$textrazor = new TextRazor();
$textrazor->addClassifier($classifierId);
$text = 'Barclays misled shareholders and the public about one of the biggest investments in the banks history, a BBC Panorama investigation has found.';
$response = $textrazor->analyze($text);
print_r($response['response']);
// The client offers various methods for manipulating your stored classifier.
print_r($textrazorClassifier->allCategories($classifierId));
print_r($textrazorClassifier->deleteClassifier($classifierId));
}
function testEntityDictionary()
{
$textrazorDictionary = new DictionaryManager();
$dictionaryId = 'test_ents_php';
try {
print_r($textrazorDictionary->deleteDictionary($dictionaryId));
} catch (Exception $e) {
// Silently ignore missing dictionary for now.
}
// Define a new dictionary, then add some test entries
print_r($textrazorDictionary->createDictionary($dictionaryId, 'STEM', true, 'eng'));
$newEntities = [];
array_push($newEntities, ['id' => 'TV_1', 'text' => 'BBC Panorama']);
print_r($textrazorDictionary->addEntries($dictionaryId, $newEntities));
// To use the new dictionary, simply add its ID to your analysis request.
$textrazor = new TextRazor();
$textrazor->addEntityDictionary($dictionaryId);
$text = 'Barclays misled shareholders and the public about one of the biggest investments in the banks history, a BBC Panorama investigation has found.';
$response = $textrazor->analyze($text);
// The matched entities will be available in the response
print_r($response['response']['entities']);
// The client offers various methods for manipulating your stored dictionary entries.
print_r($textrazorDictionary->getEntry($dictionaryId, 'TV_1'));
print_r($textrazorDictionary->allEntries($dictionaryId, 10));
print_r($textrazorDictionary->getDictionary($dictionaryId));
print_r($textrazorDictionary->allDictionaries());
print_r($textrazorDictionary->deleteDictionary($dictionaryId));
}
testAccount();
testAnalysis();
testEntityDictionary();
testClassifier();