# 1 Python beginners make simple English word learning tools

Trigger

Hello. I started programming by myself as a liberal arts college student, but I don't know how many minutes. Even if you understand how it works, others don't understand how to do it more efficiently, or what is called general project development. In that sense, I would like to publish a criticized and childish program. It is also my own growth diary. I will read it when I have a grandchild someday. And why did I program English words from my experience while studying abroad? While studying abroad, I was watching a movie with subtitles, and although it seems that there is no machine to use in conversation, there were many English words that I wanted to include as knowledge. Initially I wrote them in a notebook, but it was a lot of work and took time. Since I was taking classes at a local university, there are many tasks, so I think it would be better to set up a program that would allow you to type in words you don't understand at once and put them together in an easy-to-read state. ?? That's why I left my usual study and started working on Python. .. ..

Overview

I first studied at Paiza. Weak for free. So let's learn the basics and make it for the time being! It's like that. As for the flow, Word input → Page display of corresponding English words → Extract meaning → Exhale It would be good if we realized.

Implementation

What you need to prepare this time --Requests required for web connection --BeautifulSoup convenient for processing web data --Datetime because I want to introduce the date to the file name to save --Since it is handled as a csv file, csv

qiita.py


import requests
from bs4 import BeautifulSoup as bs4
import datetime
import csv

Then you can enter a word you don't understand or store the meaning of that word in a list.

qiita.py


unknown = []
result= []
path_w ='\Unknown_words_'+str(datetime.date.today())+'.csv'
#Creating a function to process csv
def OpenCSV():
    with open(path_w, mode='x') as new:
        try:
            new.write('\n')
            new.close()
            
        except FileExistsError:
            with open(path_w, mode='a') as writeon:
                writeon.write()
                writeon.close()

Put the entered word in unknown and the extracted meaning in result. I will also make a save destination path. The handling of the csv module is really imitation, I don't know much. If open mode ='x', I also wrote mode ='a' as an exception handling when a new file with the same name already exists.

Regarding the mode of the OPEN function, please refer to here for details.

Word search

Enter the word search of the main subject. This time we will use Weblio as an English dictionary. The reason is that the URL to open is

qiita.py



'https://ejje.weblio.jp/content/'+Words I don't understand

Because it was easy to handle. The contents are easy to understand and two birds with one stone.

qiita.py


#Create a word search function
def SearchWords(uw):
    try:
        line = 'https://ejje.weblio.jp/content/'+ uw
        res = requests.get(line)
        res.encoding = res.apparent_encoding
        bs = bs4(res.content,'html.parser')
        global meaning_a
        meaning_a = bs.find( class_='content-explanation ej').text
    except AttributeError:
#If it does not exist, AttributeError is returned, so enter exception handling.
        meaning_a = 'Could not be located.'
        pass

The argument is uw, which is an abbreviation for Unknown Words. There is nothing special to mention, but in weblio the meaning is stored in a class called "content-explanation ej", so let's make it text format with .text. At this time, if a misspelling or a non-existent English word is included in the argument, AttributeError will be returned, so it is described that it was not found at that time and skipped.

input

python


while True:
    wrd = input('Type your unknown words (If you submit Enter, it finishes.)\n')
#If it is left as it is, it will loop infinitely as it is input, so make a chance to break
    if wrd == "":
        break
    else:
        unknown.append(wrd)

I didn't know how to finish it cleanly, so when it is sent, I try to interrupt the unknown list storage there.

Main processing

python


#After inputting, fit each function from the stored list and create a dictionary
for i in range(len(unknown)):
    result_tmp = []
    obj = unknown[i]
    result_tmp.append(obj)
    SearchWords(obj)
    result_tmp.append(meaning_a)
    print(result_tmp)
    result.append(result_tmp)

For the time being, temporarily insert [word, meaning] into the list called result_tmp. Then put it in the form of a two-dimensional list (what is it? It's illiterate ...).

csv

python


with open(path_w,'a') as csv_w:
    writer = csv.writer(csv_w)
    writer.writerows(result)

Drop it in csv and you're done. The CSV file can be read in Excel, so I feel that this is the minimum.

I want to use Pandas to make Excel even more beautiful. I will post a dirty code at the end, so I will refer to it if there are any improvements, so please leave a comment!

import requests
from bs4 import BeautifulSoup as bs4
import datetime
import csv
# import pandas as pd

#Create a storage location.
unknown = []
result= []
path_w ='Where you want to save/Unknown_words_'+str(datetime.date.today())+'.csv'
def OpenCSV():
    with open(path_w, mode='x') as new:
        try:
            new.write('\n')
            new.close()
            
        except FileExistsError:
            with open(path_w, mode='a') as writeon:
                writeon.write()
                writeon.close()

#Create a word search function
def SearchWords(uw):
    try:
        line = 'https://ejje.weblio.jp/content/'+ uw
        res = requests.get(line)
        res.encoding = res.apparent_encoding
        bs = bs4(res.content,'html.parser')
        global meaning_a
        meaning_a = bs.find( class_='content-explanation ej').text
    except AttributeError:
#If it does not exist, AttributeError is returned, so enter exception handling.
        meaning_a = 'Could not be located.'
        pass


#Let me enter
while True:
    wrd = input('Type your unknown words (If you submit Enter, it finishes.)\n')
#If it is left as it is, it will loop infinitely as it is input, so make a chance to break
    if wrd == "":
        break
    else:
        unknown.append(wrd)

#After inputting, fit each function from the stored list and create a dictionary
for i in range(len(unknown)):
    result_tmp = []
    obj = unknown[i]
    result_tmp.append(obj)
    SearchWords(obj)
    result_tmp.append(meaning_a)
    print(result_tmp)
    result.append(result_tmp)

#Convert to csv file and make it usable in Excel etc.
with open(path_w,'a') as csv_w:
    writer = csv.writer(csv_w)
    writer.writerows(result)

Thank you for reading! I will continue to do my best.

Recommended Posts

# 1 Python beginners make simple English word learning tools
Learning flow for Python beginners
Django beginners make simple apps 4
Machine learning summary by Python beginners
<For beginners> python library <For machine learning>
[Python] Simple Japanese ⇒ I tried to make an English translation tool
Machine learning with python (2) Simple regression analysis
python learning
Create an English word app with python
Machine learning beginners tried to make a horse racing prediction model with python
English word spell check tool (made by python)
[Python] Make a simple maze game with Pyxel
How to make Python faster for beginners [numpy]
python. Can programming beginners make things randomly? Part 2
About list processing (Python beginners after learning Ruby)
python. Can programming beginners make things randomly? Part ①
[Python] Learning Note 1
Python learning notes
python learning output
Python learning site
Python learning day 4
Python Deep Learning
Python learning (supplement)
Deep learning × Python
Beginners practice Python
Python beginner's note
python learning notes
[Python] [Word] [python-docx] Simple analysis of diff data using python
Make a simple Slackbot with interactive button in python
Let's make a simple game with Python 3 and iPhone
Python Scikit-learn Linear Regression Analysis Nonlinear Simple Regression Analysis Machine Learning
Machine learning beginners try to make a decision tree
About adding / deleting lists (Python beginners after learning Ruby)
What is God? Make a simple chatbot with python