2015年4月23日 星期四

Sphinx 正體中文搜尋的擴充套件

網路搜尋的資料都要修改 Sphinx 的程式碼,對用 pip 作套件管理的系統並不好。 參考 Sphinx 的擴充套件教學後,就基於 bosbyj/sphinx.search.zh_CN 加入了一些例外處理和從設定檔自訂字典檔的功能,做了正體中文的擴充套件 sphinx.ext.search.zh_TW 也提交給 Sphinx,不知道會不會 marge 到未來版本。

安裝結巴中文分詞
$ pip install jieba
安裝正體中文的擴充套件
$ git clone https://github.com/enhao/sphinx.ext.search.zh_TW.git _extension
修改專案目錄下的 conf.py
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
sys.path.insert(0, os.path.abspath('_extension'))

...

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = ['search-zh_TW']

...

# Language to be used for generating the HTML full-text search index.
# Sphinx supports the following languages:
#   'da', 'de', 'en', 'es', 'fi', 'fr', 'hu', 'it', 'ja'
#   'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr'
html_search_language = 'zh_TW'

# A dictionary with options for the search language support, empty by default.
# Now only 'ja' uses this config value
html_search_options = {'dict': '_extension/dict/zh.dict'}
sphinx.ext.search.zh_TW 的字典檔是用結巴中文分詞的 dict.txt.big ,不設定 html_search_options 的話,會使用結巴中文分詞的預設字典

參考

沒有留言:

張貼留言