欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Python第三方插件實現(xiàn)西游記章節(jié)標注漢語拼音的方法

 更新時間:2020年05月22日 11:12:12   作者:人人都叫我漁歌  
這篇文章主要介紹了基于Python第三方插件實現(xiàn)西游記章節(jié)標注漢語拼音的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

      起因很單純,就是給我1年級小豆包的女兒標注三國和西游章節(jié)的漢語拼音,我女兒每天都朗讀 ,結(jié)果有很多字不認識,我愛人居然讓我給標記不認識的完了手動注音......我勒個去......身為程序員的我怎么能忘記用程序?qū)崿F(xiàn)呢,特別是咱也會點Python萬能語言。哈哈!列舉一下使用的技術(shù)。

語言:Python3.7

插件:pypinyin0.37.0  和 openpyxl 3.0.3

開發(fā)工具:pycharm 社區(qū)版

使用openpyxl操作execl的教程多的你無法想。

使用pypinyin將漢字轉(zhuǎn)換成漢語拼音很簡單,網(wǎng)絡(luò)上API一大推。而且簡單的不能再簡單了,就一句話就實現(xiàn)了。分享點代碼:

# 帶聲調(diào)的(默認)
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

這個就足夠漢字轉(zhuǎn)拼音了,不過我要求數(shù)據(jù)結(jié)構(gòu)就沒使用這個方法。我把數(shù)據(jù)結(jié)構(gòu)說一下。

三層二維數(shù)組(這個非常關(guān)鍵):

第一層:單個漢字和漢語拼音構(gòu)成。

['dì', '第'], ['yī', '一'], ['bǎi', '百'], ['huí', '回']

第二層:按照標題空格分詞。

 [['dì', '第'], ['yī', '一'], ['bǎi', '百'], ['huí', '回']], [['jìng', '徑'], ['huí', '回'], ['dōng', '東'], ['tǔ', '土']], [['wǔ', '五'], ['shèng', '圣'], ['chéng', '成'], ['zhēn', '真']]

第三層:所有標題的集合。

就是第二層的合計,西游記就是100個章節(jié)標題集合。

最開始的成果物。這個不好對應也很難閱讀。

我愛人給了我一個參考事例。如下圖:

咱也不能示弱,咱也是程序員。咱也會萬能的Python。

最開始的目標是將文字寫入到word中,所以就用了Python-docx。漢語拼音長短不一這個很難對齊。想計算漢語拼音的長度進而計算漢字的位置......這個算法得多復雜,一個排版算法...我不是大神......

這個玩意其實和數(shù)學應用題一樣,想到了其實一點也不難,就是弄個表格完了讓漢語拼音和漢字居中不就得了。想到這個以后其實一點都不難了。使用Python-docx搞了好久有個問題就是豎版的word放不下漢字和漢語拼音。頭疼啊。效果如下圖:

唉!難道是思路不對。。。

不用Python-docx了。使用openpyxl來操作execl。第一次成果物??雌饋磉€可以。

最終的成果物。

轉(zhuǎn)成PDF的成果物:

繼續(xù)鼓搗,最終搞定。。。一共花費了近6個小時,時間有點多。其實主要是數(shù)據(jù)結(jié)構(gòu)和排版耽誤時間,在有就是語法,作為Net出身而后轉(zhuǎn)Java的人來說這個…&...就是并且的意思。然而在Python里面他不就是并且是and啊啊啊啊啊!因為這個我改了N多代碼調(diào)試了好幾次,唉深度無語。真是基礎(chǔ)不牢地動山搖啊。別廢話了上代碼:

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() # 調(diào)用文件的 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):
    """
    構(gòu)建execl文件
    :param word_title: sheet頁面的名詞
    :param save_path: execl保存路徑
    :param article: 文章內(nèi)容集合
    :return:
    """
    wb = Workbook()
    ws = wb.active
    ws.title = word_title
    ws.sheet_properties.tabColor = "1072BA" # 設(shè)置背景
    xl_sheet = wb.get_sheet_by_name(word_title)
    execl_cell_width = 4.6
    for sentences in article:
      column_index = 1
      # sentences 2行數(shù)據(jù)
      # 獲取行數(shù)
      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 設(shè)置樣式
          # 設(shè)置樣式
          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 # 三個短語計數(shù)器
    wb.save(save_path)

總結(jié)

到此這篇關(guān)于基于Python第三方插件實現(xiàn)西游記章節(jié)標注漢語拼音的方法的文章就介紹到這了,更多相關(guān)python第三方插件標拼音內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論