英语听写的代码

#这个代码是可以给单词的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()

            

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值