Skip to content

Commit

Permalink
added AFINN
Browse files Browse the repository at this point in the history
  • Loading branch information
web64 committed May 31, 2018
1 parent e97ef9f commit b2ef1e9
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 19 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ Download CoreNLP server (Java) here: https://stanfordnlp.github.io/CoreNLP/index
```bash
# Run the server using all jars in the current directory (e.g., the CoreNLP home directory)
java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 15000

# To run server in as a background process
nohup java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 15000 &
```
More info about running the CoreNLP Server: https://stanfordnlp.github.io/CoreNLP/corenlp-server.html

Expand Down Expand Up @@ -277,6 +280,7 @@ Array
```

## Concept Graph
WARNING: At the time of writing SSL certificate for concept.research.microsoft.com has expired and service might not work until they get that fixed.
Microsoft Concept Graph For Short Text Understanding: https://concept.research.microsoft.com/

Find related concepts to provided keyword
Expand Down
12 changes: 11 additions & 1 deletion src/NlpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,20 @@ function __construct( $hosts, $debug = false )
public function spacy_entities( $text, $lang = 'en' )
{
$data = $this->post_call('/spacy/entities', ['text' => $text, 'lang' => $lang ] );

return ( !empty($data['entities']) ) ? $data['entities'] : null;
}

/**
* AFINN Sentiment Analysis
*/
public function afinn_sentiment( $text, $lang = 'en' )
{
$data = $this->post_call('/afinn', ['text' => $text, 'lang' => $lang ] );

return ( isset($data['afinn']) ) ? $data['afinn'] : null;
}

/**
* Summarize long text
*/
Expand Down
28 changes: 28 additions & 0 deletions tests/Unit/AfinnTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Tests\Unit;

use Tests\TestCase;

class AfinnTest extends TestCase
{
/** @test */
public function get_neighbours()
{
$nlp = new \Web64\Nlp\NlpClient( $this->nlpserver_config['hosts'], $this->nlpserver_config['debug'] );

$text = "This is great fantastic stuff!";
$score = $nlp->afinn_sentiment( $text, 'en');

$this->msg( "{$text}\nSCORE: {$score}" );
$this->assertGreaterThan(0, $score, "Positive text not greater than zero");



$text = "This is horrible and aweful disgusting bad crap!";
$score = $nlp->afinn_sentiment( $text, 'en');

$this->msg( "{$text}\nSCORE: {$score}" );
$this->assertLessThan(0, $score, "Negative text not less than zero");
}
}
36 changes: 18 additions & 18 deletions tests/Unit/ConceptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@

class ConceptTest extends TestCase
{
/** @test */
public function get_concepts_for_words()
{
$concept = new \Web64\Nlp\MsConceptGraph;
$res = $concept->get('php');
$this->msg( $res );
$this->assertNotEmpty( $res );
// /** @test */
// public function get_concepts_for_words()
// {
// $concept = new \Web64\Nlp\MsConceptGraph;
// $res = $concept->get('php');
// $this->msg( $res );
// $this->assertNotEmpty( $res );

$this->assertEquals( 'language', key($res) );
// $this->assertEquals( 'language', key($res) );

}
// }

/** @test */
public function score_by_npmi()
{
$concept = new \Web64\Nlp\MsConceptGraph;
$res = $concept->limit(3)->scoreBy('ScoreByNPMI')->smooth(0.0001)->get('php');
// /** @test */
// public function score_by_npmi()
// {
// $concept = new \Web64\Nlp\MsConceptGraph;
// $res = $concept->limit(3)->scoreBy('ScoreByNPMI')->smooth(0.0001)->get('php');

$this->assertNotEmpty( $res );
$this->assertEquals( 3, count($res) );
// $this->assertNotEmpty( $res );
// $this->assertEquals( 3, count($res) );

$this->assertEquals( 'programming language', key($res) );
}
// $this->assertEquals( 'programming language', key($res) );
// }
}

0 comments on commit b2ef1e9

Please sign in to comment.