#这个代码是可以给单词的txt文档,然后输出英文的发音
import os
import random as ra
import json
import requests
import time
# 声音播放
from playsound import playsound
file_name = 'dictation'
def down_mp3(word):
url = 'http://dict.youdao.com/dictvoice?type=0&audio=' + word
html = requests.get(url)
#file_name = 'dictation'
if not os.path.isdir(file_name):
os.mkdir(file_name)
#temp_row=os.path.join(file_name, 'sound_',word)+'.mp3'
#print(temp_row)
if not os.path.isfile('%s\\sound_%s.mp3' % (file_name, word)):
with open('%s\\sound_%s.mp3' % (file_name, word), 'wb') as f:
#print('%s\\111111sound_%s.mp3' % (file_name, word))
f.write(html.content)
'''
if not os.path.isfile(temp_row):
with open(temp_row, 'wb+') as f:
#with open(os.path.join(file_name, 'sound_',word), 'wb+') as f:
f.write(html.content)
'''
# 播放声音
def play(word):
down_mp3(word)
#playsound('words_sound\\audio_%s.mp3' % word)
playsound('%s\\sound_%s.mp3' % (file_name, word))
def read(filename):
lst=[]
lst_han=[]
#print(1)
with open(filename, 'r', encoding='utf-8') as f:
for line in f.readlines():
#line = line.replace('/', ' ')
#line = line.strip('\n.0123456789\t').split(' ')
line = line.strip('\n').split('1')
#print(len(line))
if line=='':
pass
lst.append(line[0])
lst_han.append(line[1])
#print(line)
#print(line[0])
#lst.append(line[0])
'''
if len(line)==2:
lst.append(line[0])
lst_han.append(line[-1])
else:
temp_word=''
for temp_num in range(len(lst)-2):
temp_word+=line[temp_num]
temp_word +=' '
print('aaa',temp_word)
lst.append(temp_word)
lst.append(line[-1])
'''
#print(lst)
for i in range(len(lst)):
play(lst[i])
for num in range(10):
a=input('输入0为再听一次,输入其单词为校对,输入回车下一个\n')
if len(a)>=2:
if a==lst[i]:
print('正确',lst[i],' ',lst_han[i])
else:
print('不正确')
else:
if a!='' and int(a)==0:
play(lst[i])
else:
print(lst[i], ' ', lst_han[i])
break
print('听写完了')
def main():
read('D:/雅思/tingxie/5.19.txt')
if __name__ == '__main__':
main()
英语听写的代码
最新推荐文章于 2021-08-19 23:10:25 发布