Skip to content

Commit d8c8802

Browse files
committed
Added support for Microsoft Concept Graph
1 parent 21c6b2b commit d8c8802

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

corenlp.json

Whitespace-only changes.

examples/concept.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
require "vendor/autoload.php";
4+
5+
$concept = new \Web64\Nlp\MsConcept;
6+
$concept->debug = true;
7+
8+
$res = $concept->get('php');
9+
print_r( $res );
10+
11+
echo "\nGet Score by NPMI:\n";
12+
$res = $concept->limit(3)->scoreBy('ScoreByNPMI')->smooth(0.0001)->get('php');
13+
print_r( $res );

src/MsConcept.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Web64\Nlp;
4+
5+
/**
6+
* Microsoft Concept Graph For Short Text Understanding
7+
* - Documentation: https://concept.research.microsoft.com/
8+
* - Example: https://concept.research.microsoft.com/api/Concept/ScoreByProb?instance=catalonia&topK=10&smooth=1
9+
*/
10+
class MsConcept
11+
{
12+
private $api_url = "https://concept.research.microsoft.com/api/Concept/";
13+
private $limit = 10;
14+
private $smooth = 0.0004;
15+
private $score_by = 'ScoreByProb';
16+
public $debug = false;
17+
18+
public function limit( $limit )
19+
{
20+
$this->limit = (int)$limit;
21+
22+
return $this;
23+
}
24+
25+
public function scoreBy( $score_by )
26+
{
27+
$this->score_by = $score_by;
28+
return $this;
29+
}
30+
31+
public function smooth( $smooth )
32+
{
33+
$this->smooth = $smooth;
34+
return $this;
35+
}
36+
37+
// , $num_results = 10, $score_method = 'ScoreByProb', $smooth = 0.0001
38+
public function get( $word )
39+
{
40+
$url = $this->api_url . $this->score_by . "?instance=" . urlencode($word) . "&topK=". $this->limit . "&smooth=". $this->smooth;
41+
42+
return $this->call( $url );
43+
}
44+
45+
public function call( $url )
46+
{
47+
if ( $this->debug ) echo "URL: $url \n";
48+
49+
$context_params = array(
50+
'http' => array(
51+
'method' => 'GET',
52+
'header' => "Content-Type: application/json\r\n",
53+
)
54+
);
55+
56+
$context = stream_context_create( $context_params );
57+
$response = file_get_contents($url, FALSE, $context);
58+
return json_decode($response, 1);
59+
}
60+
}

tests/Unit/ConceptTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use Tests\TestCase;
6+
7+
class ConceptTest extends TestCase
8+
{
9+
/** @test */
10+
public function get_concepts_for_words()
11+
{
12+
$concept = new \Web64\Nlp\MsConcept;
13+
$res = $concept->get('php');
14+
15+
$this->assertNotEmpty( $res );
16+
17+
$this->assertEquals( 'language', key($res) );
18+
19+
}
20+
21+
/** @test */
22+
public function score_by_npmi()
23+
{
24+
$concept = new \Web64\Nlp\MsConcept;
25+
$res = $concept->limit(3)->scoreBy('ScoreByNPMI')->smooth(0.0001)->get('php');
26+
27+
$this->assertNotEmpty( $res );
28+
$this->assertEquals( 3, count($res) );
29+
30+
$this->assertEquals( 'programming language', key($res) );
31+
}
32+
}

0 commit comments

Comments
 (0)