PHPAPI文本分类怎么实现

wen PHP项目 8

本文目录导读:

PHPAPI文本分类怎么实现

  1. 使用第三方 API(最简单)
  2. 使用 PHP 扩展库
  3. 使用朴素贝叶斯分类器
  4. 使用词袋模型实现
  5. 推荐方案
  6. 优化建议

在 PHP 中实现文本分类,通常有以下几种主流方案,我会从简单到复杂为你介绍:

使用第三方 API(最简单)

Google Cloud Natural Language API

<?php
require_once 'vendor/autoload.php';
use Google\Cloud\Language\LanguageClient;
$language = new LanguageClient([
    'keyFilePath' => '/path/to/key.json'
]);
$text = "这是一个关于体育比赛的新闻";
$annotation = $language->classifyText($text);
foreach ($annotation->categories() as $category) {
    echo $category['name'] . ': ' . $category['confidence'] . "\n";
}
?>

阿里云 NLP API

<?php
require_once 'vendor/autoload.php';
use AlibabaCloud\Client\AlibabaCloud;
AlibabaCloud::accessKeyClient('your-access-key', 'your-access-secret')
    ->regionId('cn-hangzhou')
    ->asDefaultClient();
$result = AlibabaCloud::rpc()
    ->product('Nlp')
    ->version('2018-04-08')
    ->action('GetPosChEcom')
    ->method('POST')
    ->host('nlp.cn-hangzhou.aliyuncs.com')
    ->options([
        'query' => [
            'Text' => '文本内容',
            'Token' => 'your-token',
        ],
    ])
    ->request();
print_r($result->toArray());
?>

使用 PHP 扩展库

PHP-ML (PHP机器学习库)

安装:

composer require php-ai/php-ml

实现文本分类:

<?php
require_once 'vendor/autoload.php';
use Phpml\Classification\SVC;
use Phpml\FeatureExtraction\TfIdfTransformer;
use Phpml\Tokenization\WhitespaceTokenizer;
use Phpml\FeatureExtraction\TokenCountVectorizer;
use Phpml\Pipeline;
// 1. 准备训练数据
$samples = [
    '这是一条体育新闻',
    '篮球比赛精彩纷呈',
    '股市今日大涨',
    '金融政策调整',
    '人工智能技术突破',
    '芯片行业发展趋势'
];
$labels = [
    '体育', '体育', '财经', '财经', '科技', '科技'
];
// 2. 创建处理流水线
$pipeline = new Pipeline([
    new TokenCountVectorizer(new WhitespaceTokenizer()),
    new TfIdfTransformer(),
], new SVC());
// 3. 训练模型
$pipeline->train($samples, $labels);
// 4. 预测
$text = "足球世界杯预选赛";
$predicted = $pipeline->predict([$text]);
echo "分类结果: " . $predicted[0];
?>

使用朴素贝叶斯分类器

<?php
require_once 'vendor/autoload.php';
use Phpml\Classification\NaiveBayes;
use Phpml\FeatureExtraction\TfIdfTransformer;
use Phpml\FeatureExtraction\TokenCountVectorizer;
use Phpml\Tokenization\WhitespaceTokenizer;
class TextClassifier {
    private $vectorizer;
    private $transformer;
    private $classifier;
    public function __construct() {
        $this->vectorizer = new TokenCountVectorizer(new WhitespaceTokenizer());
        $this->transformer = new TfIdfTransformer();
        $this->classifier = new NaiveBayes();
    }
    public function train(array $samples, array $labels) {
        // 特征提取
        $this->vectorizer->fit($samples);
        $this->vectorizer->transform($samples);
        $this->transformer->fit($samples);
        $this->transformer->transform($samples);
        // 训练分类器
        $this->classifier->train($samples, $labels);
    }
    public function predict(string $text) {
        $samples = [$text];
        $this->vectorizer->transform($samples);
        $this->transformer->transform($samples);
        return $this->classifier->predict($samples);
    }
}
// 使用示例
$classifier = new TextClassifier();
// 训练数据
$samples = [
    '计算机科学和编程',
    '机器学习与深度学习',
    '股票市场分析',
    '基金投资策略',
    '世界杯足球赛',
    'NBA篮球联赛'
];
$labels = ['科技', '科技', '财经', '财经', '体育', '体育'];
// 训练
$classifier->train($samples, $labels);
// 预测
$result = $classifier->predict('人工智能在医疗领域的应用');
echo "分类: " . $result[0];
?>

