首页 > 解决方案 > 为什么python的文档生成器--sphinx搜索栏中没有cjk(chinese)词的搜索结果?

问题描述

我建立了一个简单的项目,它只包含一篇文章,标题是 yahoo股票历史数据下载,文件yahoo股票历史数据下载.rst,只包含一个问题:为何可以通过浏览器下载yahoo股票历史数据,无法使用wget curl下载? 英文句子字符串可以搜索。

在此处输入图像描述

使用 grep 命令搜索 cjk(中文)单词“股票”。

grep -n -r "股票"  myproject
/source/research/yahoo股票历史数据下载.rst:yahoo股票历史数据下载
/source/research/yahoo股票历史数据下载.rst:问题:为何可以通过浏览器下载yahoo股票历史数据,无法使用wget curl下载?
/build/html/research/yahoo股票历史数据下载.html:    <title>1. yahoo股票历史数据下载 &#8212; documents 1 documentation</title>
/build/html/research/yahoo股票历史数据下载.html:<h1>1. yahoo股票历史数据下载<a class="headerlink" href="#yahoo" title="Permalink to this headline">¶</a></h1>
/build/html/research/yahoo股票历史数据下载.html:<p>问题:为何可以通过浏览器下载yahoo股票历史数据,无法使用wget curl下载?</p>
/build/html/research/yahoo股票历史数据下载.html:<li class="toctree-l1 current"><a class="current reference internal" href="#">1. yahoo股票历史数据下载</a></li>
/build/html/research/yahoo股票历史数据下载.html:      <a href="../_sources/research/yahoo股票历史数据下载.rst.txt"

在python的文档生成器--sphinx搜索栏中搜索cjk(中文)单词“股票”。
在此处输入图像描述

可以看到,yahoo无论是标题还是内容中的第一个词,标题和内容中的第二个词都是股票要搜索的目标。
为什么您的搜索没有匹配任何文件?

标签: python-3.xsearchpython-sphinx

解决方案


安装jieba lib。

sudo pip install jieba

找到jieba字典文件。

sudo find /usr   -name  "jieba"
/usr/local/lib/python3.5/dist-packages/jieba
ls  /usr/local/lib/python3.5/dist-packages/jieba
analyse     dict.txt  __init__.py  posseg
_compat.py  finalseg  __main__.py  __pycache__

jieba 词典文件位于/usr/local/lib/python3.5/dist-packages/jieba/dict.txt.

编辑 sphinx 的 conf.py。

vim source/conf.py 
html_search_language = 'zh'
html_search_options = {'dict': '/usr/local/lib/python3.5/dist-packages/jieba/dict.txt'} 

重新编译项目目录中的所有文件。

cd  yourproject
make html

重启apache2。

sudo systemctl restart apache2 #maybe it is not neccessary.

现在可以搜索汉字了。


推荐阅读