Chapter 2 of Python natural language processing exercises 12 and Chapter 2

Source: Internet
Author: User
Tags nltk

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.