Home > Backend Development > PHP Tutorial > How to connect Baidu Wenxin Yiyan API with PHP to obtain specific types of sentences and conduct sentiment analysis

How to connect Baidu Wenxin Yiyan API with PHP to obtain specific types of sentences and conduct sentiment analysis

PHPz
Release: 2023-08-12 20:16:02
Original
1134 people have browsed it

How to connect Baidu Wenxin Yiyan API with PHP to obtain specific types of sentences and conduct sentiment analysis

How to connect PHP to Baidu Wenxin Yiyan API to obtain specific types of sentences and conduct sentiment analysis

Introduction

Baidu Wenxin Yiyan is An API interface that provides Chinese sentences. You can obtain corresponding sentences based on specific types, such as inspirational, love, friendship, etc. This article will introduce how to use PHP to connect to Baidu Wenxin Yiyan API and perform sentiment analysis on sentences by calling Baidu Sentiment Analysis API.

Preparation work

Before we start, we need to do some preparation work:

  1. Register a Baidu developer account, create an application, and obtain the corresponding API Key and Secret Key. You can complete these steps on [Baidu Open Cloud Platform](https://cloud.baidu.com/).
  2. Make sure your PHP environment has the cURL extension installed. cURL is a tool used to communicate with servers.

Connect to Baidu Wenxin Yiyan API

First, we need to use cURL extension to establish a connection with Baidu Wenxin Yiyan API. The following is a simple PHP function that can be used to send a GET request and return the response data of the API. You need to replace API_KEY and SECRET_KEY with your API Key and Secret Key.

function callApi($url) {
    $apiKey = "API_KEY";
    $secretKey = "SECRET_KEY";
    
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($curl, CURLOPT_USERPWD, "{$apiKey}:{$secretKey}");
    
    $response = curl_exec($curl);
    curl_close($curl);
    
    return $response;
}
Copy after login

Next, we can use this function to call Baidu Wenxin Yiyan API and obtain specific types of sentences.

$url = "https://aip.baidubce.com/rpc/2.0/creation/v1/generate";
$type = "励志"; // 可以替换成其他类型,如爱情、友情等

$requestData = [
    "type" => $type,
    "is_profanity" => 1
];

$response = callApi($url . "?" . http_build_query($requestData));
$data = json_decode($response, true);

if(isset($data["error_code"])) {
    echo "API请求错误:" . $data["error_msg"];
} else {
    $sentence = $data["sentence"];
    echo "获取到句子:" . $sentence;
}
Copy after login

The above code will return a specific type of sentence and print the output. You can adjust the code as needed.

Perform sentiment analysis

Next, we will use Baidu sentiment analysis API to perform sentiment analysis on the obtained sentences. First, you also need to replace the API_KEY and SECRET_KEY below.

function sentimentAnalysis($text) {
    $apiKey = "API_KEY";
    $secretKey = "SECRET_KEY";
    
    $url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify";
    
    $requestData = [
        "text" => $text
    ];
    
    $response = callApi($url . "?" . http_build_query($requestData));
    $data = json_decode($response, true);
    
    if(isset($data["error_code"])) {
        echo "API请求错误:" . $data["error_msg"];
    } else {
        $positiveProb = $data["items"][0]["positive_prob"];
        $negativeProb = $data["items"][0]["negative_prob"];
        
        if($positiveProb > $negativeProb) {
            echo "情感分析结果:正向";
        } elseif($positiveProb < $negativeProb) {
            echo "情感分析结果:负向";
        } else {
            echo "情感分析结果:中性";
        }
    }
}
Copy after login

Finally, we can call this function to perform sentiment analysis on the obtained sentences.

sentence = "这是一句励志的话"; // 可以替换成其他句子

sentimentAnalysis($sentence);
Copy after login

The above code will print output based on the sentiment analysis results.

Conclusion

By connecting to Baidu Wenxin Yiyan API to obtain specific types of sentences, and using Baidu Sentiment Analysis API to perform sentiment analysis on the sentences, we can quickly obtain and analyze the sentiment of Chinese sentences. In this way, we can use these APIs in various application scenarios, such as generating various statements, conducting public opinion analysis, etc. Hope this article is helpful to you!

The above is the detailed content of How to connect Baidu Wenxin Yiyan API with PHP to obtain specific types of sentences and conduct sentiment analysis. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template