In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use Python code to achieve text-to-speech". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Python code to realize text-to-speech".
Foreword:
Development environment: Windows
Python version: 3.x
External module preparation: pygame (pip install pygame can be installed directly in the cmd command line)
Chinese characters to pinyin
I use to convert Chinese characters into Unicode codes, and then query a matching file (I use unicode_py.txt) to get the pinyin of the Chinese characters, which lists the pinyin corresponding to the Unicode code of the 4E00-9FA5 standard Chinese characters, plus a single 3007 code "0".
The documents are as follows:
After uppercase Pinyin, the words 1, 2, 3, 4 and 5 denote one, two, three, four and soft tones respectively (notice the presence of polysyllabic words)
We encapsulate it into a function with an argument of a string full of Chinese characters and a Pinyin string returned. (that is, chinese_to_pinyin returns "QIU1 SHUI3 GONG4 CHANG2 TIAN1 YI1 SE4")
The source code is as follows
Def chinese_to_pinyin (x): y =''dic = {} with open ("unicode_py.txt") as f: for i in f.readlines (): dic [i.split () [0]] = i.split () [1] for i in x: I = str (i.encode (' unicode_escape')) [- 5 i.encode ('unicode_escape')] .upper () try: Y + = dic [I] +''except: y + =' XXXX'# illegal characters we use XXXX instead of return y
Note: put this file and "unicode_py.txt" file in the same directory, the code is relatively simple, because we mainly talk about the implementation of teaching, so there is no processing of multi-tone words (so the first most frequently used tone is used by default)
Pinyin to speech
This step is also relatively simple, we can call the corresponding audio file of a single tone according to Pinyin. Audio files with a single tone can be recorded by themselves or by someone, and there are many channels. Pay attention to the file format, preferably mp3 or wav files.
Some of the documents are shown as follows:
We put it under a voice folder. The preparatory work is done.
We also encapsulate the Chinese character to voice into a make_voice function, the parameter is a Chinese character string, and then call the chinese_to_pinyin function, the return value is not important, can be empty, the function is to call the audio file pronunciation.
The source code is as follows
Import pygamedef make_voice (x): pygame.mixer.init () voi = chinese_to_pinyin (x). Split () for i in voi: if I = = 'XXXX': # handle' the sound of XXXX' Ignore continue pygame.mixer.music.load ("voice/" + I + ".mp3") pygame.mixer.music.play () while pygame.mixer.music.get_busy () = = True: pass return None
Let's show the final complete code diagram of this instructional version (the file is called "test.py"):
Import pygame def chinese_to_pinyin (x): y =''dic = {} with open ("unicode_py.txt") as f: for i in f.readlines (): dic [i.split () [0]] = i.split () [1] for i in x: I = str (i.encode (' unicode_escape')) [- 5Raffle 1] .upper () Try: y + = dic [I] + 'except: y + =' XXXX' return y def make_voice (x): pygame.mixer.init () voi = chinese_to_pinyin (x). Split () for i in voi: if I = = 'XXXX': continue pygame.mixer.music.load ("voice/" + I + ".mp3") Pygame.mixer.music.play () while pygame.mixer.music.get_busy () = True: pass return None while True: P = input ("Please enter text:") make_voice (p)
The directory structure of the tutorial files:
Test.pyunicode_py.txtvoice----A1.mp3 A2.mp3 A3.mp3...
This is the principle of realization, and at the same time, some extensions can be made according to some specific conditions, such as the pronunciation of Arabic numerals to Chinese characters (that is, 1234006.15 is pronounced as "1.23401e+06", which is not difficult to achieve), and it is also great to solve the problem of polyphonic characters.
Of course, as long as the ideas are rich enough, you can also achieve a lot of interesting functions (such as player production or simple creation of electronic music, etc.) with the audio features of pygame.
Thank you for your reading, the above is "how to use Python code to achieve text-to-voice" content, after the study of this article, I believe you on how to use Python code to achieve text-to-voice this problem has a deeper understanding, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.