3 * ARC2 CrunchBase API JSON Parser
5 * @author Benjamin Nowack <bnowack@semsol.com>
6 * @license http://arc.semsol.org/license
7 * @homepage <http://arc.semsol.org/>
12 ARC2::inc('JSONParser');
14 class ARC2_CBJSONParser extends ARC2_JSONParser {
16 function __construct($a = '', &$caller) {
17 parent::__construct($a, $caller);
20 function ARC2_CBJSONParser($a = '', &$caller) {
21 $this->__construct($a, $caller);
24 function __init() {/* reader */
26 $this->base = 'http://cb.semsol.org/';
27 $this->rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
28 $this->default_ns = $this->base . 'ns#';
29 $this->nsp = array($this->rdf => 'rdf');
38 function extractRDF() {
39 $struct = $this->struct;
40 if ($type = $this->getStructType($struct)) {
41 $s = $this->getResourceID($struct, $type);
43 $this->addT($s, $this->rdf . 'type', $this->default_ns . $this->camelCase($type), 'uri', 'uri');
44 /* explicit triples */
45 $this->extractResourceRDF($struct, $s);
49 function getStructType($struct, $rel = '') {
51 if ($url = $this->v('crunchbase_url', '', $struct)) {
52 return preg_replace('/^.*crunchbase\.com\/([^\/]+)\/.*$/', '\\1', $url);
55 if ($rel == 'person') return 'person';
56 if ($rel == 'company') return 'company';
57 if ($rel == 'acquiring_company') return 'company';
58 if ($rel == 'firm') return 'company';
59 if ($rel == 'provider') return 'service-provider';
61 if (isset($struct['_type'])) return $struct['_type'];
62 if (isset($struct['round_code'])) return 'funding_round';
63 if (isset($struct['products'])) return 'company';
64 if (isset($struct['first_name'])) return 'person';
65 if (isset($struct['investments'])) return 'financial-organization';
66 if (isset($struct['launched_year'])) return 'product';
67 if (isset($struct['providerships']) && is_array($struct['providerships'])) return 'service-provider';
71 function getResourceID($struct, $type) {
72 if ($type && isset($struct['permalink'])) {
73 return $this->base . $type . '/' . $struct['permalink'] . '#self';
75 return $this->createBnodeID();
78 function getPropertyURI($name, $ns = '') {
79 if (!$ns) $ns = $this->default_ns;
80 if (preg_match('/^(product|funding_round|investment|acquisition|.+ship|office|milestone|.+embed|.+link|degree|fund)s/', $name, $m)) $name = $m[1];
81 if ($name == 'tag_list') $name = 'tag';
82 if ($name == 'competitions') $name = 'competitor';
86 function createSubURI($s, $k, $pos) {
87 $s = str_replace('#self', '/', $s);
88 if (preg_match('/(office|ship|investment|milestone|fund|embed|link)s$/', $k)) $k = substr($k, 0, -1);
89 return $s . $k . '-' . ($pos + 1) . '#self';
94 function extractResourceRDF($struct, $s, $pos = 0) {
95 $s_type = preg_match('/^\_\:/', $s) ? 'bnode' : 'uri';
96 $date_prefixes = array();
97 foreach ($struct as $k => $v) {
98 if ($k == 'acquisition') $k = 'exit';
99 if (preg_match('/^(.*)\_(year|month|day)$/', $k, $m)) {
100 if (!in_array($m[1], $date_prefixes)) $date_prefixes[] = $m[1];
102 $sub_m = 'extract' . $this->camelCase($k) . 'RDF';
103 if (method_exists($this, $sub_m)) {
104 $this->$sub_m($s, $s_type, $v);
107 $p = $this->getPropertyURI($k);
109 /* simple, single v */
111 $o_type = preg_match('/^[a-z]+\:[^\s]+$/is', $v) ? 'uri' : 'literal';
113 if (preg_match('/^https?\:\/\/[^\/]+$/', $v)) $v .= '/';
114 $this->addT($s, $p, $v, $s_type, $o_type);
116 if ($k == 'name') $this->addT($s, 'http://www.w3.org/2000/01/rdf-schema#label', $v, $s_type, $o_type);
118 //if ($k == 'permalink') $this->addT($s, 'http://purl.org/dc/elements/1.1/identifier', $v, $s_type, $o_type);
120 /* structured, single v */
121 elseif (!$this->isFlatArray($v)) {
122 if ($o_type = $this->getStructType($v, $k)) {/* known type */
123 $o = $this->getResourceID($v, $o_type);
124 $this->addT($s, $p, $o, $s_type, 'uri');
125 $this->addT($o, $this->rdf . 'type', $this->default_ns . $this->camelCase($o_type), 'uri', 'uri');
127 else {/* unknown type */
128 $o = $this->createSubURI($s, $k, $pos);
129 $this->addT($s, $p, $o, $s_type, 'uri');
130 $this->extractResourceRDF($v, $o);
135 foreach ($v as $sub_pos => $sub_v) {
136 $this->extractResourceRDF(array($k => $sub_v), $s, $sub_pos);
140 /* infer XSD triples */
141 foreach ($date_prefixes as $prefix) {
142 $this->inferDate($prefix, $s, $struct);
146 function isFlatArray($v) {
147 foreach ($v as $k => $sub_v) {
148 return is_numeric($k) ? 1 : 0;
154 function extractTagListRDF($s, $s_type, $v) {
156 $tags = split(', ', $v);
157 foreach ($tags as $tag) {
158 if (!trim($tag)) continue;
159 $this->addT($s, $this->getPropertyURI('tag'), $tag, $s_type, 'literal');
163 function extractImageRDF($s, $s_type, $v, $rel = 'image') {
165 $sizes = $v['available_sizes'];
166 foreach ($sizes as $size) {
169 $img = 'http://www.crunchbase.com/' . $size[1];
170 $this->addT($s, $this->getPropertyURI($rel), $img, $s_type, 'uri');
171 $this->addT($img, $this->getPropertyURI('width'), $w, 'uri', 'literal');
172 $this->addT($img, $this->getPropertyURI('height'), $h, 'uri', 'literal');
176 function extractScreenshotsRDF($s, $s_type, $v) {
178 foreach ($v as $sub_v) {
179 $this->extractImageRDF($s, $s_type, $sub_v, 'screenshot');
183 function extractProductsRDF($s, $s_type, $v) {
184 foreach ($v as $sub_v) {
185 $o = $this->getResourceID($sub_v, 'product');
186 $this->addT($s, $this->getPropertyURI('product'), $o, $s_type, 'uri');
190 function extractCompetitionsRDF($s, $s_type, $v) {
191 foreach ($v as $sub_v) {
192 $o = $this->getResourceID($sub_v['competitor'], 'company');
193 $this->addT($s, $this->getPropertyURI('competitor'), $o, $s_type, 'uri');
197 function extractFundingRoundsRDF($s, $s_type, $v) {
198 foreach ($v as $pos => $sub_v) {
199 $o = $this->createSubURI($s, 'funding_round', $pos);
200 $this->addT($s, $this->getPropertyURI('funding_round'), $o, $s_type, 'uri');
201 $this->extractResourceRDF($sub_v, $o, $pos);
205 function extractInvestmentsRDF($s, $s_type, $v) {
206 foreach ($v as $pos => $sub_v) {
208 foreach (array('person' => 'person', 'company' => 'company', 'financial_org' => 'financial-organization') as $k => $type) {
209 if (isset($sub_v[$k])) $this->addT($s, $this->getPropertyURI('investment'), $this->getResourceID($sub_v[$k], $type), $s_type, 'uri');
212 if (isset($sub_v['funding_round'])) {
213 $o = $this->createSubURI($s, 'investment', $pos);
214 $this->addT($s, $this->getPropertyURI('investment'), $o, $s_type, 'uri');
215 $this->extractResourceRDF($sub_v['funding_round'], $o, $pos);
220 function extractExternalLinksRDF($s, $s_type, $v) {
221 foreach ($v as $sub_v) {
222 $href = $sub_v['external_url'];
223 if (preg_match('/^https?\:\/\/[^\/]+$/', $href)) $href .= '/';
224 $this->addT($s, $this->getPropertyURI('external_link'), $href, $s_type, 'uri');
225 $this->addT($href, $this->getPropertyURI('title'), $sub_v['title'], $s_type, 'literal');
229 function extractWebPresencesRDF($s, $s_type, $v) {
230 foreach ($v as $sub_v) {
231 $href = $sub_v['external_url'];
232 if (preg_match('/^https?\:\/\/[^\/]+$/', $href)) $href .= '/';
233 $this->addT($s, $this->getPropertyURI('web_presence'), $href, $s_type, 'uri');
234 $this->addT($href, $this->getPropertyURI('title'), $sub_v['title'], $s_type, 'literal');
238 function extractCreatedAtRDF($s, $s_type, $v) {
239 $v = $this->getAPIDateXSD($v);
240 $this->addT($s, $this->getPropertyURI('created_at'), $v, $s_type, 'literal');
243 function extractUpdatedAtRDF($s, $s_type, $v) {
244 $v = $this->getAPIDateXSD($v);
245 $this->addT($s, $this->getPropertyURI('updated_at'), $v, $s_type, 'literal');
248 function getAPIDateXSD($val) {
249 //Fri Jan 16 21:11:48 UTC 2009
250 if (preg_match('/^[a-z]+ ([a-z]+) ([0-9]+) ([0-9]{2}\:[0-9]{2}\:[0-9]{2}) UTC ([0-9]{4})/i', $val, $m)) {
251 $months = array('Jan' => '01', 'Feb' => '02', 'Mar' =>'03', 'Apr' => '04', 'May' => '05', 'Jun' => '06', 'Jul' => '07', 'Aug' => '08', 'Sep' => '09', 'Oct' => '10', 'Nov' => '11', 'Dec' => '12');
252 return $m[4] . '-' . $months[$m[1]] . '-' . $m[2] . 'T' . $m[3] . 'Z';
259 function inferDate($prefix, $s, $struct) {
260 $s_type = preg_match('/^\_\:/', $s) ? 'bnode' : 'uri';
262 foreach (array('year', 'month', 'day') as $suffix) {
263 $val = $this->v1($prefix . '_' . $suffix, '00', $struct);
264 $r .= ($r ? '-' : '') . str_pad($val, 2, '0', STR_PAD_LEFT);
266 if ($r != '00-00-00') {
267 $this->addT($s, $this->getPropertyURI($prefix . '_date'), $r, $s_type, 'literal');