首页 > 编程语言 >python-手动借助google翻译来翻译文档

python-手动借助google翻译来翻译文档

时间:2023-05-08 18:57:08浏览次数:42  
标签:index 翻译 google python list html str english txt

  1 import os
  2 import re
  3 '''
  4 读取指定的html文件
  5 去掉所有的换行符
  6 正则匹配特定项目:(?<=<div class="block">).+?(?=</div>)
  7     然后替换掉:</code>|<code>|<i>|</i> ==> ""
  8                 <sup> ==> "^"
  9                 </sup> ==> ""
 10 输出取得的项目到文件中
 11 手工翻译并按行形成一个新文本(此时程序未结束运行,等待指令)
 12 直接回车(读取默认路径的文本文件)或者输入文件绝对路径
 13 读取结果文件,先检查行数是否一致。
 14 没问题就逐行查找替换
 15 然后格式化html内容,输出到源文件
 16 '''
 17 
 18 english_str_list = list();
 19 Label_list=list();                      #记录标签,防止因为标签的原因,导致html解析不正常,就是说,源文档提取出来的里面有多少标签,翻译出来的里面,最终写入的也要有那么多标签,至于文档可读性再说,解析都解析不出来,读个锤子
 20 chinese_str_list = list();
 21 html_txt = ""   #保存html文件内容
 22 english_out_file_path = "english.txt"   #保存需要翻译的内容
 23 temp_html_out_file_path = "temp_html.txt"   #保存中间处理过程中的html内容
 24 chinese_out_file_path = "翻译结果.txt"   #保存需要翻译的内容
 25 html_file_path = '' #程序运行后,设置该值,保存修改内容时需要使用
 26 
 27 '''
 28 从指定的文件中读取html内容并提取要翻译的内容
 29 '''
 30 def english_process(file_path:str):
 31     global html_txt,english_str_list
 32     with open(file_path,'r',encoding="utf-8")as fd:
 33         html_txt = fd.readlines()
 34         html_txt="".join(html_txt)
 35     html_txt = html_txt.replace("\n","")
 36     regex_div_block = r'(?<=<div class="block">).+?(?=</div>)'
 37     regex_del = r'</code>|<code>|<i>|</i>'
 38     html_txt = re.sub(regex_del,"",html_txt)
 39     html_txt = html_txt.replace("<sup>","^").replace("</sup>","")
 40     #提取
 41     result = re.findall(regex_div_block,html_txt)
 42     class_number = html_txt.count(r'<div class="block">')
 43     if(class_number != len(result)):
 44         print(r'<div class="block">'+"的数量为"+str(class_number)+",但是正则匹配的数量为:"+str(len(result)))
 45     else:
 46         print(r'<div class="block">'+"的数量为"+str(class_number))
 47     if(len(result)):
 48         for txt in result:
 49             english_str_list.append(txt)
 50     regex_div_deprecation_block = r'(?<=<div class="deprecation-block">).+?(?=</div>)'
 51     result = re.findall(regex_div_deprecation_block,html_txt)
 52     class_number = html_txt.count(r'<div class="deprecation-block">')
 53     if(class_number != len(result)):
 54         print(r'<div class="deprecation-block">'+"的数量为"+str(class_number)+",但是正则匹配的数量为:"+str(len(result)))
 55     else:
 56         print(r'<div class="deprecation-block">'+"的数量为"+str(class_number))
 57     if(len(result)):
 58         for txt in result:
 59             english_str_list.append(txt)
 60     regex_div_notes = r'(?<=<dl class="notes">).+?(?=</dl>)'
 61     result = re.findall(regex_div_notes,html_txt)
 62     class_number = html_txt.count(r'<dl class="notes">')
 63     if(class_number != len(result)):
 64         print('<dl class="notes">'+"的数量为"+str(class_number)+",但是正则匹配的数量为:"+str(len(result)))
 65     else:
 66         print(r'<dl class="notes">'+"的数量为"+str(class_number))
 67     if(len(result)):
 68         for txt in result:
 69             english_str_list.append(txt)
 70     if( not len(english_str_list)):
 71         print("没有匹配到任何要翻译的内容,请检查!")
 72         exit(0);
 73     #替换<>字符
 74     html_txt = html_txt.replace("&lt;","<").replace("&gt;",">")
 75     for index in range(len(english_str_list)):
 76         english_str_list[index] = english_str_list[index].replace("&lt;","<").replace("&gt;",">")
 77     #输出
 78     with open(english_out_file_path,'w',encoding="utf-8")as fd:
 79         for content in english_str_list:
 80             print(content,file=fd)
 81     #记录标签
 82     regex_label = r'<.+?>|\&lt;.+?\&gt;'
 83     for content in english_str_list:
 84         Label_list.append( re.findall(regex_label,content));
 85 '''
 86 翻译结果的标签处理
 87 '''
 88 def label_process(index:int):
 89     global Label_list,chinese_str_list
 90     #替换<>字符,理论上没有
 91     chinese_str_list[index] = chinese_str_list[index].replace("&lt;","<").replace("&gt;",">")
 92     size = len(chinese_str_list[index])
 93     result_str=""
 94     str_index=0
 95     Label_list_index=0
 96     while str_index<size:
 97         temp_str =""
 98         if('<' == chinese_str_list[index][str_index]):
 99             temp_str+=chinese_str_list[index][str_index]
