【Python】英文から語順整序問題を作成する
import re
import random
# 神奈川県高校入試 R4_2022 [4] ア
sentence = "English is spoken by many people as their first languege."
# 文字列"."や"?"の前に空白を追加
sentence = sentence[:-1] + " " + sentence[-1]
# リストに変換
words = sentence.split()
words
#['English', 'is', 'spoken', 'by', 'many', 'people', 'as', 'their', 'first', 'languege', '.']
# 先頭の単語数をランダムで決める
rn =random.randrange(3)
# 単語抽出
firstwords =words[:rn]
endwords =words[rn:]
# リストwordsをランダムで並び替え
# 最終文字以外を並び替え
wordsr = random.sample(endwords[:-1], len(endwords[:-1]))
# 単語を並び替えて問題を作成
if rn == 0:
print("(" + ", ".join(wordsr) + " ) " + words[-1])
else:
print(" ".join(firstwords) + " ( " + ", ".join(wordsr) + " ) " + words[-1])
# (as, languege, their, many, by, is, people, first, spoken, English ) .
# English ( as, is, by, people, first, languege, spoken, many, their ) .
# English is ( as, people, languege, first, by, their, many, spoken ) .
【今後の作業内容】
現在のプログラムでは先頭の文字列も大文字になっている。ここは固有名詞については大文字で、それ以外は小文字に変換するプログラムを組む。
出力したデータから日本語訳、並び替え単語リスト、正解の英文のリストを作成し、csvで出力できるようにする。
複数の問題文を変換できるようにfor文で組み立てる。
この記事が気に入ったらサポートをしてみませんか?