web64 / laravel-nlp
Laravel 访问 NLP 工具的包
Requires
- lsmonki/php-open-calais: ^2.1
- stichoza/google-translate-php: ~4.1
- web64/php-nlp-client: ^0.40
README
一个简单的包装器,用于访问 Python 和 Java NLP 库以及外部 NLP API 的 PHP-NLP-Client 库。
安装
$ composer require web64/laravel-nlp
请按照此处说明安装 NLP 服务器和 CoreNLP 服务器
如果您想使用 Opencalais,请在此注册以获取令牌 这里 并阅读他们的 服务条款
配置
将 NLPSERVER_URL
添加到您的 .env
文件中以指定 NLP 服务器运行的位置。如果您想使用 CoreNLP 或 Opencalais,也在 .env 中填写这些详细信息。
NLPSERVER_URL="https://:6400/"
CORENLP_HOST="https://:9000/"
OPENCALAIS_KEY=
或者,更新 nlp.php 配置文件。
# Publish nlp.php config file $ php artisan vendor:publish --provider="Web64\LaravelNlp\NlpServiceProvider"
快速开始
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');
NLP 服务器
此包需要运行 NLP 服务器实例(https://github.com/web64/nlpserver)以使大部分功能正常工作。有关 NLP 服务器的安装说明,请参阅文档。
包含的 NLP 工具
用法
包含类以使用 NLP 门面。
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 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", ] */
使用 Polyglot 进行实体提取
此函数使用支持 40 种语言实体提取的 Polyglot 库。确保您已下载您正在使用的语言模型。
对于英语和其他主要欧洲语言,请使用 Spacy 或 CoreNLP 以获得最佳结果。
$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 ) */
使用 Spacy 进行实体提取
运行的 NLP 服务器提供对 Spacy 实体提取的访问。
Spacy 为英语、德语、西班牙语、葡萄牙语、法语、意大利语和荷兰语提供语言模型。
$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" ] ] */
使用 CoreNLP 进行实体提取
CoreNLP 为英语文本提供非常好的实体提取。
要使用此功能,您需要一个运行的 CoreNLP 服务器实例。请参阅 安装说明
在 .env 或 config/nlp/php
中的 CORENLP_HOST
中指定 CoreNLP 服务器的 URL。
$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" ] ] */
情感分析
这将返回一个从 -1 到 +1 的值,其中 > 0 被认为是正面的,而一切 < 0 被认为是负面的。
将语言代码作为第二个参数提供,以指定文本的语言。默认值='en'。
$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) ;
翻译
第二个参数是源语言。如果留为 NULL,则语言将自动检测。
$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" ] */
概念
这使用微软概念图进行短文本理解:https://concept.research.microsoft.com/
将返回一个相关概念的数组。
$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 ] */
更多
有关其他Laravel NLP包,请查看
- https://github.com/AntoineAugusti/laravel-sentiment-analysis
- https://github.com/michaeljhopkins/Laravel-Aylien-Wrapper
- https://github.com/findbrok/laravel-personality-insights
贡献
如果您有任何关于如何改进此包或文档的反馈或想法,请与我们联系。