replica531’s diary

京工電電B1/KMC

英文テストプログラムを作成した

初めに

必修科目である「英語リーディング」の最終テストが、英文を暗記して先生の指定した和文に対応する英文を暗唱するというものであったため、33個の英文を覚えなくてはいけなくなりました。高校生の頃にも似たようなテストをさせられていたので、当時のようにひたすら書き殴って覚えようかと思ったのですが、折角プログラミングをしてきたのだから英文テストを作ってみようということで取り掛かりました。

1.基礎

ネットで「単語テスト プログラミング」と調べたらpythonでいい感じのが出てきたのでそれを基礎として作成していきます。このサイトです。
【Python】繰り返し処理(while)で簡単な英単語テストを作ろう│ProCity(プロシティ)

2.間違ってた単語を出力

元のコードでは間違っているということしか分からず、どの単語を間違ったのか探すのに苦労してしまったので、間違った単語を出力するプログラムを追加しました。答えにある単語が入力文になければその単語をwrong_wordsリストに追加して最後に出力する方式でやりました。これでかなり効率的に勉強できるようになりました。

完成コード

テストリストに和文とそれに対応した英文を入れて完成です。下のコードが完成コードになります。

import random
#テストリスト
test = {
           '1.バイデンが最も明らかに興味を持っている科学的研究分野は癌科学です' : "Biden's most obvious research interest has been in cancer science",
           '2.乳幼児の能力は、言語は思考に先行するという見方に挑戦をもたらす(そういった見方を覆す可能性がある)' : 'The capacities of infants pose a challenge for the view that language precedes thought',
           '3.今は受動的に投資をしている時ではない' : 'Now is not the time to be investing passively',
           '4.この技術がすぐに適用可能になる可能性は低く' : 'It is unlikely that this technology will be available anytime soon',
           '5.これは人口の増加と比例的増加の両方を反映している' : 'This reflects both population growth and a proportional increase',
           '6.白髪は経験と関連付けられる(経験を連想させる)' : 'grey hair could be associated with experience',
           '7.IQのスコアは認知能力を評価する傾向がある(評価することを意図している)' : 'Intelligence quotinent scores are intended to assess the cognitive competences',
           '8.調査によると、女性の65%が不眠症を経験している' : 'A survey found that 65% of women experience sleeplessness',
           '9.これらの学生は大学を中退する可能性が高い' : 'These students are more likely to drop out of university',
           '10.そのシートはスキンとして使用される可能性がある' : 'The sheets have the potential to be used as a skin',
           '11.調査結果は一般的なコンセンサスを示している' : 'The findings indicate the general consensus',
           '12.そのようなサービスを体験に適用できることは注目に値する' : 'It is worth nothing that such services can be applied on experiences',
           '13.これを克服する最も良い方法はより小さい量に分割することである' : 'The best way to overcome this is to break them down into smaller blocks',
           '14.犬の脳は顔に集中するようにはできていなかった' : 'Dog brains were not designed to focus on faces',
           '15.ベトナムは評価において高くランク付けされていなかった' : 'Vietnam did not rank high in evaluation',
           '16.彼らはこの銀河の闇の中心が存在することを証明するために、根本的な貢献をしてくれました' : 'They made fundamental contributions to establishing that these dark hearts of the galaxies exist',
           '17.まだあらゆる人がチームの見解に納得して(させられて)いるわけではない' : "Not everyone is yet convinced by the team's observation",
           '18.筆者はSTINGたんぱく質がバクテリアによって獲得された(に起源をもつ) という証拠を出した' : 'The authors provide evidence that the STING protein originated in bacteria',
           '19.NMTの成功は量と質に大きく依存する' : 'The success of NMT depends heavily on the quantity and quality',
           '20.裁判所は人権を理由としてデジタル技術の利用に歯止めをかけた' : 'A court has stopped the use of digital technologies on human rights grounds',
           '21.安倍政権が景気上昇や社会的発展の名のもとに行った変革は様々な課題を残している' : 'The changes Abe made in the name of economic growth and social development leave a mixed legacy',
           '22.その後に起こったのは、(急速な膨張期である)インフレーションでした' : 'What followed was inflation - a period of rapid expansion',
           '23.トランプ大統領は定期的に検査を受けていた' : 'Trump had access to regular testing',
           '24.彼らの研究は地球で最も原始的な生き物のうちの幾らかの素晴らしい生存能力に光を当てた' : "Their research sheds light on the remarkable survival power of some of Earth's most primitive species",
           '25.重要なことに、研究は肯定的、否定的影響の間の関連性を明らかにしている' : 'Crucially, studies revealed associations between positive and negative effects',
           '26.PAはうつ病の予防と治療の双方に良い効果をもたらすようである' : 'PA seems to provide positive effects on both prevention and treatment of depression',
           '27.研究は『単語埋め込み』と呼ばれる(として知られる)機械学習法に焦点を置いてる' : 'The research focuses on a machine learning tool known as "word embedding"',
           '28.私たちは自然の中に私たちが直面するほとんどどんな難題に対する解決策でも探すことができます' : 'We can look to nature for solutions to almost any challenge we confront',
           '29.神経科学は、研究者が意識的な意図の裏にある身体的過程を特定するのに役立つかもしれない' : 'Neuroscience could help researchers to identify the physical processes underlying conscious intention',
           '30.データは石油と同じように共有財産(の一形態)だ' : 'Data is no less a form of common property than oil',
           '31.学生達はそれぞれ異なる困難に置かれていることを考慮しなければならない' : 'It must be taken into consideration that students are unevenly challenged',
           '32.この能力を維持することは、人類の生存にとって不可欠である' : 'Maintaining this ability, it is essential to the survival of human race',
           '33.AIは最終製品としてみられるべきではなくむしろ手段としてみられるべきである' : 'AI should be viewed as not the end product, but rather means'
                                }

