見出し画像

LLMを活用して単語帳を作ってAnkiに入れよう



はじめに

前回のエントリで、通訳をするうえで「文章を迷いなく理解する」ことの大事さについて書きました。「文章を迷いなく理解する」には、文章の構成要素である「単語」を一つ一つ理解することが重要です。このエントリではLLMを活用して単語の理解力を高めていく方法について記載します。具体的には、LLMを活用して複数の例文を生成し、単語帳を作成してAnkiにインポートします。

ちなみに、LLM(生成AI)の技術やAnkiという単語アプリをほぼ説明無しで使っているので、読者の8割ぐらいは途中から何が起こっているのか分からないかもしれません。ごめんなさい。後半は技術ヲタク向けに書きます。

ぜんかいのつづき

ある単語への"迷い度"を減らすことを裏返すと、ある特定の単語に対する"自信度"を上げることになります。
ある単語に対する自信度を上げるためには、

  • 単語を含む文章を聞く(読む)→ イメージを形成する → そのイメージを介してターゲット言語にうまく訳出する

という成功体験を積み重ねる必要があります。

つまり、1つの例文で1回訓練するだけでは足りません。7つぐらいの例文を繰り返し練習し、単語のイメージを定着させる必要があります。

例文を大量に読んで、単語の自信度をUPさせる!

例えば、SVL12000のLevel 3で出てくる「barren」という単語を考えてみましょう。この単語で訓練する前の僕の理解は、「不毛な」という理解でした。不毛な土地、みたいな。まぁ、正しいんですけど、この単語が通訳の現場で出てきたら百発百中で適切な日本語に訳せるか?と言われると自信は無いです。つまり自信度が低いのです。それでは例文で確認してみましょう。

barrenを使った例文を7つ出します。各文章を 
 1回だけ読む→すぐ目を離す→日本語に訳す
を繰り返してみます。

1. The desert was barren with not a single plant in sight.
2. Her creativity felt barren as she struggled to come up with new ideas.
3. The abandoned town was barren, with empty streets and boarded-up buildings.
4. The rocky terrain was barren, making it difficult for anything to grow.
5. The relationship had become barren, with no love or affection left between them.
6. The barren wasteland stretched for miles, devoid of any signs of life.
7. The once lush garden now looked barren and neglected.

どうです?「不毛」という表面的な理解だと、訳しづらい文章もあります。2.とかは「彼女は新しいアイデアが思いつかず、自分の想像力の足りなさを実感した」でも良いと思います。「彼女はアイデアを考えようとしたものの、想像力が働かなくてうろたえた」と文脈によっては思いっきり意訳しても良いでしょう。直訳的な感じでいくなら「彼女は新しいアイデアが思いつかず、想像力が枯渇しているように感じた」でも良いと思います。

文脈によってふさわしい訳し方は変わりますが、ポイントは文章を一度だけ読み、頭にイメージをくっきりと残し、それを自分なりの自然な日本語で訳出できるようにすることです。

ChatGPTのAPIを使って例文を大量に生成する

上記のような例文を大量に自動生成する方法について記載します。

ここからはプログラミングの知識が必要です。すみません。
Pythonファイルを実行できる環境がある人は、次のステップを踏むと、ある単語に対して例文をCSV形式で大量に生成することができます。

Step1: 覚えたい単語をCSVファイルのA列に列挙する

A列に例文を生成したい単語を列挙してinput.csvという名前で保存します。A1は適当にヘッダー名をつけてください。

1000個ほど列挙してみます

Step2: 例文を生成するプロンプトを用意する

A列に対して処理を行いたいプロンプトを用意します。
- 例文を7つ生成する
- 言葉の意味を説明する
- よくあるコロケーションを列挙する
- 各例文の日本語訳を生成する
の4つを実行してくれる以下のプロンプトを用意します。

Give 7 example sentences that showcase the meaning and usage of the word '{word}', then explain the meaning of the word, then list the most common collocations of the word with line breaks after every collocation, and then provide the Japanese translations of all the example sentences at the end.

Step3: Pythonでコードを書く

このエントリの一番下にコードを貼り付けたので、引用したい人は一番したまでスクロールしてください。OpenAIのChatGPTのAPIキーも必要です。
以下のようになります。(隠している部分はAPIキー)

Step4: Pythonのコードを実行する

するとoutput.csvが出力されます。若干日本語がおかしい部分がありますが、英文はほぼ違和感がありません。

▼出力結果(スプレッドシート)

▼出力結果(抜粋)

Step5: Ankiにimportする

Step4で生成したCSVを、Ankiにインポートします。
デッキを開いて学習開始すると、以下のようになります。

Ankiにimportするとこんな感じで使える

こんな感じで、barrenという単語を見てイメージを想起できるか確認し、さらに例文を表示して全文をスラスライメージできるか確認していきます。

余談:APIの利用料はいくらだった?

1000個の単語に対して例文を作らせた結果、0.73 USDでした。約110円です。安いと思います。
モデルは安価なgpt-3.5-turbo-0125を使いました。

$0.73!!! 1000個でこれは安いのでは。

おわりに

こんな感じで、ChatGPTのAPIを使って例文生成を行い、Ankiにぶちこんで通勤電車でトレーニングをすると意外と幸せになれるかもしれません。

おまけ:Pythonのコード

ほとんどChatGPT4に書いてもらったコードですが、正常に動作します!

from openai import OpenAI

# Set your OpenAI API key here
client = OpenAI(api_key='ChatGPTのAPIキーをここに貼り付け')
import pandas as pd


def generate_sentences(word):
    try:
        response = client.chat.completions.create(model="gpt-3.5-turbo",  # Using GPT-3.5-Turbo model
        messages=[
            {"role": "user", "content": f"Give 7 example sentences that showcase the meaning and usage of the word '{word}', and then explain the meaning of the word, and then list the most common collocations of the word with line breaks after every collocation, and then provide the Japanese translations of all the example sentences at the end.:"}
        ])
        # Adjusted response parsing according to the new structure
        return response.choices[0].message.content
    except Exception as e:
        print(f"Error generating sentences for {word}: {str(e)}")
        return None

def main():
    # Load the vocabulary words from a CSV file
    try:
        df = pd.read_csv('input.csv')  # Ensure your 2words are in the correct column
    except Exception as e:
        print(f"Failed to read CSV file: {str(e)}")
        return

    # Check if 'Column A' exists, if not, attempt to use the first column
    if 'Column A' not in df.columns:
        print("Column 'Column A' not found. Using the first column instead.")
        column_name = df.columns[0]  # Use the first column if 'Column A' is not found
    else:
        column_name = 'Column A'

    words = df[column_name]

    # Dictionary to hold the vocabulary and sentences
    data = {'Vocabulary Word': [], 'Example Sentences': []}

    # Generate sentences for each word
    for word in words:
        sentences = generate_sentences(word)
        data['Vocabulary Word'].append(word)
        data['Example Sentences'].append(sentences)
        print(f"Processed {word}")

    # Create a DataFrame and save to CSV
    output_df = pd.DataFrame(data)
    output_df.to_csv('output.csv', index=False)
    print("Output saved to output.csv")

if __name__ == "__main__":
    main()


この記事が気に入ったらサポートをしてみませんか?