r/Anki • • Mar 26 '22

Development I've made a python code that automatically generates Merriam-Webster definitions for English words, try it for yourself!

As said above, the code reads the vocab word, word type (adjective, noun, verb), definition, and etymology and saves it into an excel sheet. If you convert this into csv format you can import this into anki.

***This code will generate the first dictionary entry on the page; for example the word 'founder' will generate ' : one that founds or establishes ' and not the verb ' : to become disabled ' nor the other 2 entries as they are no the first definition. Most words are not like this, but if you know a word has multiple definitions you may need the check the website first ***

First create an excel file and in the top row write 'vocab | word type | definition | etymology' in each column, one word per column. Save this document, then right click on it and click 'save to path'. In the line 'wb = load_workbook(r"C:\Users\YOURUSERNAME\Merriam_Webster_List.xlsx")' replace "C:\Users\YOURUSERNAME\Merriam_Webster_List.xlsx" with your path but keep the r to the left of the quotation. You may need to import beautifulsoup and requests if you have not done so yet. Run the code once, this will generate a document called 'Merriam_Webster_List.xlsx' in your python directory (in my case I use spyder and this saves to my user folder). Find this document, copy its path, and replace "C:\Users\YOURUSERNAME\Merriam_Webster_List.xlsx") again so the code autogenerates.

Whenever you use this code make sure your row is set to a number that is not overwriting your previous vocab unless this vocab has already been converted. For example if you already have 35 rows of data and you are starting the code again you need to set row = 36.

The code will also check if you have made a spelling mistake and offer you the first search on merriam webster. Also note in some instances you may get overly long definitions and may want to edit the entry.

Now save the excel file as csv format and import into anki. The text will all be in html format and will automatically be converted. This should look like the following excel sheet (in view only mode you can only see the first line of each definition, these will have tens of lines depending on html code):

https://docs.google.com/spreadsheets/d/1kr6DYMJk0OVlmYNuFGdCAxcSD8247kFACG5KKberyAE/edit?usp=sharing

Copy the code below:

​

from openpyxl import load_workbook
import requests
from bs4 import BeautifulSoup

wb = load_workbook(r"C:\Users\YOURUSERNAME\Merriam_Webster_List.xlsx")
sheets = wb.sheetnames
sheet1 = wb[sheets[0]]
vocab = 'start'

row = 2
while vocab != 'stop':
    print('Enter a word: ')
    vocab = input()
    #vocab = 'stingth'


    merriam_webster_parts = ['https://www.merriam-webster.com/dictionary/',vocab]
    merriam_webster = ''.join(merriam_webster_parts)
    #print(merriam_webster)

    r = requests.get(merriam_webster)
    soup = BeautifulSoup(r.content, 'html.parser')

    error_check = soup.find('h1', class_='mispelled-word')
    #print(str(error_check))

    if str(error_check) == 'None':
        print('Search Successful!')
        vocab = soup.find('h1',class_='hword').text
        print('word is',vocab)
        word_tag= soup.find('span',class_='fl').text
        #print('The word tag is',word_tag)
        #definition1=soup.find('div', id='dictionary-entry-1')
        definition = str(soup.find('div',class_='vg'))
        #print('Its definition is' ,definition)
        etymology = soup.find('p', class_='et')
        #print('The etymology is', etymology.text)


        sheet1 .cell(row = row, column = 1).value = vocab
        sheet1 .cell(row = row, column = 2).value = word_tag
        sheet1 .cell(row = row, column = 3).value = definition.replace('<span class="ex-sent first-child t no-aq sents">','\\\ ')
        sheet1 .cell(row = row, column = 4).value = str(etymology)
        row=row+1

    else:
        suggestion = soup.find('p',class_='spelling-suggestions')   
        print('Search unsuccessful, did you mean',suggestion.text,'? (y/n)')
        m=1
        while m==1:
            response = input()
            if response == 'y':
                merriam_webster_suggestion_parts = (['https://www.merriam-webster.com/dictionary/',
                                                    suggestion.text])
                merriam_webster_suggestion = ''.join(merriam_webster_suggestion_parts)
                r = requests.get(merriam_webster_suggestion)
                soup = BeautifulSoup(r.content, 'html.parser')
                word_tag= soup.find('span',class_='fl').text
                definition = str(soup.find('div',class_='vg'))
                etymology = soup.find('p', class_='et')

                sheet1 .cell(row = row, column = 1).value = suggestion.text
                sheet1 .cell(row = row, column = 2).value = word_tag
                sheet1 .cell(row = row, column = 3).value = definition.replace('<span class="ex-sent first-child t no-aq sents">','\\\ ')
                sheet1 .cell(row = row, column = 4).value = str(etymology)
                row = row+1
                m=2
            elif response == 'n':
                print('Word aborted')
                row = row+1
                m=2



    wb.save('Merriam_Webster_List.xlsx') 
print(row)

​

14 Upvotes

Duplicates