How to connect PHP to Baidu Wenxin Yiyan API to obtain specific types of sentences and conduct sentiment analysis
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.
Before we start, we need to do some preparation work:
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; }
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; }
The above code will return a specific type of sentence and print the output. You can adjust the code as needed.
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 "情感分析结果:中性"; } } }
Finally, we can call this function to perform sentiment analysis on the obtained sentences.
sentence = "这是一句励志的话"; // 可以替换成其他句子 sentimentAnalysis($sentence);
The above code will print output based on the sentiment analysis results.
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!