https://github.com/o-tomox/TextGenerator
のコードをPython3で使えるようにし,Djangoアプリ内で動かそうとすと,以下のようなエラーがました。
cannot use a string pattern on a bytes-like object
コマンドラインから実行する分には,全くエラーなしに動くのですが,どうしてDjangoではこのようなエラーが出るのでしょうか?
環境は
Python 3.6.3
Django 2.2.dev20180610131139
(必要なモジュールはインストールし,基本的に最新版です。)
エラーコードおよび動作している周辺のコードは以下の通りです。
/Users/Usename/webapp/app/views.py in sample triplet_freqs = chain.make_triplet_freqs() ... ▼ Local vars Variable Value chain <app.models.PrepareChain object at 0x10dcfc978> request <WSGIRequest: GET '/sample?your_name=%E6%96%87%E7%AB%A0%E3%82%92%E5%85%A5%E5%8A%9B%E3%81%97%E3%81%A6%E3%81%8F%E3%81%A0%E3%81%95%E3%81%84'> text (b'\xe6\x96\x87\xe7\xab\xa0\xe3\x82\x92\xe5\x85\xa5\xe5\x8a\x9b\xe3' b'\x81\x97\xe3\x81\xa6\xe3\x81\x8f\xe3\x81\xa0\xe3\x81\x95\xe3\x81\x84') ------------------------------------ /Users/Usename/webapp/app/models.py in make_triplet_freqs def make_triplet_freqs(self): u""" 形態素解析から3つ組の出現回数まで @return 3つ組とその出現回数の辞書 key: 3つ組(タプル) val: 出現回数 """ # 長い文章をセンテンス毎に分割 sentences = self._divide(self.text) ... # 3つ組の出現回数 triplet_freqs = defaultdict(int) # センテンス毎に3つ組にする for sentence in sentences: ▼ Local vars Variable Value self <app.models.PrepareChain object at 0x10dcfc978> ------------------------------------ /Users/Usename/webapp/app/models.py in _divide @param text 分割前の文章 @return 一文ずつの配列 """ # 改行文字以外の分割文字(正規表現表記) delimiter = u"。|.|." # 全ての分割文字を改行文字に置換(splitしたときに「。」などの情報を無くさないため) text = re.sub(u"({0})".format(delimiter), r"\1\n", text) ... # 改行文字で分割 sentences = text.splitlines() # 前後の空白文字を削除 sentences = [sentence.strip() for sentence in sentences] ▼ Local vars Variable Value delimiter '。|.|\.' self <app.models.PrepareChain object at 0x10dcfc978> text (b'\xe6\x96\x87\xe7\xab\xa0\xe3\x82\x92\xe5\x85\xa5\xe5\x8a\x9b\xe3' b'\x81\x97\xe3\x81\xa6\xe3\x81\x8f\xe3\x81\xa0\xe3\x81\x95\xe3\x81\x84') -------------------------------------- /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/re.py in sub def sub(pattern, repl, string, count=0, flags=0): """Return the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in string by the replacement repl. repl can be either a string or a callable; if a string, backslash escapes in it are processed. If it is a callable, it's passed the match object and must return a replacement string to be used.""" return _compile(pattern, flags).sub(repl, string, count) ▼ Local vars Variable Value count 0 flags 0 pattern '(。|.|\.)' repl '\1\n' string (b'\xe6\x96\x87\xe7\xab\xa0\xe3\x82\x92\xe5\x85\xa5\xe5\x8a\x9b\xe3' b'\x81\x97\xe3\x81\xa6\xe3\x81\x8f\xe3\x81\xa0\xe3\x81\x95\xe3\x81\x84')
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/08/27 12:11
2018/08/27 12:18
2018/08/27 12:19
2018/08/27 15:15
2018/08/27 17:00 編集
2018/08/28 01:34
2018/08/28 01:51
2018/08/28 08:58