ICT - Intelligent Speech Technology

Voice Chat

Learning Objectives

  1. Implement voice chat with ASR and TTS

  2. Execute and comprehend single-branch structure

  3. Execute and comprehend double-branch structure

  4. Execute and comprehend multi-branch structure

  5. Master Python syntax: if, elif, else, equal, colon and indentation

  6. (Extra) Understand nested branch structure and operators

Activity 1 Run simple voice chat code

Copy and run the code (silly one)

python
import edge_tts
import os
import speech_recognition as sr
rec = sr.Recognizer()
mic = sr.Microphone()
print("Please speak English")
mic.__enter__()
audio = rec.listen(mic)
txt = rec.recognize_google(audio, language='en-US')
print("Recognized text:", txt)
voice = "en-US-AriaNeural"
txt = "Nice to meet you!"
tts = edge_tts.Communicate(txt, voice)
path = "tts.mp3"
tts.save_sync(path)
os.startfile(path)

Activity 2 Understand the Single-branch Structure

Insert the code 2 into the code 1and run

python
#code 1
import edge_tts
import os
import speech_recognition as sr
rec = sr.Recognizer()
mic = sr.Microphone()
print("Please speak English")
mic.__enter__()
audio = rec.listen(mic)
speech_text = rec.recognize_google(audio, language='en-US')
print("Recognized text:", speech_text)
reply = ""
voice = "en-US-AriaNeural"
tts = edge_tts.Communicate(reply, voice)
path = "tts.mp3"
tts.save_sync(path)
os.startfile(path)
python
#code 2
speech_text = rec.recognize_google(audio, language='en-US')
if speech_text == "Hello":
    reply = "Hello, how are you?"

Read and run the single branch and grammar

python
age = input()
age = int(age)
if age >= 18:
    print('Adult')

Write a program.

bash
Problem Description
Write a program to judge whether the input score passes the exam.
If the score is greater than or equal to 60, print Pass.
If the score is less than 60, do not print anything.

Input
A single integer representing the exam score (0 ≤ score ≤ 100).
Output
Print Pass only when the score ≥ 60; otherwise, print nothing.

Sample Input 1
60
Sample Output 1
Pass

Activity 3 Understand the Double-branch Structure

Insert the code 3 into the code 1and run

python
#code 3
speech_text = rec.recognize_google(audio, language='en-US')
if speech_text == "Hello":
    reply = "Hello, how are you?"
else:
    reply = "Sorry, I can not understand you."

Read and run the double branch and grammar

python
age = input()
age = int(age)
if age >= 18:
    print('Adult')
else:
    print("Child")

Write a program.

bash
Problem Description
Write a program to judge if a student's exam score is qualified.
A score is considered pass when it is no less than 60.
Input an integer score, print Pass if passed, otherwise print Fail.

Input
Single integer representing exam score.
Output
Output one word: Pass or Fail

Sample Input 1
65
Sample Output 1
Pass

Sample Input 2
58
Sample Output 2
Fail

Activity 4 Understand the Multiple-branch Structure

Insert the code 4 into the code 1and run

python
#code 4
speech_text = rec.recognize_google(audio, language='en-US')
if speech_text == "Hello":
    reply = "Hello, how are you?"
elif speech_text == "How are you":
    reply = "I am fine, thank you."
else:
    reply = "Sorry, I can not understand you."

Write a program.

bash
Problem Description
Write a program to grade the exam score using multiple conditions.
If score ≥ 90: print A
If score ≥ 80 and < 90: print B
If score ≥ 70 and < 80: print C
If score ≥ 60 and < 70: print Pass
If score < 60: print Fail
Input
A single integer (0 ≤ score ≤ 100)
Output
Print the corresponding grade, only print Pass for 60-69, print Fail if below 60.

Sample Input 1
95
Sample Output 1
A

Sample Input 2
82
Sample Output 2
B

Sample Input 3
65
Sample Output 3
Pass

Sample Input 4
50
Sample Output 4
Fail

Activity 5 Syntax Learning Test

Read the syntax element below

Syntax Element

Description

Example

if

Judge the first condition

if a > 0:

elif

Judge extra conditions sequentially

elif a < 0:

else

Execute when all conditions fail

else:

colon :

Mark the end of condition line

Add at the end of judgment statement

indentation

Fixed space offset for code block

Indent inner code uniformly Read the syntax element below


Read the operators below

Operator

Meaning

Example

+

Addition

5 + 3

-

Subtraction

10 - 4

*

Multiplication

2 * 6

/

Division

8 / 2

**

Exponentiation

3 ** 2

>

Greater than

a > 5

>=

Greater than or equal to

a >= 5

<

Less than

a < 10

<=

Less than or equal to

a <= 10

==

Equal to

a == 5

!=

Not equal to

a != 3

and

Logical AND

a>1 and a<10

or

Logical OR

a==2 or a==4

not

Logical NOT

not a==0

in

Exist in sequence

"hi" in text

ICT 3 MCQ

Single ChoiceICT 3 MCQ 1#22
[0/1]

Which keyword starts the first conditional judgment?

Single ChoiceICT 3 MCQ 2#23
[0/1]

Which keyword is used for additional conditional checks?

Single ChoiceICT 3 MCQ 3#24
[0/1]

Which part runs when all above conditions are false?

Single ChoiceICT 3 MCQ 4#25
[0/1]

What symbol must be added at the end of a condition line?

Single ChoiceICT 3 MCQ 5#26
[0/1]

What defines a code block in Python branch structure?

Single ChoiceICT 3 MCQ 6#27
[0/1]

How many else blocks can one complete branch structure have?

Single ChoiceICT 3 MCQ 7#28
[0/1]

Which statement is grammatically correct?

Single ChoiceICT 3 MCQ 8#29
[0/1]

Indented code belongs to ______.

Single ChoiceICT 3 MCQ 9#30
[0/1]

The correct order of branch keywords is ______.

Single ChoiceICT 3 MCQ 10#31
[0/1]

When using multiple elif, conditions are judged ______.

Activity 6 Nested Branch and Logical operators

Use nested branch in the code

python
import edge_tts
import os
import speech_recognition as sr

rec = sr.Recognizer()
mic = sr.Microphone()
print("Please speak English")

mic.__enter__()
audio = rec.listen(mic)
speech_text = rec.recognize_google(audio, language='en-US')
print("Recognized text:", speech_text)

reply = ""

# Use all operators + Nested branch structure
if "Hello" in speech_text or "Hi" in speech_text:
    reply = "Hello! How are you?"
elif speech_text == "How are you":
    # Nested if + not
    if not speech_text != "How are you":
        reply = "I am fine, thank you!"
    else:
        reply = "I'm doing well!"
elif speech_text == "What is your name":
    reply = "My name is AI voice robot."
elif speech_text == "Goodbye" and "Bye" not in speech_text:
    reply = "See you next time!"
elif speech_text == "Thank you" or speech_text == "Thanks":
    reply = "You are welcome!"
elif speech_text != "What":
    reply = "Nice to talk with you!"
else:
    reply = "Sorry, I cannot understand you."

voice = "en-US-AriaNeural"
tts = edge_tts.Communicate(reply, voice)
path = "tts.mp3"
tts.save_sync(path)
os.startfile(path)


AI Notes