英语听写小软件

因为自己英语不太行,然后单词平常记忆老是忘记,但是觉得如果有听写的话,记忆力会大大提高,所以借用python写了一个小软件。

用到了python中的一个pyttsx3发音工具包。

实现过程很容易,下面就直接把放上来。

# -*- coding: utf-8 -*-
"""
Created on Tue Sep 25 18:11:09 2018

@author: C_hao
"""

import pyttsx3
import random
print("开始进行拼写,输入结束即停止程序")
print("拼写英文,请输入1")
print("拼写中文,请输入2")
temp=input("请输入选项:")
if temp=='1':
    wd=0
    trl=1
else:
    wd=1
    trl=0
f = open(r"english.txt",'r')        
line = f.readline()              
engine = pyttsx3.init()
content=[]
while line:
    line=str(line).strip()
    content.append(line.split('-'))
    line = f.readline()
f.close() 

while True:
    x=random.randint(0,len(content)-1)
    engine.say(content[x][wd])
    engine.runAndWait()
    word=input("请拼写:")
    word=word.strip()
    if content[x][wd]==word:
        print("拼写正确")
    elif word=="结束":
        break
    else:
        print("拼写错误")    
    translate=input("请翻译:")
    translate=translate.strip()
    if content[x][trl]==translate:
        print("翻译正确")
    elif translate=="结束":
        break
    else:
         print("翻译错误")


这是english.txt中里面文本的内容:

management-管理
hello-你好

 

下面写个.bat程序即可每次点击就可以运行。

python dication.py

 

以下是一个基于 Python 和 Google Cloud Speech-to-Text API 的简单英语听写软件的代码示例: ```python import tkinter as tk import pyaudio import wave import io import os from google.cloud import speech_v1p1beta1 as speech from google.cloud.speech_v1p1beta1 import enums # 设置 Google Cloud 服务账号文件路径 os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '/path/to/your/credentials.json' # 初始化 Google Cloud 语音识别客户端 client = speech.SpeechClient() # 定义录音参数 CHUNK = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 16000 RECORD_SECONDS = 5 WAVE_OUTPUT_FILENAME = "output.wav" class EnglishDictationApp: def __init__(self, master): self.master = master master.title("English Dictation App") # 创建录音和听写按钮 self.record_button = tk.Button(master, text="Record", command=self.record) self.record_button.pack() self.transcribe_button = tk.Button(master, text="Transcribe", command=self.transcribe, state=tk.DISABLED) self.transcribe_button.pack() # 创建输入框和答案框 self.input_text = tk.Text(master, height=5, width=50) self.input_text.pack() self.answer_text = tk.Text(master, height=5, width=50, state=tk.DISABLED) self.answer_text.pack() def record(self): # 创建 PyAudio 对象 audio = pyaudio.PyAudio() # 打开音频流 stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) print("Recording...") frames = [] # 录制音频 for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): data = stream.read(CHUNK) frames.append(data) print("Recording finished.") # 停止音频流 stream.stop_stream() stream.close() audio.terminate() # 保存音频文件 wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb') wf.setnchannels(CHANNELS) wf.setsampwidth(audio.get_sample_size(FORMAT)) wf.setframerate(RATE) wf.writeframes(b''.join(frames)) wf.close() # 激活听写按钮 self.transcribe_button.config(state=tk.NORMAL) def transcribe(self): # 读取音频文件 with io.open(WAVE_OUTPUT_FILENAME, 'rb') as audio_file: content = audio_file.read() # 创建语音识别请求对象 audio = speech.types.RecognitionAudio(content=content) config = speech.types.RecognitionConfig( encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16, sample_rate_hertz=RATE, language_code='en-US') # 发送语音识别请求 response = client.recognize(config, audio) # 获取识别结果 transcripts = [result.alternatives[0].transcript for result in response.results] # 显示识别结果 self.answer_text.config(state=tk.NORMAL) self.answer_text.delete(1.0, tk.END) self.answer_text.insert(tk.END, '\n'.join(transcripts)) self.answer_text.config(state=tk.DISABLED) # 启动应用 root = tk.Tk() app = EnglishDictationApp(root) root.mainloop() ``` 这个代码示例使用 PyAudio 库录制音频,使用 Google Cloud Speech-to-Text API 进行语音识别,使用 tkinter 库构建了一个简单的用户界面。当用户点击 "Record" 按钮时,程序会开始录制音频;当用户点击 "Transcribe" 按钮时,程序会使用 Google Cloud Speech-to-Text API 对录制的音频进行语音识别,并将识别结果显示在用户界面上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值