Python script to learn Chinese tones

I wrote a script to learn to distinguish between the Chinese tones. It’s not very useful without the corresponding multimedia (ie: recordings I got my gf to make), but maybe with script in hand, others can easily make their own such multimedia?

The script plays a sound, eg ‘dianhua’, then presents two possibilities for the user to choose between, eg the dianhua meaning ‘stippled painting’, or the dianhua meaning ‘telephone’.

There are three parts to the script:
- testdiff.py : the script, in Python
- testdiff.xml: configuration file, where you specify each test, the sounds for each test, and a corresponding text description
- the sound files, in a subdirectory ‘sounds’

Sample configuration file:

<root>
<tests>
<test>
<choice sound="几点.mp3" text="几点 jī diăn (what time is it?)" />
<choice sound="机电.mp3" text="机电 jī diàn (electrical machine)" />
</test>
<test>
<choice sound="忌惮.mp3" text="忌惮 jì dàn (fear)" />
<choice sound="鸡蛋.mp3" text="鸡蛋 jī dàn (egg)" />
</test>
</tests>
</root>

testdiff.py python script, gpl v2:

#!/usr/bin/python

# Copyright Hugh Perkins 2010
# hughperkins@gmail.com http://manageddreams.com
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
#  more details.
#
# You should have received a copy of the GNU General Public License along
# with this program in the file licence.txt; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
# 1307 USA
# You can find the licence also on the web at:
# http://www.opensource.org/licenses/gpl-license.php
#
# ======================================================================================
#

import sys
import os
from elementtree.ElementTree import ElementTree
import random
import subprocess
import time

def playSound( soundfilepath ):
	popen2 = subprocess.Popen(['playsound',soundfilepath], stdout=subprocess.PIPE)
	popen2.wait()

def runTest():
	print "Next test:"

	config = ElementTree(file='testdiff.xml')
	numtests = 0
	for test in config.findall('/tests/test'):
		numtests = numtests + 1

	testnum = random.randint(0, numtests - 1 )

	chosentest = None
	numtests = 0
	for test in config.findall('/tests/test'):
		if numtests == testnum:
			chosentest = test
		numtests = numtests + 1

	numchoices = 0
	for choice in chosentest.findall('choice'):
		numchoices = numchoices + 1

	testchoice = random.randint(0, numchoices - 1 )

	numchoices = 0
	for choice in chosentest.findall('choice'):
		#print str(numchoices) + ' ' + choice.get('text')
		if testchoice == numchoices:
			soundfilepath = 'sounds/' + choice.get('sound')
			choicetext = choice.get('text')
		numchoices = numchoices + 1

	playSound(soundfilepath)

	numchoices = 0
	for choice in chosentest.findall('choice'):
		print str(numchoices) + ' ' + choice.get('text')
		numchoices = numchoices + 1

	gotchoice = False
	while not gotchoice:
		print "Your choice? ('r' to repeat sound)"

		userchoice = sys.stdin.readline().strip().lower()
		if userchoice == str(testchoice):
			gotchoice = True
			print "Yes! : " + choicetext
			print ""
			playSound(soundfilepath)
		elif userchoice == 'r':
			playSound(soundfilepath)
		else:
			gotchoice = True
			print "In fact: " + choicetext
			print ""
			playSound(soundfilepath)
			done = False
			while not done:
				print 'r to replay correct answer, b to play both, n for next'
				userchoice = sys.stdin.readline().strip().lower()
				if userchoice == 'r':
					playSound(soundfilepath)
				if userchoice == 'b':
					for choice in chosentest.findall('choice'):
						print choice.get('text')
						soundfilepath = 'sounds/' + choice.get('sound')
						playSound(soundfilepath)
				elif userchoice == 'n':
					done = True

while True:
	runTest()
	time.sleep(1)

I might turn it into a webpage, and try to generate some ad-revenue, but most likely I won’t :-P

Note that the code above is pretty dreadful, but it works, which is the goal, since I want to one day be able to hear the difference in the tones in Chinese.

(PS if you ask me nicely, and I know you, I might consider providing you with some of the corresponding multimedia, but I’m not that keen on just plastering my gf’s voice all over the web, what with ‘my voice is my password’ becoming more popular and so on….)

Leave a Reply