daVmixcool / php-sentiment-analyzer
PHP Sentiment Analyzer 是一个基于词典和规则的 sentiment 分析工具,使用 VADER(Valence Aware Dictionary and sentiment Reasoner)来理解句子中的情感。
1.2.2
2020-09-17 13:50 UTC
Requires
- php: >=5.5.9
README
PHP Sentiment Analyzer 是一个基于词典和规则的 sentiment 分析工具,使用 VADER(Valence Aware Dictionary and sentiment Reasoner)来理解句子中的情感。
特性
- 文本
- 表情符号
- 表情符号
要求
- PHP 5.5 及以上
步骤
安装
Composer
运行以下命令通过 Composer 包含
composer require davmixcool/php-sentiment-analyzer
简单用法
Use Sentiment\Analyzer; $analyzer = new Analyzer(); $output_text = $analyzer->getSentiment("David is smart, handsome, and funny."); $output_emoji = $analyzer->getSentiment("😁"); $output_text_with_emoji = $analyzer->getSentiment("Aproko doctor made me 🤣."); print_r($output_text); print_r($output_emoji); print_r($output_text_with_emoji);
简单输出
David is smart, handsome, and funny. ---------------- ['neg'=> 0.0, 'neu'=> 0.337, 'pos'=> 0.663, 'compound'=> 0.7096]
😁 ------------------- ['neg' => 0, 'neu' => 0.5, 'pos' => 0.5, 'compound' => 0.4588]
Aproko doctor made me 🤣 ------------- ['neg' => 0, 'neu' => 0.714, 'pos' => 0.286, 'compound' => 0.4939]
高级用法
您现在可以动态更新不在词典中的单词的 VADER(Valence)词典。请参见以下示例
Use Sentiment\Analyzer; $sentiment = new Sentiment\Analyzer(); $strings = [ 'Weather today is rubbish', 'This cake looks amazing', 'His skills are mediocre', 'He is very talented', 'She is seemingly very agressive', 'Marie was enthusiastic about the upcoming trip. Her brother was also passionate about her leaving - he would finally have the house for himself.', 'To be or not to be?', ]; //new words not in the dictionary $newWords = [ 'rubbish'=> '-1.5', 'mediocre' => '-1.0', 'agressive' => '-0.5' ]; //Dynamically update the dictionary with the new words $sentiment->updateLexicon($newWords); //Print results foreach ($strings as $string) { // calculations: $scores = $sentiment->getSentiment($string); // output: echo "String: $string\n"; print_r(json_encode($scores)); echo "<br>"; }
高级输出
Weather today is rubbish ------------- {"neg":0.455,"neu":0.545,"pos":0,"compound":-0.3612}
This cake looks amazing ------------- {"neg":0,"neu":0.441,"pos":0.559,"compound":0.5859}
His skills are mediocre ------------- {"neg":0.4,"neu":0.6,"pos":0,"compound":-0.25}
He is very talented ------------- {"neg":0,"neu":0.457,"pos":0.543,"compound":0.552}
She is seemingly very agressive ------------- {"neg":0.338,"neu":0.662,"pos":0,"compound":-0.2598}
Marie was enthusiastic about the upcoming trip. Her brother was also passionate about her leaving - he would finally have the house for himself. ------------- {"neg":0,"neu":0.761,"pos":0.239,"compound":0.765}
String: To be or not to be? ------------- {"neg":0,"neu":1,"pos":0,"compound":0}
星标数
分支数
许可证
本软件包遵循MIT 许可证。
参考
Hutto, C.J. & Gilbert, E.E. (2014). VADER: A Parsimonious Rule-based Model for Sentiment Analysis of Social Media Text. Eighth International Conference on Weblogs and Social Media (ICWSM-14). Ann Arbor, MI, June 2014.