PHP code example of aflorea4 / laravel-nlp

1. Go to this page and download the library: Download aflorea4/laravel-nlp library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aflorea4 / laravel-nlp example snippets


use Web64\LaravelNlp\Facades\NLP;

// Get Article content and metadata
$article = NLP::article($url);

// Detect language
$lang = NLP::language("What language is this?");

// Entity Extraction
$entities = NLP::entities( $text, 'en' );

// Sentiment analysis
$sentiment = NLP::sentiment( $text, 'en' );

// Translage text to Portuguese
$translated_text = NLP::translate($text, null, 'pt');

use Web64\LaravelNlp\Facades\NLP;

$lang = NLP::language("What language is this?");
// 'en'

$article = NLP::article("https://medium.com/@taylorotwell/wildcard-letsencrypt-certificates-on-forge-d3bdec43692a");
dump($article);
/*
[
    "article_html" => "<div><h1 name="9022" id="9022" class="graf graf--h3 graf--leading raf--title">Wildcard LetsEncrypt Certificates on&#160;Forge</h1>...",
    "authors" => ["Taylor Otwell"],
    "canonical_url" => https://medium.com/@taylorotwell/wildcard-letsencrypt-certificates-on-forge-d3bdec43692a",
    "meta_data" => [ ... ],
    "meta_description" => "Today we’re happy to announce support for wildcard LetsEncrypt certificates on Laravel Forge…",
    "publish_date" => "2018-03-16 13:43:54",
    "text" => "Wildcard LetsEncrypt Certificates on Forge...",
    "title" => "Wildcard LetsEncrypt Certificates on Forge – Taylor Otwell – Medium",
    "top_image" => "https://cdn-images-1.medium.com/max/1200/1*y1yKkIQqGHcpOmsObR7WIQ.png",
]
*/

$text = "Barack Hussein Obama is an American politician who served as the 44th President of the United States from January 20, 2009 to January 20, 2017. Before that, he served in the Illinois State Senate from 1997 until 2004.";

$entities = NLP::entities( $text, 'en' );
/*
Array
(
    [0] => Barack Hussein Obama
    [1] => American
    [2] => United States
    [3] => Illinois
)
*/

$entities = NLP::spacy_entities($text, 'en' );
/*
array:4 [
  "DATE" => array:1 [
    0 => "1857"
  ]
  "GPE" => array:3 [
    0 => "Kentucky"
    1 => "the United States"
    2 => "the Confederate States of America"
  ]
  "NORP" => array:1 [
    0 => "Breckinridge  "
  ]
  "PERSON" => array:2 [
    0 => "John C. Breckinridge"
    1 => "James Buchanan's"
  ]
]
*/

$entities = NLP::corenlp_entities($text);
/*
array:6 [
  "PERSON" => array:3 [
    0 => "John C. Breckinridge"
    1 => "James Buchanan"
  ]
  "STATE_OR_PROVINCE" => array:1 [
    0 => "Kentucky"
  ]
  "COUNTRY" => array:1 [
    0 => "United States"
  ]
  "ORGANIZATION" => array:1 [
    0 => "Confederate States of America"
  ]
  "DATE" => array:1 [
    0 => "1857"
  ]
  "TITLE" => array:1 [
    0 => "vice president"
  ]
]
*/

$sentiment = NLP::sentiment( "This is great!" );
// 1

$sentiment = NLP::sentiment( "I hate this product", 'en' );
// -1

$summary = NLP::summarize($long_text);
$summary = NLP::summarize($long_text, $max_word_count) ;


$translated_text = NLP::translate($text, $from_lang, $to_lang);

$translated_text = NLP::translate("Mange er opprørt etter avsløringene om at persondata for 87 millioner Facebook-brukere skal være på avveie", null, 'pt');
// 'Muitas pessoas estão chateadas após a divulgação de que os dados pessoais de 87 milhões de usuários do Facebook devem estar fora de ordem'

$words = NLP::neighbours( 'president');
dump( $words );
/*
array:10 [
  0 => "chairman"
  1 => "vice-president"
  2 => "President"
  3 => "Chairman"
  4 => "Vice-President"
]
*/

// ensure polyglot language model is installed: polyglot download LANG:no
$norwegian_words = NLP::neighbours( 'president', 'no');
/*
array:10 [
  0 => "visepresident"
  1 => "presidenten"
  2 => "statsminister"
  3 => "utenriksminister"
  4 => "finansminister"
]
*/

$concepts = NLP::concepts( 'php' );

/*
array:10 [
  "language" => 0.40301612064483
  "technology" => 0.19656786271451
  "programming language" => 0.14456578263131
  "open source technology" => 0.057202288091524
  "scripting language" => 0.049921996879875
  "server side language" => 0.044201768070723
  "web technology" => 0.031201248049922
  "server-side language" => 0.027561102444098
]
*/
bash
# Publish nlp.php config file
$ php artisan vendor:publish --provider="Web64\LaravelNlp\NlpServiceProvider"