WordSearch English word meaning search tool

English word meaning search Gadgets:

There are five functions:

1.Definition (word definitions - English Interpretation)
2.WordFamily (similar words)
3.Synonym (synonyms)
4.Antonym (antonyms)
5.ContextualSentence (sentence)

 

The need to fill in the branches of a query word 'Word.txt' file later.

Run the .py file, select the need to generate relevant content

Import Requests
 Import Time
 from BS4 Import BeautifulSoup
 Import Re
 Import SYS
 # modify handed depth limit 
sys.setrecursionlimit (100000 )

# Word file location 
# between words separated by returns, for example: 
# the Achieve 
# Acquisition 
# Administration 
# Affect 
# Appropriate 
# Aspects 
# Assistance 
# the Categories 
# Chapter 
# Commission 
WORDS_PATH = ' ./Word.txt ' 
# page head 
the URL of = ' HTTP : //dict.cn/ ' 
# list of words 
wORDS = []
 # table of contents 
cONTENT = []
 # input when the comment 
# SELECT_NUM = 4
SELECT_NUM_DICT = {
    1: 'Definition',
    2: 'WordFamily',
    3: 'Synonym',
    4: 'Antonym',
    5: 'ContextualSentence'
}
# 正则表达式
STR_MATCH_DICT = {
    1: r'.?([a-zA-z].*)\;',
    2: r'\/(\b[a-zA-Z]+\b)\"',
    3: r'\/(\b[a-zA-Z]+\b)\"',
    4: r'\/(\b[a-zA-Z]+\b)\"',
    5: r'\<li\>(.*?)\<br\/\>',
}
# 搜索规则
QUERY_RULE_DICT = {
    1: '.section.def .layout.en ol[slider="3"] li',
    2: '.section.rel .layout.nwd a',
    3: '.section.rel .layout.nfo ul[slider="12"] li a',
    4: '.section.rel .layout.nfo ul[slider="12"] li a',
    5: '.section.sent .layout.sort ol[slider="2"] li'
}


# 5: '.section.def .layout.en ol[slider="3"] li'

DEF RUN (str_match, query_rule):
     '' ' iterate to obtain the corresponding attributes of each word ' ''

    # Get web elements 
    # create links 
    rs = requests.Session ()

    # Traversed to get each word element 
    for Word in WORDS:
        word_url = URL + word

        res = rs.get(word_url)

        # Get link source 
        Soup = the BeautifulSoup (res.text, ' lxml ' )

        # Query 
        Element = soup.select (query_rule)
        element = str(element)
        element = re.findall(str_match, element)

        # Element = STR (Element) 
        IF Element:
             # sentences selected from the shortest 
            IF SELECT_NUM ==. 5 :
                Element = min (Element, Key = len)
             # Thesaurus selected from the frontmost 
            elif SELECT_NUM ==. 1 or SELECT_NUM == 2 or SELECT_NUM ==. 3 :
                Element = Element [0]
             # antonyms select the last 
            elif SELECT_NUM ==. 4 :
                element = element[-1]

        else:
            element = ''

        # Add any string, whether to distinguish find the word interpretation, if not required for the empty string '' can 
        Sign = ' the SSS: ' 
        Content = Sign + Element
        CONTENT.append(content)

    


DEF Show ():
     '' ' display selection menu ' '' 
    Print ( ' Please select word generated content: \ the n- ' 
          ' \ n1.Definition (word definitions) \ the n- ' 
          ' \ n2.WordFamily (similar words) \ n- ' 
          ' \ n3.Synonym (synonyms) \ n- ' 
          ' \ n4.Antonym (antonyms) \ n- ' 
          ' \ n5.ContextualSentence (sentences) \ n- ' 
          ' ' )
    select_num = int (the INPUT ( ' Please enter your choice: ' ))
     return select_num


DEF Write ():
     '' ' written stored content ' '' 
    File = SELECT_NUM_DICT [SELECT_NUM]

    date = time.strftime('%Y%m%d%H')

    file_name = file + date + '.txt'
    print('写入')
    jump = str('\r')
    # 写入
    with open(file_name, 'w+') as f:
        for i in CONTENT:
            f.writelines(i)
            f.write(jump)


DEF Mean ():
     '' ' get the word ' '' 
    # get a word 
    with Open (WORDS_PATH) AS f:
         for Word in f.readlines ():
            word = word.replace('\n', '')
            WORDS.append(word)

    IF len (WORDS) <. 1 :
         Print ( ' not get word ' )
         return

    # Display selection menu 
    select_num = Show ()
     # modify global variables 
    , Ltd. Free Join SELECT_NUM

    SELECT_NUM = select_num
     #
     # # The default selection 5 
    # IF SELECT_NUM:

    # Corresponding to regular expression associated 
    str_match = STR_MATCH_DICT [SELECT_NUM]
    query_rule = QUERY_RULE_DICT[SELECT_NUM]

    run(str_match, query_rule)
    # Write to 
    the Write ()
     Print ( ' task ends ' )


if __name__ == '__main__':
    mean()    

 

 

 

###

This tool is used the word sea. Corresponding to the elements as well as content acquisition path regular expressions are also directed to the site.

To change, rewrite the content.

 

Github Download link:

https://github.com/Roshenn/WordDictionarySearch

 

Guess you like

Origin www.cnblogs.com/jrri/p/11693981.html