Python learning practice | correct method of speech intelligent speech module

Time:2020-9-29

Recently, the rhythm of life and study is easy to be interrupted. Finally, today, after the internship, the summer camp is over.

A few days ago, when a person was very tired of reviewing, he picked up Python again and looked at a lot of knowledge points. What a deliberate thought! Speaking of this. By the way, I’d like to recommend a complete set of resources for Python learning Seven clothes nine seven seven bar five (Digital homophony) conversion can be found, here are senior programmers to share previous learning experience, learning notes, as well as work experience of first-line enterprises, and carefully collate a copy of Python zero foundation to project actual combat materials, explain the latest technology of python, prospects, learning need to leave a message of small details

Environmental preparation
One ️⃣ Python2. * or python3*

Two ️⃣ Install pywin32 extension library

Three ️⃣ Install speech module

Installing Python 2 / 3
As for the choice between installing Python 2 and python 3, I personally recommend installing both. When using it in some places, you need to switch the environment variables a little more.
Of course, in terms of the length of time that Python 3 has been launched, for beginners, it is recommended that they first choose to install Python 2, be familiar with some syntax structure definitions of python, and should be more comfortable to use.
After all, python 2 has been launched for a longer time, and there are some problems and solutions available for searching. At the same time, there are still some module functions that can not adapt to Python 3 or higher versions of Python 3 *.
The specific installation process is not detailed. No matter which version is almost the same, it is easy to get started, which is also one of the features of Python
After the python environment is installed, it is also a programming language. It is essential to choose an appropriate ide. Here, I recommend reading Python ide. For more Python ides, please refer to: http://wiki.python.org/moin/PythonEditors
PyCharm IDE
Among many ides, pycharm is the most popular one. As a member of JetBrains family, this software has perfect functions, such as debugging, syntax highlighting, project management, code jump, intelligent prompt, automatic completion, unit test and even version control. In addition, it also provides some good features for Django development, supports Google App Engine, and even cooler, pychar supports ironpython.

Official download address: http://www.jetbrains.com/pycharm/download/

IntelliJ idea registration code: http://idea.lanyus.com/ And the crack help document provided

Idea series theme download address: http://www.riaway.com/theme.php The supported ides for these topics include intelij idea, phpstorm, pychar, rubymine, webstorm and appcode.

Note: how to install the download theme

Open your editor from the main menu, select File > Import setting, and select the jar file you downloaded;
Wait for the configuration after restart: Open File > Settings > editor > colors and fonts, and then select the theme you want to install.
Install pywin32 extension library
Pywin32 is Python for windows extensions, which provides pyhton interface to access and call the underlying functions of windows. Pywin32 includes modules such as win32API, win32com, win32gui and win32process

Download address: https://sourceforge.net/projects/pywin32/files/pywin32/Build%20221/

This will be downloaded and installed according to the python Version (2. * / 3. *) and CPU bits (32-bit / 64 bit).

For example, in my case, you need to install pywin32-221.win-amd64-py3.7.exe

Install speech module
It can be installed by using the PIP install speech command, but for users who have installed Python 3, it is necessary to modify the configuration file after installation according to some new features of Python 3

As for PIP here, it is a python package management tool, which provides the functions of finding, downloading, installing and unloading Python packages;
At the same time, Python 2.7.9 + and python 3.4 + and above have their own PIP tools. As long as the option is not checked during installation, there is no need to install them specially. You can judge whether they have been installed by using the command PIP — version;
PIP official website download address: https://pypi.org/project/pip/ Note that pip is associated with which version of Python is used to run the installation script;
It is worth noting that some Linux distributions can install PIP directly with the package manager, such as Debian and Ubuntu: sudo apt get install Python pip.

PIP common related instructions
Function instruction
Display version and path PIP — version
Get help PIP — help
Upgrade PIP PIP install – U pip
Install the latest version package PIP install somepackage
Install the specified version of PIP install somepackage = = 1.0.4
Minimum version: PIP install ‘somepackage > = 1.0.4’
Upgrade package PIP install — upgrade somepackage
Upgrade to the specified package using = =, >=
Uninstall package PIP install somepackage
PIP search somepackage
Display installation package information PIP show
View the details of the specified package PIP show – F somepackage
List installed packages PIP list
View upgradeable packages PIP list – O
Note: if there is a problem with the upgrade PIP command above, you can use the following command: sudo easy_ install –upgrade pip

Solutions to normal use of speech in Python 3 *
speech’s Project description

After installing the speech module, it needs to be modified speech.py File, the file path is under.. \ Python 37 ﹣ lib ﹣ site packages

