fy

import os

import re

import requests

import hashlib

import json

import time

# 百度翻译的appid和密钥

appid = ‘20241028002188033’

secretKey = ‘YCBeEA3BEardixgP5nqR’

def baidu_translate(text, from_lang=’zh’, to_lang=’en’):

# 构建请求URL

url = “http://api.fanyi.baidu.com/api/trans/vip/translate”

# 构建请求参数

salt = str(int(time.time() // 60))

sign = hashlib.md5((appid + text + salt + secretKey).encode(‘utf-8’)).hexdigest()

params = {

‘q’: text,

‘from’: from_lang,

‘to’: to_lang,

‘appid’: appid,

‘salt’: salt,

‘sign’: sign,

}

# 发送请求

response = requests.get(url, params=params)

# 解析响应

result = response.json()

# 提取翻译结果

if ‘trans_result’ in result:

return result[‘trans_result’][0][‘dst’]

else:

return text # 如果翻译失败,返回原文本

def remove_specific_text(text):

# 删除特定的文本片段

text = text.replace(‘“`html’, ”)

text = text.replace(‘“`’, ”)

text = text.replace(‘

‘, ”)

text = text.replace(‘

‘, ”)

text = text.replace(‘At [Your Company Name], ‘, ”)

text = re.sub(r’

\s*’, ”, text)

text = re.sub(r’

\s*’, ”, text)

return text

def process_files(directory):

processed_count = 0

for filename in os.listdir(directory):

if filename.endswith(‘.txt’):

file_path = os.path.join(directory, filename)

with open(file_path, ‘r’, encoding=’utf-8′) as file:

content = file.read()

# 删除特定的文本片段

content = remove_specific_text(content)

# 翻译中文字符

def translate_match(match):

return baidu_translate(match.group())

content = re.sub(r'[\u4e00-\u9fff]+’, translate_match, content)

# 保存修改后的内容

with open(file_path, ‘w’, encoding=’utf-8′) as file:

file.write(content)

processed_count += 1

return processed_count

# 遍历当前目录下的所有txt文件并处理

directory = os.getcwd() # 获取当前工作目录

processed_files = process_files(directory)

print(f”Processed {processed_files} files.”)

Leave a Comment

Jou e-posadres sal nie gepubliseer word nie. Verpligte velde word met * aangedui