Current Location: Home> Latest Articles> Sphinx PHP Chinese Word Segmentation and Retrieval Optimization Guide

Sphinx PHP Chinese Word Segmentation and Retrieval Optimization Guide

M66 2025-07-14

Introduction

With the rapid growth of the internet and the information explosion era, full-text search engines have become essential tools for information retrieval. Traditional full-text search engines are typically optimized for Western languages like English, but Chinese presents unique challenges. This article will introduce how to implement Chinese word segmentation and retrieval optimization using Sphinx PHP, along with specific code examples.

Chinese Word Segmentation

Chinese word segmentation is a crucial process in Chinese full-text search, which involves splitting Chinese text into independent words. Traditional full-text search engines use inverted indexes based on word frequency for searching. However, since a Chinese word often consists of multiple characters, segmentation is required.

Sphinx PHP provides a Chinese word segmentation extension called sphinxsegs, which can split Chinese text into independent words and supports custom dictionaries. Below is an example of how to use sphinxsegs for Chinese word segmentation:

<?php
$seg = sphinxsegs_initial();
sphinxsegs_setencoding($seg, 'utf-8');
sphinxsegs_setwordlist($seg, 'path/to/wordlist.dic');
$text = 'Chinese full-text search engine';
$result = sphinxsegs_segment($seg, $text);
print_r($result);
sphinxsegs_close($seg);
?>

In the code above, we first initialize the Chinese word segmenter using the sphinxsegs_initial function, then set the text encoding to UTF-8 using sphinxsegs_setencoding, and specify the custom dictionary file with sphinxsegs_setwordlist. Next, we specify the text to be segmented and use sphinxsegs_segment to perform the segmentation. Finally, we close the segmenter using sphinxsegs_close.

Retrieval Optimization

Chinese text retrieval has some unique issues, such as synonym handling and word weighting. To improve the recall and accuracy of Chinese full-text search, we need to optimize the retrieval process.

Sphinx PHP offers several features to optimize retrieval, including synonym replacement and weight adjustments. Below is an example of using Sphinx PHP for retrieval optimization:

<?php
require('sphinxapi.php');
$cl = new SphinxClient();
$cl->SetServer('localhost', 9312);
$cl->SetMatchMode(SPH_MATCH_EXTENDED2);
$cl->SetFieldWeights(array('title' => 10, 'content' => 1));
$keywords = 'Chinese full-text search engine';
$result = $cl->Query($keywords, 'index_name');
print_r($result);
if ($result && $result['total'] > 0) {
    foreach ($result['matches'] as $match) {
        echo 'ID: ' . $match['id'] . '; Weight: ' . $match['weight'] . '; Attributes: ' . $match['attrs']['title'] . PHP_EOL;
    }
}
?>

In this code, we first include the Sphinx PHP client library, sphinxapi.php, and create a SphinxClient object. Then, we set the Sphinx server's address and port, configure the match mode to SPH_MATCH_EXTENDED2, and set the field weights using SetFieldWeights. Next, we specify the search keywords and use the Query function to perform the search. Finally, we process the results returned by the $result variable.

Conclusion

This article introduced how to implement Chinese word segmentation and retrieval optimization using Sphinx PHP, with detailed code examples. By using the Chinese word segmentation and retrieval optimization features of Sphinx PHP, developers can significantly improve the performance of Chinese full-text search, leading to better recall and accuracy. We hope this article is helpful for developers working on Chinese full-text search applications.