現在HP(http://sandmark.hateblo.jp/entry/2017/10/09/040243)を参考に、人工知能を作ろうと思っています。
その途中でpythonファイルの中身をコマンドプロンプトで実行しようとすると件名のようなエラーが出てきます。
エラーの詳細は
>>> from dictionary import Dictionary >>> dict = Dictionary() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Users\聡一朗\repos\unmo\dictionary.py", line 24, in __init__ pattern, phrases = line.strip().split('\t') AttributeError: 'int' object has no attribute 'strip'
です。下記にdictionary.pyとdics_pattern.txtを掲載しておきます。
dictionary.py class Dictionary: """思考エンジンの辞書クラス。 クラス変数: DICT_RANDOM -- ランダム辞書のファイル名 DICT_PATTERN -- パターン辞書のファイル名 プロパティ: random -- ランダム辞書 pattern -- パターン辞書 """ DICT_RANDOM = 'dics_random.txt' DICT_PATTERN = 'dics_pattern.txt' def __init__(self): """ファイルから辞書の読み込みを行う。""" with open(Dictionary.DICT_RANDOM, encoding='utf-8') as f: self._random = [x for x in f.read().splitlines() if x] self._pattern = [] with open(Dictionary.DICT_PATTERN, encoding='utf-8') as f: for line in f: pattern, phrases = line.strip().split('\t') if pattern and phrases: self._pattern.append({'pattern': pattern, 'phrases': phrases}) @property def random(self): """ランダム辞書""" return self._random @property def pattern(self): """パターン辞書""" return self._pattern
dics_pattern.txt チョコ(レート)? %match%おいしいよね|食べ過ぎると太るよ! 天気 明日晴れるといいなー
エラーが示す該当行や参照しているdics_pattern.txtには数字らしきものはないのになぜ'int'が含まれたこのようなエラーが出るのでしょうか。
------------以下追記-----------------
pdbモジュールを使用してデバックしたところ、lineの中身がdics_pattern.txtの{チョコ(レート)? %match%おいしいよね|食べ過ぎると太るよ!}となっていることが判明しました。とするとおそらく文字列を分割する役目を持つsplit関数が何らかの原因で動作しないため、{ValueError: not enough values to unpack (expected 2, got 1)}というエラーが出ると思われます。ちなみに件名のAttributeErrorの方はなぜだか出なくなりました。
回答2件
あなたの回答
tips
プレビュー