php西游记源码,基于Python第三方插件实现西游记章节标注汉语拼音的方法
起因很单纯,就是给我1年级小豆包的女儿标注三国和西游章节的汉语拼音,我女儿每天都朗读 ,结果有很多字不认识,我爱人居然让我给标记不认识的完了手动注音......我勒个去......身为程序员的我怎么能忘记用程序实现呢,特别是咱也会点Python万能语言。哈哈!列举一下使用的技术。
语言:Python3.7
插件:pypinyin0.37.0 和 openpyxl 3.0.3
开发工具:pycharm 社区版
使用openpyxl操作execl的教程多的你无法想。
使用pypinyin将汉字转换成汉语拼音很简单,网络上API一大推。而且简单的不能再简单了,就一句话就实现了。分享点代码:
# 带声调的(默认)
def yinjie(word):
sentens = "".join(word.split())
print(sentens)
s = ''
# heteronym=True开启多音字
for i in pypinyin.pinyin(word, heteronym=False):
s = s + ''.join(i) + " "
return s这个就足够汉字转拼音了,不过我要求数据结构就没使用这个方法。我把数据结构说一下。
三层二维数组(这个非常关键):
第一层:单个汉字和汉语拼音构成。
['dì', '第'], ['yī', '一'], ['bǎi', '百'], ['huí', '回']第二层:按照标题空格分词。
[['dì', '第'], ['yī', '一'], ['bǎi', '百'], ['huí', '回']], [['jìng', '径'], ['huí', '回'], ['dōng', '东'], ['tǔ', '土']], [['wǔ', '五'], ['shèng', '圣'], ['chéng', '成'], ['zhēn', '真']]第三层:所有标题的集合。
就是第二层的合计,西游记就是100个章节标题集合。
最开始的成果物。这个不好对应也很难阅读。
基于Python第三方插件实现西游记章节标注汉语拼音的方法-1.png (10.88 KB, 下载次数: 0)
2020-7-12 11:45 上传
我爱人给了我一个参考事例。如下图:
基于Python第三方插件实现西游记章节标注汉语拼音的方法-2.png (102.27 KB, 下载次数: 0)
2020-7-12 11:45 上传
咱也不能示弱,咱也是程序员。咱也会万能的Python。
最开始的目标是将文字写入到word中,所以就用了Python-docx。汉语拼音长短不一这个很难对齐。想计算汉语拼音的长度进而计算汉字的位置......这个算法得多复杂,一个排版算法...我不是大神......
这个玩意其实和数学应用题一样,想到了其实一点也不难,就是弄个表格完了让汉语拼音和汉字居中不就得了。想到这个以后其实一点都不难了。使用Python-docx搞了好久有个问题就是竖版的word放不下汉字和汉语拼音。头疼啊。效果如下图:
基于Python第三方插件实现西游记章节标注汉语拼音的方法-3.png (29.61 KB, 下载次数: 0)
2020-7-12 11:45 上传
唉!难道是思路不对。。。
不用Python-docx了。使用openpyxl来操作execl。第一次成果物。看起来还可以。
基于Python第三方插件实现西游记章节标注汉语拼音的方法-4.png (10 KB, 下载次数: 0)
2020-7-12 11:45 上传
最终的成果物。
基于Python第三方插件实现西游记章节标注汉语拼音的方法-5.png (78.54 KB, 下载次数: 0)
2020-7-12 11:45 上传
转成PDF的成果物:
基于Python第三方插件实现西游记章节标注汉语拼音的方法-6.png (117.3 KB, 下载次数: 0)
2020-7-12 11:45 上传
继续鼓捣,最终搞定。。。一共花费了近6个小时,时间有点多。其实主要是数据结构和排版耽误时间,在有就是语法,作为Net出身而后转Java的人来说这个…&...就是并且的意思。然而在Python里面他不就是并且是and啊啊啊啊啊!因为这个我改了N多代码调试了好几次,唉深度无语。真是基础不牢地动山摇啊。别废话了上代码:
import pypinyin
from openpyxl import Workbook
from openpyxl.drawing.text import Font
from openpyxl.styles import Font, colors, Alignment
from pulgin.Tools import Tools
class HanZhiAddPinYin:
def __init__(self):
pass
def signWord(self,word):
pinyicontent = pypinyin.pinyin(word, heteronym=False)
word_pinyin = [pinyicontent[0][0], word]
return word_pinyin
def sentences(self,keyWords):
listsentense = []
for duanyu in keyWords.split():
print(duanyu)
duanyu_list = []
for word in duanyu:
duanyu_list.append(self.signWord(word))
listsentense.append(duanyu_list)
print(
listsentense
)
return listsentense
def articles(self,txt_file_path):
article = []
encoding = Tools.get_file_encoding(txt_file_path)
f = open(txt_file_path, "r", encoding=encoding, errors='ignore') # 返回一个文件对象
line = f.readline().strip() # 调用文件的 readline()方法
index = 1
while line:
article.append(self.sentences(line))
line = f.readline()
index = index + 1
f.close()
return article
def builder_execl(self,word_title, save_path, article):
"""
构建execl文件
:param word_title: sheet页面的名词
:param save_path: execl保存路径
:param article: 文章内容集合
:return:
"""
wb = Workbook()
ws = wb.active
ws.title = word_title
ws.sheet_properties.tabColor = "1072BA" # 设置背景
xl_sheet = wb.get_sheet_by_name(word_title)
execl_cell_width = 4.6
for sentences in article:
column_index = 1
# sentences 2行数据
# 获取行数
pinyin_row = xl_sheet.max_row + 1 # 拼音所在的行
hanzi_row = pinyin_row + 1 # 汉字所在的行
sentences_index = 0
for duanyu in sentences: # ['dì', '第'], ['yī', '一'], ['huí', '回']
for sign_word in duanyu: # ['dì', '第']
# region 设置样式
# 设置样式
execl_cell_font = Font(name='华文楷体', size=12, italic=False, color=colors.BLACK, bold=True)
execl_pinyin_row = xl_sheet.cell(row=pinyin_row, column=column_index)
execl_hanzi_row = xl_sheet.cell(row=hanzi_row, column=column_index)
execl_pinyin_row.alignment = Alignment(horizontal='center', vertical='center')
execl_hanzi_row.alignment = Alignment(horizontal='center', vertical='center')
execl_pinyin_row.font = execl_cell_font
execl_hanzi_row.font = execl_cell_font
xl_sheet.column_dimensions['A'].width = 3
xl_sheet.column_dimensions['B'].width = 3
xl_sheet.column_dimensions['C'].width = 3
xl_sheet.column_dimensions['D'].width = execl_cell_width
xl_sheet.column_dimensions['E'].width = execl_cell_width
xl_sheet.column_dimensions['F'].width = execl_cell_width
xl_sheet.column_dimensions['H'].width = execl_cell_width
xl_sheet.column_dimensions['I'].width = execl_cell_width
xl_sheet.column_dimensions['G'].width = execl_cell_width
xl_sheet.column_dimensions['J'].width = execl_cell_width
xl_sheet.column_dimensions['K'].width = execl_cell_width
xl_sheet.column_dimensions['L'].width = execl_cell_width
xl_sheet.column_dimensions['M'].width = execl_cell_width
xl_sheet.column_dimensions['N'].width = execl_cell_width
xl_sheet.column_dimensions['O'].width = execl_cell_width
xl_sheet.column_dimensions['P'].width = execl_cell_width
xl_sheet.column_dimensions['Q'].width = execl_cell_width
xl_sheet.column_dimensions['R'].width = execl_cell_width
xl_sheet.column_dimensions['S'].width = execl_cell_width
xl_sheet.column_dimensions['T'].width = execl_cell_width
xl_sheet.column_dimensions['U'].width = execl_cell_width
xl_sheet.column_dimensions['V'].width = execl_cell_width
xl_sheet.column_dimensions['W'].width = execl_cell_width
# endregion
xl_sheet.cell(row=pinyin_row, column=column_index, value=sign_word[0])
xl_sheet.cell(row=hanzi_row, column=column_index, value=sign_word[1]) # 0 第一百回 1 径回东土 2 五圣成真
# print(sentences_index)
# print(len(duanyu) + len(sentences[0]))
# print(column_index)
if sentences_index == 1 and len(duanyu) + len(sentences[0]) == column_index:#坑人的and
xl_sheet.cell(row=pinyin_row, column=column_index + 1, value=",")
xl_sheet.cell(row=hanzi_row, column=column_index + 1, value=",")
column_index = column_index + 1 # 遇到断句多增加一列向后
column_index = column_index + 1 # 列向后
sentences_index = sentences_index + 1 # 三个短语计数器
wb.save(save_path)
总结
到此这篇关于基于Python第三方插件实现西游记章节标注汉语拼音的方法的文章就介绍到这了,更多相关python第三方插件标拼音内容请搜索咔叽论坛以前的文章或继续浏览下面的相关文章希望大家以后多多支持咔叽论坛!
php西游记源码,基于Python第三方插件实现西游记章节标注汉语拼音的方法相关推荐
- (附源码)基于Python的“书怡”在线书店系统的设计与实现 毕业设计082332
Django书怡在线书店系统 摘 要 随着科学技术的飞速发展,社会的方方面面.各行各业都在努力与现代的先进技术接轨,通过科技手段来提高自身的优势,在线书店系统当然也不能排除在外.书怡在线书店系统是以实 ...
- python库源码分析_python第三方库Faker源码解读
源码背景 Faker是一个Python第三方库,GITHUB开源项目,主要用于创建伪数据创建的数据包含地理信息类.基础信息类.个人账户信息类.网络基础信息类.浏览器信息类.文件信息类.数字类 文本加密 ...
- 2022去/水印小程序源码+基于WordPress插件
正文: 2022去/水印小程序源码+基于WordPress插件,本版本代码是全开源的,基于Wordpress的插件开发的. 上传到Wordpress,安装插件,启动之后,绑定自己的小程序id即可,wo ...
- linux python源码安装,linux上源码安装python
以下例子基于python 2.7.9,其他版本同理.# 1.下载python# wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tg ...
- linux python3.8源码安装_linux 下从源码安装 Python——小白踩坑记
实验室服务器使用的系统为 Ubuntu 16.04,自带的 python 版本为 Python 2.7.12 和 Python 3.5.2,命令行下使用$ python命令来启动 python 时默认 ...
- python 百度云搜索引擎入口_2016最新百度云网盘搜索引擎源码,附带Python爬虫+PHP网站+Xunsearch搜索引擎...
源码简介 : 适用范围:百度云网盘 搜索引擎 源码,百度 搜索引擎 源码,网盘搜索 爬虫 源码 演示地址:(以截图为准) 运行环境:PHP.MYSQL 其他说明:分享的是一款 搜索引擎 源码,百度云盘 ...
- python百度云盘搜索引擎_2016百度云网盘搜索引擎源码,附带Python爬虫+PHP网站+Xunsearch搜索引擎...
品牌: 其他 语言: PHP 数据库: Mysql 源文件: 完全开源(含全部源文件) 授权: 免授权 规格: 整站源码 移动端: 无移动端 安装服务: 收费安装(另补差价) 操作系统: Window ...
- 网盘搜索引擎php源码,2016最新百度云网盘搜索引擎源码,附带Python爬虫+PHP网站+Xunsearch搜索引擎...
源码简介 : 适用范围:百度云网盘 搜索引擎 源码,百度 搜索引擎 源码,网盘搜索 爬虫 源码 演示地址:(以截图为准) 运行环境:PHP.MYSQL 其他说明:分享的是一款 搜索引擎 源码,百度云盘 ...
- php直播平台源码基于 Nginx 搭建(rtmp、http)直播服务器
php直播平台源码基于 Nginx 搭建(rtmp.http)直播服务器 直播协议介绍 国内常见公开的直播协议有几个:RTMP.HLS.HDL(HTTP-FLV).RTP,我们来逐一介绍. RTMP ...
- 全民直播时代——直播平台源码基于WebRTC开发实时通信服务
全民直播时代--直播平台源码基于WebRTC开发实时通信服务 摘要 本次分享基于 WEBRTC 技术的实时通信服务的开发经验,希望通过这次分享能让大家对这方面更有兴趣. 什么是互动直播? 互动直播是多 ...
最新文章
- psRobot:植物小RNA分析系统
- python学习手册翻译很差吗-为什么要翻译?值得坚持下去么?
- 设备树之GPIO和中断实例
- win2008服务器维护费用,win2008 服务器安全检查步骤指引(日常维护说明)
- spark的java源码,Spark源码包的编译
- 0基础lua学习(十八)C调用Lua----02Lua堆栈
- PHP在接下来的几年内将是主主流的
- E-prime脑电实验设计
- 2019 年我是怎么熬过来的?
- 关于 Pycharm 2019.2 版本出现等宽字体对不齐的问题的解决方法
- windows datacenter 2012 R2 密钥
- 使用OpenCV和Python进行人脸识别
- web浏览器上画图之raphael
- 服务器返回的14种常见HTTP状态码
- 【Multisim仿真】74LS194组成的8个led流水灯
- HikariCP数据库连接池,太快了!
- linux中释放sweep分区,详解Windows系统中如何释放C盘空间
- html5页面发送短信验证码,H5实现APP“输入短信验证码”效果
- 微信小程序关注公众号模板显示隐藏问题
- 哈尔滨工业大学2022计算机系统大作业