import spacy
import os
nlp = spacy.load('zh_core_web_sm')
with open('input.txt', 'r', encoding='utf-8') as f:
text = f.read()
doc = nlp(text)
res = [sent.text.strip() for sent in doc.sents if sent.text.strip()]
idx = 1
txt_zn_folder = 'txt_zn/'
for content in res:
file_path = os.path.join(txt_zn_folder, str(idx) + '.txt')
idx += 1
with open(file_path, 'w') as f:
f.write(content)
二、通过百度翻译api翻译成英文
import requests
import hashlib
import random
import json
import os
import time
def translate(text):
url = 'http://api.fanyi.baidu.com/api/trans/vip/translate'
app_id = ''
app_key = ''
salt = random.randint(10000, 99999)
sign = hashlib.md5((app_id + text + str(salt) + app_key).en