|
1
|
|
|
<?php |
|
2
|
|
|
namespace Bedd\BabelNet; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Client for the BabelNet HTTP Api |
|
6
|
|
|
* |
|
7
|
|
|
* @see http://babelnet.org/guide |
|
8
|
|
|
*/ |
|
9
|
|
|
class Client |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* API Key |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
private $api_key = ''; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Base URL |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $base_url = 'https://babelnet.io/v4/'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
private $defaultParams = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Constructor |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $api_key |
|
34
|
|
|
* @param array $defaultParams |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct($api_key, array $defaultParams = []) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->api_key = $api_key; |
|
39
|
|
|
$this->defaultParams = $defaultParams; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Executes an api call |
|
44
|
|
|
* @param string $service |
|
45
|
|
|
* @param array $params |
|
46
|
|
|
* @throws \Exception |
|
47
|
|
|
* @return mixed |
|
48
|
|
|
*/ |
|
49
|
|
|
private function exec($service, $params = array()) |
|
50
|
|
|
{ |
|
51
|
|
|
//add key |
|
52
|
|
|
$params['key'] = $this->api_key; |
|
53
|
|
|
//make request |
|
54
|
|
|
$url = $this->base_url.$service.'?'.http_build_query($params); |
|
55
|
|
|
$ch = curl_init(); |
|
56
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
57
|
|
|
curl_setopt($ch, CURLOPT_POST, false); |
|
58
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
59
|
|
|
curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); |
|
60
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
|
61
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
|
62
|
|
|
$response = json_decode(curl_exec($ch), true); |
|
63
|
|
|
$info = curl_getinfo($ch); |
|
64
|
|
|
if ($info['http_code'] !== 200) { |
|
65
|
|
|
throw new \Exception($response['message'], $info['http_code']); |
|
66
|
|
|
} |
|
67
|
|
|
curl_close($ch); |
|
68
|
|
|
return $response; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns a param list from a method-reflection and his params |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $method |
|
75
|
|
|
* @param array $args |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
|
|
private function getParamsByArguments($method, $args) |
|
79
|
|
|
{ |
|
80
|
|
|
$params = []; |
|
81
|
|
|
$m = new \ReflectionMethod($method); |
|
82
|
|
|
foreach ($m->getParameters() as $param) { |
|
83
|
|
|
$value = isset($args[$param->getPosition()]) ? $args[$param->getPosition()] : null; |
|
84
|
|
|
$name = $param->getName(); |
|
|
|
|
|
|
85
|
|
|
if ($value === null && $param->isOptional()) { |
|
86
|
|
|
if ($param->isDefaultValueAvailable() && ($new_value = $param->getDefaultValue()) !== null) { |
|
87
|
|
|
$value = $new_value; |
|
88
|
|
|
} else if (isset($this->defaultParams[$name])) { |
|
89
|
|
|
$value = $this->defaultParams[$name]; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
if ($value === null) { |
|
93
|
|
|
continue; |
|
94
|
|
|
} |
|
95
|
|
|
$params[$name] = $value; |
|
96
|
|
|
} |
|
97
|
|
|
return $params; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @see http://babelnet.org/guide#RetrieveBabelNetversion |
|
102
|
|
|
* @throws \Exception |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getVersion() |
|
106
|
|
|
{ |
|
107
|
|
|
$res = $this->exec('getVersion'); |
|
108
|
|
|
return isset($res['version']) ? $res['version'] : false; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @see http://babelnet.org/guide#RetrievetheIDsoftheBabelsynsets(concepts)denotedbyagivenword |
|
113
|
|
|
* @param string $word |
|
114
|
|
|
* @param string $lang |
|
115
|
|
|
* @param string $filterLangs |
|
116
|
|
|
* @param string $pos |
|
117
|
|
|
* @param string $source |
|
118
|
|
|
* @param string $normalizer |
|
119
|
|
|
* @return string[] |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getSynsetIds($word, $lang = null, $filterLangs = null, $pos = null, $source = null, $normalizer = null) |
|
|
|
|
|
|
122
|
|
|
{ |
|
123
|
|
|
return array_column($this->exec('getSynsetIds', $this->getParamsByArguments(__METHOD__, func_get_args())), 'id'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @see http://babelnet.org/guide#Retrievetheinformationofagivensynset |
|
128
|
|
|
* @param string $id |
|
129
|
|
|
* @param string $filterLangs |
|
130
|
|
|
* @return array |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getSynsetById($id, $filterLangs = null) { |
|
|
|
|
|
|
133
|
|
|
return $this->exec('getSynset', $this->getParamsByArguments(__METHOD__, func_get_args())); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @see http://babelnet.org/guide#Retrievethesensesofagivenword |
|
138
|
|
|
* @param string $word |
|
139
|
|
|
* @param string $lang |
|
140
|
|
|
* @param string $filterLangs |
|
141
|
|
|
* @param string $pos |
|
142
|
|
|
* @param string $source |
|
143
|
|
|
* @param string $normalizer |
|
144
|
|
|
* @return array |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getSenses($word, $lang = null, $filterLangs = null, $pos = null, $source = null, $normalizer = null) |
|
|
|
|
|
|
147
|
|
|
{ |
|
148
|
|
|
return $this->exec('getSenses', $this->getParamsByArguments(__METHOD__, func_get_args())); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @see http://babelnet.org/guide#RetrieveedgesofagivenBabelNetsynset6 |
|
153
|
|
|
* @param string $id |
|
154
|
|
|
* @return array |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getEdges($id) |
|
|
|
|
|
|
157
|
|
|
{ |
|
158
|
|
|
return $this->exec('getEdges', $this->getParamsByArguments(__METHOD__, func_get_args())); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|