100             str_index = str_index + 1
101             while '>' != chinese_str_list[index][str_index]:
102                 temp_str+=chinese_str_list[index][str_index]
103                 str_index = str_index + 1
104             temp_str+=chinese_str_list[index][str_index]
105             str_index = str_index + 1
106             if(Label_list_index<len(Label_list[index])):
107                 if(Label_list[index][Label_list_index] == temp_str):    #这里抛出越界异常的话,请查看翻译结果文档,和英文文档对应的行比对下,处理翻译后多余的内容后重新运行脚本。
108                     #标签一样
109                     Label_list_index += 1   #下次该比对下一个标签
110                     result_str+=temp_str
111                     temp_str = ""
112                 else:
113                     #标签不一样
114                     '''
115                         若只是有多余空格或者多、少了某个字符怎么办?
116                         若是就是缺失了原标签该怎么办
117                         目前暂时就在当前插入这个标签
118                     '''
119                     result_str+=Label_list[index][Label_list_index]
120                     Label_list_index += 1
121             else:
122                 break;
123         else:
124             result_str += chinese_str_list[index][str_index]
125             str_index+=1
126     chinese_str_list[index] = result_str
127 
128 '''
129 将翻译结果填充到原来的位置
130 '''
131 def chinese_process(file_path:str):
132     global html_txt,chinese_str_list
133     with open(file_path,'r',encoding="utf-8")as fd:
134         chinese_str_list = fd.readlines()
135     if(len(chinese_str_list)==len(english_str_list)):
136         for i in range(len(english_str_list)):
137             chinese_str_list[i] = re.sub(r"<\s+","<",chinese_str_list[i])   #< li>
138             chinese_str_list[i] = re.sub(r"\s+>",">",chinese_str_list[i])   #<li >
139             chinese_str_list[i] = re.sub(r'([^<])a href',r"\1<a href",chinese_str_list[i])  #你a href
140             chinese_str_list[i] = re.sub(r'(?<=</)\s+',r"",chinese_str_list[i])      #</ xxx>这种形式的要去除掉
141             #对付<a href="../../../java.base/java/util/package-summary.html#CollectionsFramework">Java 集合框架</的成员一个>。这种情况
142             chinese_str_list[i] = re.sub(r'<([a-zA-Z]+?)(\s*[a-zA-Z"/=#\?\$\. -]+?>[^</>].+?)</([^a-zA-Z]+)>',r"<\1\2</\1>\3",chinese_str_list[i])
143             label_process(i)    #这步调用要在常规处理最后以及在写入之前
144             if("</a>" in english_str_list[i]):
145                 #这种翻译的可能稀碎,需要把原文也加上去
146                 #原先直接将原文加上去,这样就会导致问题:文档里面的重复字符串怎么办?全部替换的话,可能导致其他字符串不全,替换一次,那么后面的重复串就不能被翻译了
147                 #html_txt = html_txt.replace(english_str_list[i],chinese_str_list[i] + r"<p>原文:<p>" + english_str_list[i],1)
148                 temp_str = ""
149                 for char in english_str_list[i]:
150                     if(" " == char):
151                         temp_str += "  "
152                     else:
153                         temp_str += char
154                 html_txt = html_txt.replace(english_str_list[i],chinese_str_list[i] + r"<p>原文:<p>" + temp_str,1)
155             else:
156                 html_txt = html_txt.replace(english_str_list[i],chinese_str_list[i],1)
157     else:
158         print("待翻译的行数和翻译结果文件中的行数不一致,请检查!")
159         return
160 '''
161 一些额外的替换
162 '''
163 def other_translate():
164     global html_txt
165     html_txt = html_txt.replace(r"<h2>Field Summary</h2>",r"<h2>Field Summary(字段摘要)</h2>").replace(r'<div class="caption"><span>Fields</span></div>',r'<div class="caption"><span>Fields(字段)</span></div>').replace(r'<div class="table-header col-first">Modifier and Type</div>',r'<div class="table-header col-first">Modifier and Type(修饰符和类型)</div>').replace(r'<h2>Constructor Summary</h2>',r'<h2>Constructor Summary(构造函数摘要)</h2>').replace(r'<h2>Method Summary</h2>',r'<h2>Method Summary(函数概要)</h2>').replace(r'<h2>Field Details</h2>',r'<h2>Field Details(字段细节)</h2>').replace(r'<h2>Constructor Details</h2>',r'<h2>Constructor Details(构造函数详细信息)</h2>').replace(r'<h2>Method Details</h2>',r'<h2>Method Details(函数细节)</h2>').replace(r'class="table-tab">Concrete Methods</button>',r'class="table-tab">Concrete Methods(具体函数)</button>')
166 
167 html_file_path = input("请输入html文件所在位置:\n");
168 english_process(html_file_path);
169 #输出现在的html内容,以供检查
170 with open(temp_html_out_file_path,'w',encoding="utf-8")as fd:
171     print(html_txt,file=fd);
172 print("要翻译的内容已经输出到"+os.getcwd()+"  目录中,名称为:"+english_out_file_path)
173 temp_str = input("请输入翻译结果文件所在位置,不输入直接回车则使用默认位置的文件:\n");
174 if(len(temp_str)):
175     chinese_out_file_path = temp_str
176 chinese_process(chinese_out_file_path);
177 other_translate();
178 #输出翻译结果
179 with open(html_file_path,'w',encoding="utf-8")as fd:
180     print(html_txt,file=fd);
181 '''
182 有时候会不小心造成替换后的文档不可滑动,那是因为<div class="flex-content">对应的闭合标签</div>被提前了,找到“<div class="flex-content">”对应的闭合标签,将其移动到在</body>标签前即可。
183 '''
View Code

