meaningcloud/meaningcloud-php

MeaningCloud 官方 PHP SDK

3.0 2020-09-14 10:53 UTC

This package is auto-updated.

Last update: 2024-09-14 19:58:38 UTC


README

这是 MeaningCloud 的官方 PHP 客户端,旨在让您能够轻松地从自己的应用程序中使用 MeaningCloud 的服务。

MeaningCloud

MeaningCloud 是一个基于云的文本分析服务,通过 API 允许您从各种非结构化内容中提取意义:社交媒体对话、文章、文档等。您可以在此查看我们的演示 这里

不同的 API 提供了对许多 NLP 任务(如自动分类、情感分析、主题提取等)的轻松访问。要使用此服务,您只需登录到 MeaningCloud(通过注册或使用其他服务登录:https://www.meaningcloud.com/developer/login),您将收到一个订阅了免费计划的许可证密钥,最多包含 20k 个信用。

您可以在以下链接中了解更多关于计划和可用功能的信息: https://www.meaningcloud.com/products/pricing

SDK 版本

从版本 3.0 开始,分类 API class-1.1 已弃用。请参阅 class-1.1 到 class-2.0 模型迁移指南 以升级您的分类模型。

入门

安装

您可以通过使用 Composer 将 meaningcloud-php 加载到项目中。

如果您已经安装了 composer,只需运行以下命令

composer require meaningcloud/meaningcloud-php

配置

要开始使用 MeaningCloud 的 API,您需要登录到 MeaningCloud(通过注册或使用其他服务登录)。一旦完成,您将获得一个 许可证密钥。将其复制并粘贴到代码中的相应位置,选择您想使用的 API 和参数,然后就可以使用了。

您可以在网站上的 API 部分(https://www.meaningcloud.com/developer/apis)找到有关 API 的所有技术文档。

一些资源包含在 垂直语言包 中。要使用它们,您必须能够访问它们,无论是通过请求我们提供给所有这些的 30 天免费试用期,还是通过订阅相应的包。

我们随时在 support@meaningcloud.com 提供支持

功能

此 SDK 当前包含以下内容

  • MCRequest:用于轻松创建对 MeaningCloud 的任何 API 的请求。它还可以用于直接生成请求,而无需使用特定类。

    • MCClassRequest:对 MeaningCloud 文本分类 API 的请求进行建模。
    • MCClusteringRequest:对 MeaningCloud 文本聚类 API 的请求进行建模。
    • MCDeepCategorizationRequest:对 MeaningCloud 深度分类 API 的请求进行建模。
    • MCLanguageRequest:表示对MeaningCloud语言识别API的请求。
    • MCParserRequest:表示对Meaningcloud词元还原、词性标注和解析API的请求。
    • MCSentimentRequest:表示对MeaningCloud情感分析API的请求。
    • MCSummarizationRequest:表示对Meaningcloud摘要API的请求。
    • MCTopicsRequest:表示对MeaningCloud主题提取API的请求。
  • MCResponse:表示MeaningCloud API的通用响应。

    • MCClassResponse:表示文本分类API的响应,提供辅助函数以处理响应并提取每个类别中的不同字段。
    • MCClusteringResponse:表示文本聚类API的响应,提供辅助函数以处理响应并提取每个簇中的不同字段。
    • MCDeepCategorizationResponse:表示深度分类API的响应,提供辅助函数以处理响应并提取每个类别中的不同字段。
    • MCLanguageResponse:表示语言识别API的响应,提供辅助函数以处理响应并提取不同级别和不同元素检测到的情感。
    • MCParserResponse:表示词元还原、词性标注和解析API的响应,提供辅助函数以处理响应并提取文本的词元和词性标注。
    • MCSentimentResponse:表示情感分析API的响应,提供辅助函数以处理响应并提取不同级别和不同元素检测到的情感。
    • MCSummarizationResponse:表示摘要API的响应,提供辅助函数以处理响应并获取提取的摘要。
    • MCTopicsResponse:表示主题提取API的响应,提供辅助函数以处理响应,提取不同类型的主题以及其中一些最常用的字段。

用法

bin文件夹中,有两个示例

  • MCClient.php,其中包含一个简单示例,说明如何使用SDK

  • MCShowcase,它实现了一个管道,从文件夹中读取纯文本文件,并生成两个CSV文件作为输出:一个包含对每个文本执行的各种分析类型,以及在对整个集合运行文本聚类的结果。所执行的分析包括

以下是如何使用此客户端的示例(也包含在bin文件夹中)。此代码向语言识别API和主题提取API发送两个请求,一个请求用于检测第一个请求中的语言。两个请求的结果都打印在标准输出中

require_once(__DIR__.'/../vendor/autoload.php');

use MeaningCloud\MCRequest;
use MeaningCloud\MCLangResponse;
use MeaningCloud\MCTopicsRequest;

$server = 'https://api.meaningcloud.com/';
$license_key = '<< your license key >>'; // your license key (https://www.meaningcloud.com/developer/account/subscription)

$text = 'London is a very nice city but I also love Madrid.';

try {
  // We are going to make a request to the Language Identification API
  $mc = new MCRequest($server.'lang-2.0', $license_key);

  //We set the content we want to analyze
  $mc->setContentTxt($text);
  //$mc->setContentUrl('https://en.wikipedia.org/wiki/Star_Trek'); //if we want to analyze an URL
  $langResponse = new MCLangResponse($mc->sendRequest());

  // if there are no errors in the request, we will use the language detected to make a request to Sentiment and Topics
  if($langResponse->isSuccessful()) {
    echo "\nThe request to 'Language Identification' finished successfully!\n";


    $languages = $langResponse->getLanguages();
    if(!empty($languages)) {
      $language = $languages[0];
      $codeLanguage = $langResponse->getLanguageCode($language);
      echo "\tLanguage detected: ".$langResponse->getLanguageName($language).' ('.$codeLanguage.")\n";

      // We are going to make a request to the Topics Extraction API
      $mc_topics = new MCTopicsRequest($license_key, $codeLanguage, $text);

      // We send the request to the API
      $topicsResponse = $mc_topics->sendTopicsRequest();

      // if there are no errors in the request, we print the output
      if($topicsResponse->isSuccessful()) {
        echo "\nThe request to 'Topics Extraction' finished successfully!\n";

        $entities = $topicsResponse->getEntities();
        if(!empty($entities)) {
          echo "\tEntities detected (".sizeof($entities)."):\n";
          foreach ($entities as $entity) {
            echo "\t\t".$topicsResponse->getTopicForm($entity).' --> '.$topicsResponse->getTypeLastNode($topicsResponse->getOntoType($entity))."\n";
          }
        }
      } else {
        echo "\nOh no! There was the following error: ".$topicsResponse->getStatusMsg()."\n";
      }
    }
  } else {
    if(is_null($langResponse->getResponse()))
      echo "\nOh no! The request sent did not return a Json\n";
    else
      echo "\nOh no! There was the following error: ".$langResponse->getStatusMsg()."\n";
  }
} catch (Exception $e) {
  echo "\nEXCEPTION: ".$e->getMessage().' ('.$e->getFile().':'.$e->getLine().')'."\n\n";
}