Create convert_last_fmt.py
Browse files- convert_last_fmt.py +97 -0
convert_last_fmt.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import tempfile
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
##### 関数定義 #####
|
| 6 |
+
# 問題IDから選択肢IDを作成
|
| 7 |
+
def create_choice_id(question_id, num):
|
| 8 |
+
return str(question_id) + 'CH00' + str(num)
|
| 9 |
+
|
| 10 |
+
# 問題IDから正解IDを作成
|
| 11 |
+
def create_correct_id(question_id, num):
|
| 12 |
+
return str(question_id) + 'A00' + str(num)
|
| 13 |
+
|
| 14 |
+
# 最終FMTを作成
|
| 15 |
+
def convert_last_fmt_question(csv_file):
|
| 16 |
+
input = pd.read_csv(csv_file.name)
|
| 17 |
+
|
| 18 |
+
# 問題マスタの作成
|
| 19 |
+
output_question = pd.DataFrame({'問題ID': [], '知識ID': [], '出題形式ID': [], 'リード文':[],'問題': [
|
| 20 |
+
], '問題_翻訳': [], '正解文': [], '品詞': [], '解説テキスト': [],'リスト表示':[]})
|
| 21 |
+
|
| 22 |
+
# 問題マスタに行を追加
|
| 23 |
+
for index, row in input.iterrows():
|
| 24 |
+
new_row = pd.DataFrame({'問題ID': [row['問題ID']], '知識ID': [row['知識ID']], '出題形式ID': [row['出題形式ID']], 'リード文':[row['リード文']],'問題': [
|
| 25 |
+
row['問題']], '問題_翻訳': [row["問題_翻訳"]], '正解文': [row['正解文']], '品詞': [""], '解説テキスト': [row["解説テキスト"]],"リスト表示":[row["リスト表示"]]})
|
| 26 |
+
output_question = pd.concat([output_question, new_row], ignore_index=True)
|
| 27 |
+
|
| 28 |
+
# outputのindexを設定
|
| 29 |
+
output_question.set_index('問題ID', inplace=True)
|
| 30 |
+
|
| 31 |
+
# 問題IDの昇順で並び替え
|
| 32 |
+
output_question.sort_values(by='問題ID', ascending=True, inplace=True)
|
| 33 |
+
|
| 34 |
+
# 問題マスタをCSVファイルに出力
|
| 35 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.csv') as tmp:
|
| 36 |
+
output_question.to_csv(tmp.name, index=False, encoding='cp932', errors='ignore')
|
| 37 |
+
output_path = tmp.name
|
| 38 |
+
|
| 39 |
+
# ファイル名を変更
|
| 40 |
+
new_path = os.path.join(os.path.dirname(output_path), "question.csv")
|
| 41 |
+
os.rename(output_path, new_path)
|
| 42 |
+
return new_path
|
| 43 |
+
def convert_last_fmt_selection(csv_file):
|
| 44 |
+
input = pd.read_csv(csv_file.name)
|
| 45 |
+
# 選択肢マスタの作成
|
| 46 |
+
output_choice = pd.DataFrame({'選択肢ID': [], '問題ID': [], '選択肢': [], '表示順': []})
|
| 47 |
+
# 選択肢マスタに行を追加
|
| 48 |
+
for index, row in input.iterrows():
|
| 49 |
+
# 選択肢配列を作成
|
| 50 |
+
selection = row["選択肢"].split("/")
|
| 51 |
+
# 選択肢配列から選択肢を取り出し、行を追加
|
| 52 |
+
for i in range(len(selection)):
|
| 53 |
+
new_row = pd.DataFrame({'選択肢ID': [create_choice_id(
|
| 54 |
+
row['問題ID'], i+1)], '問題ID': [row['問題ID']], '選択肢': [selection[i]], '表示順': [i+1]})
|
| 55 |
+
output_choice = pd.concat([output_choice, new_row], ignore_index=True)
|
| 56 |
+
# outputのindexを設定
|
| 57 |
+
output_choice.set_index('選択肢ID', inplace=True)
|
| 58 |
+
# 選択肢IDの昇順で並び替え
|
| 59 |
+
output_choice.sort_values(by='選択肢ID', ascending=True, inplace=True)
|
| 60 |
+
# 選択肢マスタをCSVファイルに出力
|
| 61 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.csv') as tmp:
|
| 62 |
+
output_choice.to_csv(tmp.name, index=False, encoding='cp932', errors='ignore')
|
| 63 |
+
output_path = tmp.name
|
| 64 |
+
|
| 65 |
+
# ファイル名を変更
|
| 66 |
+
new_path = os.path.join(os.path.dirname(output_path), "choices.csv")
|
| 67 |
+
os.rename(output_path, new_path)
|
| 68 |
+
return new_path
|
| 69 |
+
def convert_last_fmt_correct(csv_file):
|
| 70 |
+
input = pd.read_csv(csv_file.name)
|
| 71 |
+
# 正解選択肢マスタの作成
|
| 72 |
+
output_correct_choice = pd.DataFrame(
|
| 73 |
+
{'正解ID': [], '問題ID': [], '正解': [], '並び順': []})
|
| 74 |
+
# 正解選択肢マスタに行を追加
|
| 75 |
+
for index, row in input.iterrows():
|
| 76 |
+
# 正解の配列を作成
|
| 77 |
+
correct = row["正解"].split("/")
|
| 78 |
+
# 正解の配列から正解を取り出し、行を追加
|
| 79 |
+
for i in range(len(correct)):
|
| 80 |
+
new_row = pd.DataFrame({'正解ID': [create_correct_id(
|
| 81 |
+
row['問題ID'], i+1)], '問題ID': [row['問題ID']], '正解': [correct[i]], '並び順': [i+1]})
|
| 82 |
+
output_correct_choice = pd.concat(
|
| 83 |
+
[output_correct_choice, new_row], ignore_index=True)
|
| 84 |
+
# outputのindexを設定
|
| 85 |
+
output_correct_choice.set_index('正解ID', inplace=True)
|
| 86 |
+
|
| 87 |
+
# 正解IDの昇順で並び替え
|
| 88 |
+
output_correct_choice.sort_values(by='正解ID', ascending=True, inplace=True)
|
| 89 |
+
# 選択肢マスタをCSVファイルに出力
|
| 90 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.csv') as tmp:
|
| 91 |
+
output_correct_choice.to_csv(tmp.name, index=False, encoding='cp932', errors='ignore')
|
| 92 |
+
output_path = tmp.name
|
| 93 |
+
|
| 94 |
+
# ファイル名を変更
|
| 95 |
+
new_path = os.path.join(os.path.dirname(output_path), "correct.csv")
|
| 96 |
+
os.rename(output_path, new_path)
|
| 97 |
+
return new_path
|