java官方文档全英文。反正就那样吧

 

标签:index,翻译,google,python,list,html,str,english,txt
From: https://www.cnblogs.com/love-DanDan/p/17382825.html

相关文章

  • Python基础面试题
    1、Python和Java、PHP、C、C#、C++等其他语言的对比?'''1.C语言,它既有高级语言的特点,又具有汇编语言的特点,它是结构式语言。C语言应用指针:可以直接进行靠近硬件的操作,但是C的指针操作不做保护,也给它带来了很多不安全的因素。C++在这方面做了改进,在保留了指针操作的同时又增强......
  • Python面向对象面试题
    1、简述面向对象的三大特性。#答案封装: 封装指的是把一堆数据属性与方法数据放在一个容器中,这个容器就是对象。让对象可以通过"."来调用对象中的数据属性与方法属性。继承: 继承指的是子类可以继承父类的数据属性与方法属性,并可以对其进行修改或使用。多......
  • Python网络并发面试题
    1、python的底层网络交互模块有哪些?#答案:'''socket,urllib,urllib3,requests,grab,pycurl'''2、简述OSI七层协议。#答案:'''应用层:HTTP,FTP,NFS表示层:Telnet,SNMP会话层:SMTP,DNS传输层:TCP,UDP网络层:IP,ICMP,ARP,数据链路层:Ethernet,PP......
  • Python模块面试题
    1.列举常用的模块。基础:os,sys,time,datetime,json,pickle,randon,hashlib,re,math,logging爬虫:requests,BeautifulSoup,xpath,gevent,asyncio,twisted数据分析:pandas,numpy,scipy,matplotlib,seaborn等。。。2.如何安装第三方模块?pip3install模块名称3.re的ma......
  • Python设计模式面试题
    单例模式1请手写一个单例#encoding=utf8importthreadingimporttime#这里使用方法__new__来实现单例模式classSingleton(object):#抽象单例def__new__(cls,*args,**kw):ifnothasattr(cls,'_instance'):orig=super(Singleton,cls)......
  • python selenium 元素定位方法
    1.通过元素的id属性进行元素定位,在html中元素的id是唯一的定位方法:find_element_by_id(id)fromseleniumimportwebdriver#创建WebDriver对象,指明使用chrome浏览器驱动wd=webdriver.Chrome()#调用WebDriver对象的get方法可以让浏览器打开指定网址wd.get('ht......
  • python 打包
    1,单文件打包pyinstaller-F-wfile.py-F:-w::启动exe文件,不显示控制台2,多文件打包 文件结构:testmain.pyset.py如果使用:pyinstaller-F-wmain.py发生问题:相互依赖的文件,打包后无法引用,导致报错可以使用:pyinstaller-Dmain.py-pset.py假......
  • Python绘制雷达图
    基本步骤创建一个axe对象,创建时设置参数polar为True以使用极坐标系绘制图像示例代码frommatplotlibimportpyplotaspltax=plt.subplot(111,polar=True)#设置绘制极坐标xs=['a','b','c','d','e','a']ys=[1,2,4,3,1,1......
  • 肉身布莱特-感知机python实现
    一、代码:做线性拟合importnumpyasnpfrommatplotlibimportpyplotasplt#很简单的一个体积增大和毒气量的关系拟合virulence=np.random.rand(100)volume=np.random.rand(100)virulence.sort()volume.sort()plt.scatter(volume,virulence)w=0b=0alp......
  • 用Python语言进行时间序列ARIMA模型分析
    应用时间序列时间序列分析是一种重要的数据分析方法,应用广泛。以下列举了几个时间序列分析的应用场景:1.经济预测:时间序列分析可以用来分析经济数据,预测未来经济趋势和走向。例如,利用历史股市数据和经济指标进行时间序列分析,可以预测未来股市的走向。2.交通拥堵预测:时间......