我的第八个项目:做一个web版停用词下载器
点击上方蓝色字体,关注程序员zhenguo
背景
什么是停用词?
停用词是在处理自然语言数据(或文本)之前或之后会自动过滤掉某些字或词,这些字或词即被称为
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.py
是app.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_file
和make_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,之前的所有项目代码如下图所示都放在文件夹里了:
八个项目的完整源代码
不用打赏,点个赞或在看
就心满意足了
关键词
文件
模块
语言
视图函数
核心
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。