#間違っている単語リストを返す関数
def wrong_words(randomword,inputword):
    #答えと入力から単語を抽出
    words_answer=test[randomword].split()
    words_inputword=inputword.split()
    #間違っている単語を出力
    wrong_words=[]
    for s in words_answer:
        for t in words_inputword:
            if s==t:
                break
            if t==words_inputword[-1]:
                wrong_words.append(s)
    return wrong_words

count = 0
score = 0
while count<10:
    count += 1
    randomword = random.choice(list(test.keys()))
    print(randomword)
    inputword = input('英文を入力してください:')
    if inputword==test[randomword]:
        print('正解です。')
        score += 10
        if count==10:
            break
        else:
            continue
    else:
        print('不正解です。正解は%sです'%(test[randomword]))
        print(wrong_words(randomword,inputword),'を間違っています。')
        #復習
        inputword = input('正解を入力してください:')
        if inputword==test[randomword]:
            print('復習完了!')
        else:
            print('ちゃうで!')
            print(wrong_words(randomword,inputword),'を間違っています。')
        if count==10:
            break
        else:
            continue
#結果発表~
print('あなたの点数は%dでした。'%(score))

テスト結果

29番のそこそこ長い例文を出題されましたが、完璧に答えることができました。書き殴り勉強だと手が痛くなりますがタイピングだったらそんなことも特になかったので高校の頃から作っとけばよかったなあと感じました。何はともあれ無事にテストを終え、単位も獲得できたと思うのでよかったです!

おまけ

最初、「見たままモード」で編集してたらコードを埋め込むことができず、調べたら「はてな記法」で編集する必要がありました。それでコードの埋め込みはできたのですがコードが勝手に改行されたり、コードが全部表示され長すぎたりと不便だったので改善策を調べたら見ての通り、いい感じになったので共有しておきます。
まず、下のサイトを参考にして
はてなブログのコードの埋め込みの折り返しを無効にする - たねやつの木

pre, code {
    max-height    : 500px;
    overflow      : scroll;
    white-space   : pre !important;
    text-overflow : clip !important;
}

をデザイン>カスタマイズ>デザインCSSのところに追加しました。これだけだと勝手に改行されたままだったので、次のサイトを参考にして
はてなブログのスマホ表示で記事中のソースコードを折り返さないようにする - Sprint Life

<style>
  pre{
    white-space: pre;
  }

  .entry-content pre{
    word-wrap: normal;
  }
</style>

を設定>詳細設定>headに要素を追加 の部分に書くことでいい感じになりました。わーい