Chapter 2 of Python natural language processing exercises 12 and Chapter 2
Problem description: CMU pronunciation dictionary contains multiple pronunciations of certain words. How many different words does it contain? What is the proportion of words with multiple pronunciations in this dictionary?
Because nltk. corpus. cmudict. entries () cannot use the set () method to remove duplicate words. It can only be traversed and then counted. The proportion of multiple pronunciation words in this dictionary first counts the number of each pronunciation, and then divided by the total number of dictionaries.
Code:
1 import nltk 2 from _ future _ import division 3 4 entries = nltk. corpus. cmudict. entries () 5 ky = entries [0] [0] 6 cnt = 0 7 for key, pron in entries: # Different Pronunciation counts 8 if ky! = Key: 9 ky = key10 cnt + = 111 12 length = len (entries) # Total number of CMU dictionaries 13 lenSets = [len (pron) for key, pron in entries] #14 percentage = [] 15 16 for I in range (1, max (lenSets) + 1): 17 percentage. append (lenSets. count (I)/length * 100) 18 19 print cnt20 for I in percentage: 21 print '%. 3f % '% I # format the output