[Python] Let's throw school tasks into Python [Efficiency]

Introduction

I'm currently in my first year of high school In my spare time due to the corona sickness, I studied Python thoroughly and I started studying Java and Elixir all night, so my school homework was ... After all, because it is Python for efficiency, I thought I would do my school homework. It may seem strange in terms of code efficiency and speed, but I would appreciate it if you could see it with a warm eye.

Composition

** Theme: What is art to you ** I thought this should be written in Python. I was studying natural language processing a little, so I used Mecab to divide it. A new sentence was produced from multiple model sentences by Markov chain. Conveniently, Python has a module called markovify. Used to process the word-separated text.


import MeCab
import markovify 
text = ''
parse_text = MeCab.Tagger('-Owakati').parse(text)

text2 = ''
parse_text2 = MeCab.Tagger('-Owakati').parse(text2)

text_model = markovify.Text(parse_text)
text_model2 = markovify.Text(parse_text2)

comb_model = markovify.combine([text_model,text_model2],[1,1])
 
for _ in range(8):
    print(comb_model.make_sentence())

The text produced by this

Art is meaningful to create with your own sensibility, not art taught at school.

The content was to sell a fight, and the result was 3/10 as expected. 122DE3D7-2F1E-488C-8B21-B37B05CCBC1F.jpeg

English tasks that are difficult to express in one word

Content of the task

Use a certain app to translate the read Japanese into English and read it aloud Or, there is something that reads English as it is.

Think about the logic of the program

First, build a program for voice recognition, When reading aloud in English, you have to write the processing when reading aloud in Japanese.

import pyaudio
import wave
import speech_recognition
from playsound import playsound
from googletrans import Translator
from gtts import gTTS
translator = Translator()

def rcd():
    audio = pyaudio.PyAudio()
    stream = audio.open(format=pyaudio.paInt16,channels=1,rate=44100,input=True,input_device_index=0,frames_per_buffer=2**11)

    frames = []
    for i in range(0, int(44100 / 2**11 * 10)):
        data = stream.read(2**11)
        frames.append(data)

    stream.stop_stream()
    stream.close()
    audio.terminate()

    sFile = wave.open(sound.wav, 'wb')
    sFile.setnchannels(1)
    sFile.setsampwidth(audio.get_sample_size(pyaudio.paInt16))
    sFile.setframerate(44100)
    sFile.writeframes(b''.join(frames))
    sFile.close()

def to_text():
    r = speech_recognition.Recognizer()
    with speech_recognition.AudioFile(sound.wav) as src:
        audio = r.record(src)
    text = r.recognize_google(audio,key='Mykey',language='en-US')
    return text

def read_english():
    play_sound(‘sound.wav’)

def to_jp():
    text_data = to_text()
    trans_ja = translator.translate(text_data,src='en',dest='ja')
    return trans_ja

def read_ja():
    text_data = to_jp()
    tts = gTTS(text=text_data, lang='ja')
    tts.save('jp.mp3')
    play_sound('jp.mp3')

def main_jp():
    rcd()
    read_ja()

def main_en():
    rcd()
    read_english()

if __name__ =='__main__':
    mode = input('mode:')
    if mode == 'ja':
        main_jp()
    elif mode =='en':
        main_en()
    else:
        pass

I submitted the assignment with this DFD7C828-421C-4334-A48B-4A05813B4FB4.png I could only pass 1/15 (T. T) Naturally, the results were also sloppy.

Finally

I think Python, which has a lot of modules, is suitable for automating school homework. Of course, my homeroom teacher told me a lot about this, With this as a trigger, he recommended the AO entrance exam to me, whose test results were not so good. As paper becomes less used in the future and the demand for electronic devices increases, the demand for programs will increase proportionally. I felt that Python wouldn't hurt to do it early on. b

Recommended Posts

[Python] Let's throw school tasks into Python [Efficiency]