Current Location: Home> Latest Articles> How to Implement Pinyin Conversion for Baidu Wenxin Yiyan Random Sentences in PHP Development

How to Implement Pinyin Conversion for Baidu Wenxin Yiyan Random Sentences in PHP Development

gitbox 2025-06-16

1. Overview

Baidu Wenxin Yiyan is a popular sentence generator that creates a variety of interesting phrases. To better showcase Chinese, these sentences are often converted into simplified and traditional Chinese characters, as well as Pinyin. In PHP development, this functionality can be achieved by using the open-source Pinyin library, pinyin.php.

2. Installing the Pinyin Library

2.1 Downloading the Source Code

First, you can download the source code of pinyin.php from GitHub. Visit the GitHub link: https://github.com/overtrue/pinyin, or use the git command to clone it:

<span class="fun">git clone https://github.com/overtrue/pinyin.git</span>

2.2 Including the Pinyin Library

In your PHP project, use composer to include the pinyin.php library. Add the dependency in the composer.json file, and run composer update:

"require": {
    "overtrue/pinyin": "^4.0.0"
}

In the PHP file where you want to use the Pinyin library, include the following code:

<span class="fun">require __DIR__ . '/vendor/autoload.php';</span>

3. Implementing the Text-to-Pinyin Function

3.1 Including the Pinyin Library

In the PHP file where you need to use the text-to-Pinyin function, first include the Pinyin library:

require __DIR__ . '/vendor/autoload.php';
use Overtrue\Pinyin\Pinyin;

3.2 Implementing the Text-to-Pinyin Function

Using the methods provided by the Pinyin library, we can quickly implement the functionality to convert Chinese text into Pinyin. The following code shows how to convert Chinese text into a Pinyin string:

function convertToPinyin($text) {
    global $pinyin;
    $pinyinText = $pinyin->convert($text, PINYIN_TONE);
    $pinyinText = str_replace(' ', '', $pinyinText);
    $pinyinText = str_replace('/', '-', $pinyinText);
    return $pinyinText;
}

This function calls the Pinyin class's convert method to convert the Chinese text into a Pinyin string, specifying that the Pinyin should include tones. Then, it uses str_replace to remove spaces and replace "/" with "-", resulting in a properly formatted Pinyin string.

3.3 Testing Pinyin Conversion

Next, you can write some test code to verify that the text-to-Pinyin functionality works correctly:

$text = '百度文心一言';
echo convertToPinyin($text); // Output: bai-du-wen-xin-yi-yan

For the Chinese text "百度文心一言", the output will be the Pinyin string "bai-du-wen-xin-yi-yan".

4. Implementing Baidu Wenxin Yiyan Random Sentences

4.1 Installing Baidu Wenxin Yiyan API

Baidu Wenxin Yiyan offers an API that developers can use. Before using it, you need to apply for an API key to access it. The detailed steps can be found in the official documentation.

The API can also be installed using composer:

<span class="fun">composer require militch/yiyan-api</span>

When using the API, you need to include the API class first:

<span class="fun">use Militch\Yiyan\Yiyan;</span>

4.2 Fetching Random Sentences

Using the Baidu Wenxin Yiyan API, you can fetch randomly generated sentences. The following code fetches a sentence from the API and then converts it into a Pinyin string:

$yiyan = new Yiyan();
$yiyan->setType('hitokoto');
$yiyan->setEncode('text');
$hitokoto = $yiyan->get();
echo convertToPinyin($hitokoto);

This code fetches a random sentence from the API and converts it into a Pinyin string for output.

5. Conclusion

Through the steps above, we can implement the Pinyin conversion functionality for random sentences from Baidu Wenxin Yiyan in PHP development. By using the open-source Pinyin library, we can easily convert Chinese text into a Pinyin string. Additionally, the Baidu Wenxin Yiyan API provides a convenient way to fetch random sentences.