In chapter 2 of "Python natural language processing", Exercise 6: How can I solve this problem? /A> Are there any problems? /A> Lu (I) has been running $ has been running too many has been running

Source: Internet
Author: User
Tags nltk

In chapter 2 of "Python natural language processing", Exercise 6: How can I solve this problem? /A> Are there any problems in hongbang? /A> Lu (I) has been running $ has been running too many has been running

Problem description: In the discussion of the comparative vocabulary, create an object called translate, through which you can use German and Italian words to find the corresponding English words. What problems may occur in this method? Can you propose a way to avoid this problem?

The practice in the book is to use the entries () method to specify a language linked list to access the same-source words in multiple languages, and then convert it into a simple dictionary. The Code is as follows:

1 from nltk.corpus import swadesh2 swadesh.fileids()3 it2en = swadesh.entries(['it', 'en'])4 de2en = swadesh.entries(['de', 'en'])5 translate = dict(it2en)6 translate.update(dict(de2en))7 translate['Hund']

However, this method has a problem. The original linked list contains many-to-many relationship words, such:

1 (u'tu, Lei', u'you (singular), thou')2 (u'lui, egli', u'he')3 (u'loro, essi', u'they')4 (u'qui, qua', u'here')5 (u'udire, sentire', u'hear')6 (u'odorare, annusare', u'smell')7 (u'dividere, separare', u'split')8 (u'aguzzo, affilato', u'sharp')9 (u'asciutto, secco', u'dry')

When you enter translate ['tu'], you (singular) and thou are not displayed correctly, but KeyError: 'tu ':

1 >>> translate['tu']2 Traceback (most recent call last):3   File "<stdin>", line 1, in <module>4 KeyError: 'tu'

Solution:

Traverse the language linked list. When multiple-to-many relationships are detected, process the element and add it to the original language linked list.

Code:

 1 from nltk.corpus import swadesh 2 swadesh.fileids() 3 it2en = swadesh.entries(['it', 'en']) 4 de2en = swadesh.entries(['de', 'en']) 5  6 # list[tuple(str, str), ...] 7 for it, en in it2en: 8     if ',' in it: 9         words = it.split(', ')10         for eachWord in words:11             newWord = (eachWord, en)12             it2en.append(newWord)13 14 for de, en in it2en:15     if ',' in de:16         words = de.split(', ')17         for eachWord in words:18             newWord = (eachWord, en)19             de2en.append(newWord)20 21 translate = dict(it2en)22 translate.update(dict(de2en))

 

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.