使用词袋模型实现

<?php
class SimpleTextClassifier {
    private $categories = [];
    private $vocabulary = [];
    private $wordCounts = [];
    public function train(array $documents, array $labels) {
        // 构建词汇表
        foreach ($documents as $doc) {
            $words = $this->tokenize($doc);
            foreach ($words as $word) {
                if (!isset($this->vocabulary[$word])) {
                    $this->vocabulary[$word] = 0;
                }
                $this->vocabulary[$word]++;
            }
        }
        // 统计每个类别中的词频
        for ($i = 0; $i < count($documents); $i++) {
            $category = $labels[$i];
            if (!isset($this->wordCounts[$category])) {
                $this->wordCounts[$category] = [];
            }
            $words = $this->tokenize($documents[$i]);
            foreach ($words as $word) {
                if (!isset($this->wordCounts[$category][$word])) {
                    $this->wordCounts[$category][$word] = 0;
                }
                $this->wordCounts[$category][$word]++;
            }
        }
        // 保存类别
        $this->categories = array_unique($labels);
    }
    public function predict(string $text) {
        $words = $this->tokenize($text);
        $scores = [];
        foreach ($this->categories as $category) {
            $score = 0;
            foreach ($words as $word) {
                if (isset($this->wordCounts[$category][$word])) {
                    $score += $this->wordCounts[$category][$word];
                }
            }
            $scores[$category] = $score;
        }
        // 返回得分最高的类别
        arsort($scores);
        return key($scores);
    }
    private function tokenize(string $text) {
        // 简单分词
        $text = mb_strtolower($text, 'UTF-8');
        $text = preg_replace('/[^\p{L}\p{N}\s]/u', '', $text);
        return preg_split('/\s+/u', $text, -1, PREG_SPLIT_NO_EMPTY);
    }
}
// 使用示例
$classifier = new SimpleTextClassifier();
$documents = [
    'PHP是最好的编程语言',
    'Python人工智能开发',
    '股票投资风险控制',
    '基金定投策略',
    '篮球比赛精彩回顾'
];
$labels = ['编程', '编程', '金融', '金融', '体育'];
$classifier->train($documents, $labels);
$result = $classifier->predict('JavaScript前端开发');
echo "分类结果: " . $result;
?>

推荐方案

对于生产环境:

<?php
// 使用 Elasticsearch 的机器学习功能
require_once 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()
    ->setHosts(['localhost:9200'])
    ->build();
// 创建分类模型
$params = [
    'body' => [
        'description' => '文本分类模型',
        'source' => [
            'index' => ['documents'],
            'query' => ['match_all' => new \stdClass()]
        ],
        'dest' => [
            'index' => 'classified_documents'
        ],
        'analysis' => [
            'classifier' => [
                'field' => 'text',
                'target_field' => 'category',
                'num_top_classes' => 5
            ]
        ]
    ]
];
$response = $client->ml()->putDataFrameAnalytics('text-classifier', $params);
?>

优化建议

  1. 中文分词:使用 jieba-php 进行中文分词
  2. 停用词过滤:去掉无意义的词语
  3. 特征选择:使用卡方检验或信息增益选择关键词
  4. 交叉验证:评估模型性能
  5. 模型持久化:训练好的模型保存到文件

选择哪种方案取决于你的具体需求,如果对精度要求高且数据量大,建议使用第三方 API 或 TensorFlow,如果简单应用,PHP-ML 就足够了。

抱歉,评论功能暂时关闭!