前提
python初心者です
授業の一環で、flaskで俳句の季節類推システムを作っています。
入力された季語がどの季節に該当するかを判定し、もし、季節が判別しづらいものであったら(三春などに該当するものですが、ここでは詳細を省きます)、その季語をword2vecで、俳句中の他の単語との類似度から算出した値で決めるというものです
word2vecでの関数からのreturnを受け取る時に以下のエラーメッセージが発生しました。
rekigo = q[0]
IndexError: list index out of range
実現したいこと
エラーを消して、一連の流れを完遂させたい
エラーメッセージ
rekigo = q[0]
IndexError: list index out of range
該当のソースコード
python
1view.py 2 3import logging 4 5from flask import Blueprint, render_template, request, session 6from flask_wtf.csrf import CSRFError 7 8from apps.haiku.form import UploadhaikuForm 9from apps.haiku.season import seki 10from apps.haiku.setup_log import setup_log 11from apps.haiku.word2vec import simiweight, split_weight 12 13ha = Blueprint("haiku", __name__, template_folder="templates") 14 15@ha.route("/", methods=["GET", "POST"]) 16def detectkigo(): 17 r = [] 18 q = [] 19 form = UploadhaikuForm() 20 if request.method == "GET": 21 return render_template("haiku/index.html", form=form) 22 elif request.method == "POST" and form.validate: 23 session["postkigo"] = form.kigo # htmlからの入力 24 session["posthaiku"] = form.haiku # htmlからの入力 25 postkigo = session["postkigo"] 26 posthaiku = str(session["posthaiku"]) 27 season = seki(postkigo) 28 setup_log() 29 logging.debug("%s", season) 30 if season == "三春": 31 r = split_weight(postkigo, posthaiku, season) 32 nouns = r[0] 33 sword = r[1] 34 seasonname = r[2] 35 q = simiweight(nouns, sword, seasonname) 36 # return split_weight(kigo, haiku, season) 37 elif season == "三夏": 38 r = split_weight(postkigo, posthaiku, season) 39 nouns = r[0] 40 sword = r[1] 41 seasonname = r[2] 42 q = simiweight(nouns, sword, seasonname) 43 # return split_weight(kigo, haiku, season) 44 elif season == "三秋": 45 r = split_weight(postkigo, posthaiku, season) 46 nouns = r[0] 47 sword = r[1] 48 seasonname = r[2] 49 q = simiweight(nouns, sword, seasonname) 50 # return split_weight(kigo, haiku, season) 51 elif season == "三冬": 52 r = split_weight(postkigo, posthaiku, season) 53 nouns = r[0] 54 sword = r[1] 55 seasonname = r[2] 56 q = simiweight(nouns, sword, seasonname) 57 # return split_weight(kigo, haiku, season) 58 59 rekigo = q[0] 60 resum = q[1] 61 thse = q[2] # rekigo=春夏秋冬 resum=類似度合計値 62 return render_template( 63 "haiku/index.html", season=rekigo, sum=resum, three=thse, form=form 64 ) 65 66 if __name__ == "__main__": 67 ha.debug = True 68 ha.run(host="0.0.0.0", port=int("5000"), debug=True) 69 70 71@ha.errorhandler(CSRFError) 72def handle_csrf_error(e): 73 return render_template("haiku/csrf_error.html", reason=e.description), 400 74
python
1season.py 2 3from openpyxl import load_workbook 4 5def seki(kigo): 6 wb = load_workbook(r"C:/Users/81804/flaskbook/apps/haiku/季語.xlsx") 7 ws = wb["スクレイピング結果"] 8 ws = wb.active 9 target_string = kigo 10 season = "三秋" 11 for r in ws.columns: 12 for c in r: 13 # もしc.valueがtarget_valueと一致したらその行番号を返す 14 if c.value == target_string: 15 # セル番地を指定する:M10 16 v = c.row 17 b = int(v) 18 season = ws.cell(row=b, column=2).value 19 return season
python
1word2vec.py 2 3import MeCab 4from gensim.models import KeyedVectors 5 6model_dir = r"C:/Users/81804/flaskbook/apps/haiku/entity_vector.model.bin" 7model = KeyedVectors.load_word2vec_format(model_dir, binary=True) 8 9 10def split_weight(sword, text, season): 11 m = MeCab.Tagger("-Ochasen") 12 parse_result = [line.split() for line in m.parse(text).splitlines()][:-1] 13 nouns = [w[0] for w in parse_result if w[3].startswith("名詞")] 14 15 if sword in nouns: 16 num = nouns.index(sword) # 俳句中の季語の配列の格納場所を判定 17 18 seasoname = season 19 nouns.pop(num) # 季語を配列から除去 20 return nouns, sword, seasoname # 俳句中の名詞と季語を渡す 21 22def simiweight(nouns, sword, seasoname): 23 seasons = ["春", "夏", "秋", "冬"] 24 weight = [] 25 for e in seasons: 26 s = model.similarity(sword, e) 27 weight.append(s) 28 29 max = -1 30 i = 0 31 count = 0 32 for n in seasons: # nで季節を切り替え 33 sum = 0 34 for f in nouns: # 俳句中の単語 35 p = model.similarity(f, n) 36 sum = sum + p * weight[i] 37 if sum > max: 38 max = sum 39 count = count + 1 40 i = i + 1 41 season = seasoname 42 43 if count == 1: 44 se = "春" 45 elif count == 2: 46 se = "夏" 47 elif count == 3: 48 se = "秋" 49 elif count == 4: 50 se = "冬" 51 52 return se, max, season
python
1form.py 2 3from flask_wtf.form import FlaskForm 4from wtforms import TextAreaField 5from wtforms.fields.simple import SubmitField 6from wtforms.validators import Length 7 8 9class UploadhaikuForm(FlaskForm): 10 # ファイルフィールドに必要なバリデーションを設定する 11 kigo = TextAreaField( 12 "季語を入力してください:", validators=[Length(min=1, max=5, message="季語の長さにしてください")] 13 ) 14 haiku = TextAreaField( 15 "俳句を入力してください:", validators=[Length(min=8, max=20, message="俳句の長さにしてください")] 16 ) 17 submit = SubmitField("アップロード")
流れとしては、form.pyで受け取った季語と俳句を、view.pyにおいて、順にseason.pyで季語を季語リストから判定し、word2vec.pyのsplit_weightに渡し、その結果をsimiweightに渡し、q[]に格納されるといった感じです。
初めてpythonで書いたので、汚いところばかりですが、原因を教えていただけると幸いです。
試したこと
rekigo = q[0]
resum = q[1]
thse = q[2] の部分を、
rekigo, resum, thse = q とすると、
ValueError: not enough values to unpack (expected 3, got 0)
となり、
returnをもともと受け取れていないのかとも思います。
回答1件
あなたの回答
tips
プレビュー