Online Dictionary (no interface)

Keywords: Programming JSON Python encoding PHP

Implementation ideas:

  1. The definition of the dictionary is obtained from ICIBA server, so you need to obtain an identity key first. The obtaining methods are as follows: ①. Open ICIBA website: ICIBA (2) select ICIBA to check the word, and after submitting the information, obtain an identity key in the email
  2. Use openural to capture all information of ICIBA website and convert it into string
  3. Using d = json.loads(s) to convert the acquired information into dict
  4. If you print d and extract the string needed in d, you will find the following situation: (this is for the convenience of reading and adjusting the format, the original format of d actually has no spaces and line breaks.)
    '''
    {'word_name': 'hello',
    'is_CRI': 1,
    'exchange': {'word_pl': ['hellos'], 
    'word_past': '',
    'word_done': '', 
    'word_ing': '',
    'word_third': '', 
    'word_er': '', 
    'word_est': ''}, 
    'symbols': [{'ph_en': "hə'ləʊ", 'ph_am': 'həˈloʊ', 'ph_other': '', 'ph_en_mp3': '',                     
        'ph_am_mp3': 
        'http://res.iciba.com/resource/amp3/1/0/5d/41/5d41402abc4b2a76b9719d911017c592.mp3', 
        'ph_tts_mp3': 'http://res-tts.iciba.com/5/d/4/5d41402abc4b2a76b9719d911017c592.mp3', 
        'parts': 
            [{'part': 'int.', 'means': ['Hello, hello.', 'Hello, hello', 'Greeting', 'Say hello']}, 
            {'part': 'n.', 'means': ['"A greeting or greeting']}, {'part': 'vi.', 'means': 
                ['Shout hello.']}]}], 
           'items': ['']}
    
    '''

    This is a very long string. Here we just need to extract the meaning of the word: note that this process is actually very interesting, involving the iteration of data types. Here is the idea of extraction: ①. According to the symbols, if the whole data type is a dictionary, call d['symbols'] to enter the dictionary first, ②. Observe the data and find that the value of d['symbols'] is an array of length 1 Then we call d['symbols'][0] to enter the array, ③. Finally, the d['symbols'][0] array is actually composed of dictionaries. We call d['symbols'][0]['parts'] to extract all the meanings of the words. ④ because the value of d['symbols'][0]['parts'] is also a dictionary, we only need to traverse the dictionary to output the meanings of the words. (I stuck for about an hour when I did this for the first time. In fact, this is the iteration of data in python. Just pay attention to the level by level traversal of symbols.)

  5. Traverse the processed string

Implementation source code:

import json
import urllib.request

log = print


def openurl(url):
    # Write the url as the top 250 page of Douban
    # url = 'https://movie.douban.com/top250'
    # Download the page and get a variable s of bytes type
    s = urllib.request.urlopen(url).read()
    # Using utf-8 encoding to convert s to string and return
    content = s.decode('utf-8')
    return content


def translate(word):
    """
    word Is a word without spaces
    """
    key = 'A052B9965AA8C418D9866AC5F8F24F8D'
    url = 'http://dict-co.iciba.com/api/dictionary.php?type=json&key={}&w={}'.format(key, word)

    # openurl is the openurl function of lesson 4 assignment 14
    # Used to get the results returned by the network dictionary
    s = openurl(url)
    d = json.loads(s)
    result = ''
    list1 = d['symbols'][0]['parts']
    for i in range(len(list1)):
        result = result + list1[i]['part'] + '\n'
        list2 = list1[i]['means']
        for m in range(len(list2)):
            result = result + list2[m] + '\n'
        result = result + '\n'
    return result


def main():
    word = 'hello'
    s = translate(word)
    log(s)


if __name__ == '__main__':
    main()

 

Posted by zenon on Tue, 10 Dec 2019 03:00:25 -0800