Line59 modify import thread to import threading;
Line157 modify print prompt to print (prompt);
For the last function_ ensure_ event_ The thread is modified as follows:
class T(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
pass

def _ensure_event_thread():
“””
Make sure the eventthread is running, which checks the handlerqueue
for new eventhandlers to create, and runs the message pump.
“””
global _eventthread
if not _eventthread:
def loop():
while _eventthread:
pythoncom.PumpWaitingMessages()
if _handlerqueue:
(context,listener,callback) = _handlerqueue.pop()
# Just creating a _ListenerCallback object makes events
# fire till listener loses reference to its grammar object
_ListenerCallback(context, listener, callback)
time.sleep(.5)
_eventthread = T()
_eventthread.start()
Intelligent speech automatic word reading script
A few days ago, a friend shared with me a document about computer related English vocabulary. After receiving it, it has been put on the computer desktop. I don’t know if it is useful.

This afternoon, as the recent summer camp activities have been communicating with AI related research teams, I suddenly thought that we could use the existing Python voice expansion package to implement an automatic (not to mention intelligent) mode similar to “classroom dictation”.

The specific source program can also be seen below, the whole program structure is very simple, also did not spend much time, I believe it is not difficult to understand.

It is worth noting that:

The sample words are all related to computer science. If you want to change them into other words, you just need to modify them test.csv File is OK. Among them, test.csv There are two columns of data in the document, which represent the English vocabulary and the corresponding Chinese interpretation. To realize the function of Chinese English translation, it is necessary to change the order of these columns.

Similar to the “classroom dictation” mode, there is an interval between each time_ Time time output words according to line pronunciation twice (during dictation, teachers usually read 2-3 times at intervals), and then, each dictation group (loop)_ They will be displayed together (in Chinese and English), so as to see how high their “correct rate” is.

In fact, we have done a lot of understanding and experiments on which intelligent voice module to choose

At the beginning, I planned to use pyttsx (Python 3 seems to use pyttsx3). During this period, I encountered many problems. Especially for Python 3, I can only search more about the Chinese speech recognition problems of pyttsx and the way to explore, pyttsx3 – text to speech x-platform, py Library: text to speech x-platform, py Library: text to speech (pywin32, pyttsx)
After that, pydub also came into contact with. Pydub needs to rely on libav or ffmpeg. Recommended reading: Fifty tone Dictation: Python audio processing library pydub, python audio processing library: pydub.
Now that I’ve talked about this, I’d like to talk more about audio, such as how to play MP3, WAV, and Ogg audio files in Python
4.1 call the system default player to play

import time
import os
file = r’F:/Test/musicT/Hello.mp3′
os.system(file)
time.sleep(10)
4.2 pyGame plays, but there is a lack of speech speed distortion. PyGame provides two methods to load music files
4.2.1 pygame.mixer.Sound , mainly loading Ogg and wav audio files.
4.2.2 pygame.mixer.music , mainly loading MP3 audio files.

import time
import pygame
file = r’F:/Test/musicT/Hello.mp3′
pygame.mixer.init()
track = pygame.mixer.music.load(file)
pygame.mixer.music.play()
time.sleep(10)
pygame.mixer.music.stop()
4.3 mp3play plays at normal speed, but it seems that it can only be used in python2 *. And does not support python3*

import time
import mp3play

def playmusic(path):
clip = mp3play.load(path)
clip.play()
time.sleep(10)
clip.stop()

file = r’F:/Test/musicT/Hello.mp3′
playmusic(file)
In the process of programming, there are the following problems. The solution to this situation is to find and end the intelligent voice process under task management

 

source program
Of course, you can also go to download latest version on GitHub, emmm… Thank you in advance for your star A kind of

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/7/27 18:01
# @Author : zhouie
# @File : main.py
# @Software: PyCharm

import speech
import time
import csv

Data = [] ා temporary storage of data per line of *. CSV file
Interval_ Time = 4 ා interval time between two words
LOOP_ Num = 8 ා cyclic cardinality
Think_ Time = 15 ා review waiting time

csv_file = open(‘./res/test.csv’, encoding=’utf-8′)
csv_reader_lines = csv.reader(csv_file)
# print(csv_reader_lines)

num = 0
for one_line in csv_reader_lines:
data.append(one_line)
num = num + 1

speech.say (computer related English word translation test program, demo version)
speech.say(“This is a small routine (compiled by Python) for exercise about English phrases in the field of computer”)

i = 0
while i < num:
# print(i + 1, data[i][0])
# speech.say(i + 1)
# speech.say(data[i][0])
# time.sleep(Interval_Time)
# speech.say(data[i][0])
if 0 == (i + 1) % LOOP_NUM:
speech.say (let’s review the above words.)
speech.say(“Just follow me , look back on the words you have learned…”)
print(“!–#######–*–#######–!”)
Print (I / loop)_ Num) + 1, “group Vocabulary:)
for j in range(i – (LOOP_NUM – 1), i + 1):
print(data[j][0], data[j][1])
speech.say(data[j][0])
speech.say(data[j][1])
print(“!–#######–*–#######–!”)
speech.say “How accurate are you? “”
speech.say(“So , What about your correct rate?”)
time.sleep(Think_Time)
i = i + 1

speech.say(“Congratulation!”)

Effect display: video demo address: https://v.qq.com/x/page/o0737zviriw.html
Finally, I would like to recommend a full range of resources for Python learning Seven clothes nine seven seven bar five (Digital homophony) conversion can be found, here are senior programmers to share previous learning experience, learning notes, as well as work experience of first-line enterprises, and carefully collate a copy of Python zero foundation to project actual combat materials, explain the latest technology of python, prospects, learning need to leave a message of small details

The text and pictures of this article are from the Internet and my own ideas. They are for study and communication only. They do not have any commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for handling.

Recommended Today

Understand mybatis step by step through the project

Reprint please be sure to indicate the source, original is not easy! Related articles:Understand mybatis < 1 > step by step through the project All code address of the project:Github-Mybatis Mybatis solves the problem of JDBC programming 1. The frequent creation and release of database links results in the waste of system resources, which affects […]