38. Python批量翻译英语单词

Python批量翻译英语单词

用途:
对批量的英语文本,生成英语-汉语翻译的单词本,提供Excel下载

本代码实现:

  1. 提供一个英文文章URL,自动下载网页;
  2. 实现网页中所有英语单词的翻译;
  3. 下载翻译结果的Excel

涉及技术:

  1. pandas的读取csv、多数据merge、输出Excel
  2. requests库下载HTML网页
  3. BeautifulSoup解析HTML网页
  4. Python正则表达式实现英文分词

1. 读取英语-汉语翻译词典文件

词典文件来自:https://github.com/skywind3000/ECDICT
使用步骤:

  1. 下载代码打包:https://github.com/skywind3000/ECDICT/archive/master.zip
  2. 解压master.zip,然后解压其中的‪stardict.csv文件
import pandas as pd
# 注意:stardict.csv的地址需要替换成你自己的文件地址
df_dict = pd.read_csv("D:/tmp/ECDICT-master/stardict.csv")
d:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:3063: DtypeWarning: Columns (11) have mixed types.Specify dtype option on import or set low_memory=False.
  interactivity=interactivity, compiler=compiler, result=result)
df_dict.shape
(3402564, 13)
df_dict.sample(10).head()
wordphoneticdefinitiontranslationposcollinsoxfordtagbncfrqexchangedetailaudio
3370509WWDHNaNNaN[网络] 淇楄壋NaNNaNNaNNaNNaNNaNNaNNaNNaN
518014chauhtan (chotan)NaNNaN卓丹NaNNaNNaNNaNNaNNaNNaNNaNNaN
389953breviaristNaNNaN[网络] 短笛师NaNNaNNaNNaNNaNNaNNaNNaNNaN
951231electric-vehicleNaNNaNabbr. “EV”的变体;“electric car”的变体\n[网络] 电动汽车NaNNaNNaNNaNNaNNaNNaNNaNNaN
91258Albionianæl'biәniәnNaN[地质]阿尔比翁期NaNNaNNaNNaN0.00.0NaNNaNNaN
# 把word、translation之外的列扔掉
df_dict = df_dict[["word", "translation"]]
df_dict.head()
wordtranslation
0'ana. 一\nn. 英文字母表的第一字母;【乐】A音\nart. 冠以不定冠词主要表示类别\...
1'A' game[网络] 游戏;一个游戏;一局
2'Abbāsīyah[地名] 阿巴西耶 ( 埃 )
3'Abd al Kūrī[地名] 阿卜杜勒库里岛 ( 也门 )
4'Abd al Mājid[地名] 阿卜杜勒马吉德 ( 苏丹 )

2. 下载网页,得到网页内容

import requests
# Pandas官方文档中的一个URL
url = "https://pandas.pydata.org/docs/user_guide/indexing.html"
html_cont = requests.get(url).text
html_cont[:100]
'\n\n<!DOCTYPE html>\n\n<html xmlns="http://www.w3.org/1999/xhtml">\n  <head>\n    <meta charset="utf-8" />'

3. 提取HTML的正文内容

即:去除HTML标签,获取正文

from bs4 import BeautifulSoup
soup = BeautifulSoup(html_cont)
html_text = soup.get_text()
html_text[:500]
'\n\n\nIndexing and selecting data — pandas 1.0.1 documentation\n\n\n\n\n\n\n\n\n\n\n\n\nMathJax.Hub.Config({"tex2jax": {"inlineMath": [["$", "$"], ["\\\\(", "\\\\)"]], "processEscapes": true, "ignoreClass": "document", "processClass": "math|output_area"}})\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nHome\n\n\nWhat\'s New in 1.0.0\n\n\nGetting started\n\n\nUser Guide\n\n\nAPI reference\n\n\nDevelopment\n\n\nRelease Notes\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nIO tools (text, CSV, HDF5, â\x80¦)\n\n\nIndexing and selecting data\n\n\nMultiIndex / advanced indexing\n\n\nMerge, join, a'

4. 英文分词和数据清洗

# 分词
import re
word_list = re.split("""[ ,.\(\)/\n|\-:=\$\["']""",html_text)
word_list[:10]
['', '', '', 'Indexing', 'and', 'selecting', 'data', '—', 'pandas', '1']
# 读取停用词表,从网上复制的,位于当前目录下
with open("./datas/stop_words/stop_words.txt") as fin:
    stop_words=set(fin.read().split("\n"))
list(stop_words)[:10]
['',
 'itself',
 'showed',
 'throughout',
 'pointed',
 'n',
 'against',
 'name',
 'none',
 'ran']
# 数据清洗
word_list_clean = []
for word in word_list:
    word = str(word).lower().strip()
    # 过滤掉空词、数字、单个字符的词、停用词
    if not word or word.isnumeric() or len(word)<=1 or word in stop_words:
        continue
    word_list_clean.append(word)
word_list_clean[:20]
['indexing',
 'selecting',
 'data',
 'pandas',
 'documentation',
 'mathjax',
 'hub',
 'config',
 'tex2jax',
 'inlinemath',
 '\\\\',
 '\\\\',
 ']]',
 'processescapes',
 'true',
 'ignoreclass',
 'document',
 'processclass',
 'math',
 'output_area']

5. 分词结果构造成一个DataFrame

df_words = pd.DataFrame({
    "word": word_list_clean
})
df_words.head()
word
0indexing
1selecting
2data
3pandas
4documentation
df_words.shape
(4915, 1)
# 统计词频
df_words = (
    df_words
    .groupby("word")["word"]
    .agg(count="size")
    .reset_index()
    .sort_values(by="count", ascending=False)
)
df_words.head(10)
wordcount
620df161
659dtype87
1274true86
593dataframe80
1038pd75
917loc72
970nan72
721false58
914list58
835indexing53

6. 和单词词典实现merge

df_merge = pd.merge(
    left = df_dict,
    right = df_words,
    left_on = "word",
    right_on = "word"
)
df_merge.sample(10)
wordtranslationcount
658teamn. 队, 组\nvt. 把马(牛)套在同一辆车上, 把...编成一组\nvi. 驾驶卡车, 协作3
523providingconj. 以...为条件, 假如1
394linesn. 台词1
118columns塔器49
136conformsv. 遵守( conform的第三人称单数 ); 顺应; 相一致; 相符合1
529pythonn. 大蟒, 巨蟒\n[计] Python 程序设计语言;人生苦短,我用 Python26
185determinev. 决定, 决心1
285forwarda. 向前的, 早的, 迅速的, 在前的, 进步的\nvt. 促进...的生长, 转寄, 运...1
49argumentsn. 参数3
564reporteda. 报告的;据报道的1
df_merge.shape
(718, 3)

7. 存入Excel

df_merge.to_excel("./38. batch_chinese_english.xlsx", index=False)

后续升级:

  1. 可以提供txt/excel/word/pdf的批量输入,生成单词本;
  2. 可以做成网页、微信小程序的形式,在线访问和使用
  3. 用户可以标记或上传“已经认识的词语”,每次过滤掉
  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值