aflorea4/php-nlp-client
Composer 安装命令:
composer require aflorea4/php-nlp-client
包简介
Library for accessing NLP apis (Fixed HTTP header interpretation)
README 文档
README
This is a simple PHP library for performing multilingual Natural Language tasks using Web64's NLP-Server https://github.com/web64/nlpserver and other providers.
NLP tasks available through Web64's NLP Server:
- Language detection
- Article Extraction from HTML or URL
- Entity Extraction (NER) - Multilingual
- Sentiment Analysis - Multilingual
- Embeddings / Neighbouring words - Multilingual
- Summarization
NLP Tasks Available through Stanford's CoreNLP Server:
NLP Tasks Available through Microsoft Labs API:
Laravel Package
There is also a Laravel wrapper for this library available here: https://github.com/web64/laravel-nlp
Installation
composer require web64/php-nlp-client
NLP Server
Most NLP features in this package requires a running instance of the NLP Server, which is a simple python flask app providing web service api access to common python NLP libraries.
Installation instrcuctions: https://github.com/web64/nlpserver
Entity Extraction - Named Entity Recognition (NER)
This library provides access to three different methods for entity extraction.
| Provider | Language Support | Programming Lang. | API Access |
|---|---|---|---|
| Polyglot | 40 languages | Python | NLP Server |
| Spacy | 7 languages | Python | NLP Server |
| CoreNLP | 6 languages | Java | CoreNLP Standalone server |
If you are dealing with text in English or one of the major European language you will get the best results with CoreNLP or Spacy.
The quality of extracted entities with Polyglot is not great, but for many languages it is the only available option at the moment.
Polyglot and Spacy NER is accessible thorough the NLP Server, CoreNLP requires its own standalone java server.
Usage
Language detection:
$nlp = new \Web64\Nlp\NlpClient('http://localhost:6400/'); $detected_lang = $nlp->language( "The quick brown fox jumps over the lazy dog" ); // 'en'
Article & Metadata Extraction
// From URL $nlp = new \Web64\Nlp\NlpClient('http://localhost:6400/'); $newspaper = $nlp->newspaper('https://github.com/web64/nlpserver'); // or from HTML $html = file_get_contents( 'https://github.com/web64/nlpserver' ); $newspaper = $nlp->newspaper_html( $html ); Array ( [article_html] => <div><h1><a id="user-content-nlp-server" class="anchor" href="#nlp-server"></a>NLP Server</h1> .... </div> [authors] => Array() [canonical_url] => https://github.com/web64/nlpserver [meta_data] => Array() [meta_description] => GitHub is where people build software. More than 27 million people use GitHub to discover, fork, and contribute to over 80 million projects. [meta_lang] => en [source_url] => [text] => NLP Server. Python Flask web service for easy access to multilingual NLP tasks such as language detection, article extraction... [title] => web64/nlpserver: NLP Web Service [top_image] => https://avatars2.githubusercontent.com/u/76733?s=400&v=4 )
Entitiy Extraction & Sentiment Analysis (Polyglot)
This uses the Polyglot multilingual NLP library to return entities and a sentiment score for given text.Ensure the models for the required languages are downloaded for Polyglot.
$polyglot = $nlp->polyglot_entities( $text, 'en' ); $polyglot->getSentiment(); // -1 $polyglot->getEntityTypes(); /* Array ( [Locations] => Array ( [0] => United Kingdom ) [Organizations] => [Persons] => Array ( [0] => Ben [1] => Sir Benjamin Hall [2] => Benjamin Caunt ) ) */ $polyglot->getLocations(); // Array of Locations $polyglot->getOrganizations(); // Array of organisations $polyglot->getPersons(); // Array of people $polyglot->getEntities(); /* Returns flat array of all entities Array ( [0] => Ben [1] => United Kingdom [2] => Sir Benjamin Hall [3] => Benjamin Caunt ) */
Entity Extraction with Spacy
$text = "Harvesters is a 1905 oil painting on canvas by the Danish artist Anna Ancher, a member of the artists' community known as the Skagen Painters."; $nlp = new \Web64\Nlp\NlpClient('http://localhost:6400/'); $entities = $nlp->spacy_entities( $text ); /* Array ( [DATE] => Array ( [0] => 1905 ) [NORP] => Array ( [0] => Danish ) [ORG] => Array ( [0] => the Skagen Painters ) [PERSON] => Array ( [0] => Anna Ancher ) ) */
English is used by default. To use another language, ensure the Spacy language model is downloaded and add the language as the second parameter
$entities = $nlp->spacy_entities( $spanish_text, 'es' );
Sentiment Analysis
$sentiment = $nlp->sentiment( "This is the worst product ever" ); // -1 $sentiment = $nlp->sentiment( "This is great! " ); // 1 // specify language in second parameter for non-english $sentiment = $nlp->sentiment( $french_text, 'fr' );
Neighbouring words (Embeddings)
$nlp = new \Web64\Nlp\NlpClient('http://localhost:6400/'); $neighbours = $nlp->neighbours('obama', 'en'); /* Array ( [0] => Bush [1] => Reagan [2] => Clinton [3] => Ahmadinejad [4] => Nixon [5] => Karzai [6] => McCain [7] => Biden [8] => Huckabee [9] => Lula ) */
Summarization
Extract short summary from a long text
$summary = $nlp->summarize( $long_text );
Readability
Article Extraction using python port of Readability.js
$nlp = new \Web64\Nlp\NlpClient( 'http://localhost:6400/' ); // From URL: $article = $nlp->readability('https://github.com/web64/nlpserver'); // From HTML: $html = file_get_contents( 'https://github.com/web64/nlpserver' ); $article = $nlp->readability_html( $html ); /* Array ( [article_html] => <div><h1>NLP Server</h1><p>Python 3 Flask web service for easy access to multilingual NLP tasks ... [short_title] => web64/nlpserver: NLP Web Service [text] => NLP Server Python 3 Flask web service for easy access to multilingual NLP tasks such as language detection ... [title] => GitHub - web64/nlpserver: NLP Web Service ) */
CoreNLP - Entity Extraction (NER)
CoreNLP has much better quality for NER that Polyglot, but only supports a few languages including English, French, German and Spanish.
Download CoreNLP server (Java) here: https://stanfordnlp.github.io/CoreNLP/index.html#download
Install CoreNLP
# Update download links with latest versions from the download page wget http://nlp.stanford.edu/software/stanford-corenlp-full-2018-10-05.zip unzip stanford-corenlp-full-2018-10-05.zip cd stanford-corenlp-full-2018-02-27 # Download English language model: wget http://nlp.stanford.edu/software/stanford-english-kbp-corenlp-2018-10-05-models.jar
Running the CoreNLP server
# 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 &
When the CoreNLP server is running you can access it on port 9000: http://localhost:9000/
More info about running the CoreNLP Server: https://stanfordnlp.github.io/CoreNLP/corenlp-server.html
$corenlp = new \Web64\Nlp\CoreNlp('http://localhost:9000/'); $entities = $corenlp->entities( $text ); /* Array ( [NATIONALITY] => Array ( [0] => German [1] => Turkish ) [ORGANIZATION] => Array ( [0] => Foreign Ministry ) [TITLE] => Array ( [0] => reporter [1] => journalist [2] => correspondent ) [COUNTRY] => Array ( [0] => Turkey [1] => Germany ) */
Concept Graph
Microsoft Concept Graph For Short Text Understanding: https://concept.research.microsoft.com/
Find related concepts to provided keyword
$concept = new \Web64\Nlp\MsConceptGraph; $res = $concept->get('php'); /* Array ( [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 [server side scripting language] => 0.023920956838274 [feature] => 0.021840873634945 ) */
Python libraries
These are the python libraries used by the NLP Server for the NLP and data extraction tasks.
| Library | URL | NLP Task used |
|---|---|---|
| langid.py | https://github.com/saffsd/langid.py | Language detection |
| Newspaper | https://github.com/codelucas/newspaper | Article & metadata extraction |
| Spacy | https://spacy.io/ | Entity extraction |
| Polyglot | https://github.com/aboSamoor/polyglot | Multilingual NLPprocessing toolkit |
| Gensim | https://radimrehurek.com/gensim/ | Summarization |
| Readability | https://github.com/buriy/python-readability | Article extraction |
Other PHP NLP projects
Contribute
Get in touch if you have any feedback or ideas on how to improve this package or the documentation.
aflorea4/php-nlp-client 适用场景与选型建议
aflorea4/php-nlp-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 131 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「nlp」 「article extraction」 「natural language」 「entity extraction」 「language detection」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 aflorea4/php-nlp-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aflorea4/php-nlp-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 aflorea4/php-nlp-client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Extract, Transform and Load data using PHP.
Library for merging source data to destination data only if destination data remains valid after that
PHP Interface for Babel Street Text Analytics
Backend Optimizing Bundle
Article CSS-ID Frontend Output Optimization
Puts the Symfony Translation Component on steroids
统计信息
- 总下载量: 131
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-07-19