点击上方蓝色字体,关注程序员zhenguo

你好,我是 zhenguo
这是我的第500篇原创
是我的第八个项目,实现一个web版常见语言的停用词下载器。
我的第七个项目:做一个web版记事本

背景

什么是停用词?
停用词是在处理自然语言数据(或文本)之前或之后会自动过滤掉某些字或词,这些字或词即被称为Stop Words(停用词)

项目环境

Python版本是3.7.11
主要基于flask开发,并使用其中下面的这些对象:
from
 flask 
import
 Flask, render_template, send_file, make_response

除此之外,还使用一个有意思的包:pypinyin,用来按照不同语言的拼音显示在web页面。
使用的内置模块有:
import
 os

from
 collections 
import
 OrderedDict

项目功能

打开终端窗口,切换到项目根目录下,输入下面一行命令:
flask run

启动后,在浏览器输入:
http://127.0.0.1:5000/

然后就会展示下面网页,部分截图如下:

实现框架

下面是项目的目录结构,如下所示:
stopwords_web

|____stopwords

|____.flaskenv

|____util.py

|____static

| |____images

| | |____bee.ico

| |____style.css

|____app.py

|____templates

| |____index.html

| |____base.html

|____stopwords.py

stopwords是放各种语言停用词的文件夹;
.flaskenv是flask的全局环境配置问价;
util.py是项目的基础py模块;
static是web资源文件夹;
app.py是项目的主模块;
templates是html模板文件;
stopwords.pyapp.py视图模块的主要业务处理逻辑

核心代码

核心模块包括stopwords.py,使用type函数动态创建类Result,逐个遍历文件夹stopwords中的停用词文件,并为Result类动态创建属性:
  • 此语言停用词数
  • 此语言前5个停用词例子
defdo_stopwords():
    result_dict = dict()

    Result = type(
'Result'
, (object,), dict(word_n=
0
, words=list()))

try
:

for
 file 
in
 os.listdir(
'stopwords'
):

if
 file == 
'.DS_Store'
:

continue
            res = Result()

            file_path = os.path.join(
'stopwords'
, file)

with
 open(file_path) 
as
 fr:

                stopwords = fr.readlines()

                res.word_n = len(stopwords)

                res.words = 
','
.join(map(
lambda
 word: word.replace(
'\n'
''
), stopwords[:
5
]))

                result_dict[eng2chi[file]] = res


return
 OrderedDict(sorted(result_dict.items(), key=
lambda
 x: lazy_pinyin(x[
0
])))

except
 FileNotFoundError 
as
 no_found:

raise
 no_found

注意一个细节,展示到页面时,各个语言排序按照拼音顺序,若拼音相同,再按照声调,若还相同按照笔画数和顺序,pypinyin包已实现此能力,调用lazy_pinyin函数,代码如下所示:
OrderedDict(sorted(result_dict.items(), key=lambda x: lazy_pinyin(x[0])))

第二个核心模块是app.py,一共两个视图函数。
第一个视图函数是index,实现主页显示的全部元素:
@app.route('/', methods=['GET'])
defindex():
    stop_words = do_stopwords()

return
 render_template(
'index.html'
, stop_words=stop_words)

第二个视图函数是download,路由/stopwords/download/<lang>中的参数lang是要下载停用词的语言。
send_filemake_response是Flask内置的函数,实现远程文件下载到本地:
@app.route('/stopwords/download/<lang>', methods=['POST'])
defdownload(lang):
for
 lang_i 
in
 os.listdir(
'stopwords'
):

if
 lang_i == chi2eng[lang]:

            path = os.path.join(
'stopwords'
, lang_i)

            response = make_response(send_file(path))

            response.headers[
"Content-Disposition"
] = 
f"attachment; filename={lang_i}.txt"
return
 response

配置headers,Content-Disposition属性为附件attachment,文件名为filename
f
'attachment; filename={lang_i}.txt'
index.html中以table标签展示各种语言的停用词,模板框架是Jinja
    {% 
for
 lang 
in
 stop_words%}

    <tr>

        <td>{{lang}}</td>

        <td>{{stop_words[lang].words}}</td>

        <td>{{stop_words[lang].word_n}}</td>

        <td>

            <form class=
"inline-form"
 method=
"post"
 action=
"{{ url_for('download', lang=lang) }}"
>

                <input class=
"btn"type
=
"submit"
 name=
"download"
 value=
"下载"
>

            </form>

        </td>

    </tr>

    {% endfor %}

项目测试

截止2021年1月23日,测试未发现bug。
完整代码下载
上面完整py代码文件,在我的公众号后台回复:c,之前的所有项目代码如下图所示都放在文件夹里了:
八个项目的完整源代码
长按关注,回复c
不用打赏,点个赞或在看

就心满意足了
继续阅读
阅读原文