rich-davis1/phpw2v
Composer 安装命令:
composer require rich-davis1/phpw2v
包简介
A PHP implementation of Word2Vec, a popular word embedding algorithm created by Tomas Mikolov and popularized by Radim Řehůřek & Peter Sojka with the Gensim Python library.
README 文档
README
A PHP implementation of Word2Vec, a popular word embedding algorithm created by Tomas Mikolov and popularized by Radim Řehůřek & Peter Sojka with the Gensim Python library
Installation
Install PHPW2V into your project using Composer:
$ composer require rich-davis1/phpw2v
Requirements
- PHP 7.4 or above
Using PHPW2v
Step 1: Require Vendor autoload and import PHPW2V at the top of your file
<?php
require __DIR__ . '/vendor/autoload.php';
use PHPW2V\Word2Vec;
use PHPW2V\SoftmaxApproximators\NegativeSampling;
Step 2: Prepare an array of sentences
$sentences = [
'the fox runs fast',
'the cat jogged fast',
'the pug ran fast',
'the cat runs fast',
'the dog ran fast',
'the pug runs fast',
'the fox ran fast',
'dogs are our link to paradise',
'pets are humanizing',
'a dog is the only thing on earth that loves you more than you love yourself',
];
Step 3: Train your model & save it for use later
$dimensions = 150; //vector dimension size
$sampling = new NegativeSampling; //Softmax Approximator
$minWordCount = 2; //minimum word count
$alpha = .05; //the learning rate
$window = 3; //window for skip-gram
$epochs = 500; //how many epochs to run
$subsample = 0.05; //the subsampling rate
$word2vec = new Word2Vec($dimensions, $sampling, $window, $subsample, $alpha, $epochs, $minWordCount);
$word2vec->train($sentences);
$word2vec->save('my_word2vec_model');
Step 4: Load your previously trained model and find the most similar words
$word2vec = new Word2Vec();
$word2vec = $word2vec->load('my_word2vec_model');
$mostSimilar = $word2vec->mostSimilar(['dog']);
Which results in:
Array
(
[fox] => 0.65303660275952
[pug] => 0.63475600376409
[you] => 0.63469270773687
[cat] => 0.28333476473645
[are] => 0.0086017358485732
[ran] => -0.016116842526914
[the] => -0.068253396295047
[runs] => -0.11967150816883
[fast] => -0.12999690227979
)
Step 5: Find similar words with both positive and negative contexts
$mostSimilar = $word2vec->mostSimilar(['dog'], ['cat']);
Step 6: Get the word embedding of a word to be used in other NLP projects
$wordEmbedding = $word2vec->wordVec('dog');
rich-davis1/phpw2v 适用场景与选型建议
rich-davis1/phpw2v 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.16k 次下载、GitHub Stars 达 47, 最近一次更新时间为 2020 年 04 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 rich-davis1/phpw2v 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rich-davis1/phpw2v 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.16k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 47
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-04-19