リアルタイムで収集したツイートから、url, 返信, RTを除去した後にリスト内の重複を削除したものを表示しようとしたら、以下のエラーが出ました。
解決に向けてお知恵をお貸しいただけますと幸いです。宜しくお願いします。
エラー
Traceback (most recent call last): File "honban.py", line 149, in <module> stream.sample() File "/Users/macuser/Workspaces/jxpress/trendword/.direnv/python-3.7.3/lib/python3.7/site-packages/tweepy/streaming.py", line 449, in sample self._start(is_async) File "/Users/macuser/Workspaces/jxpress/trendword/.direnv/python-3.7.3/lib/python3.7/site-packages/tweepy/streaming.py", line 389, in _start self._run() File "/Users/macuser/Workspaces/jxpress/trendword/.direnv/python-3.7.3/lib/python3.7/site-packages/tweepy/streaming.py", line 320, in _run six.reraise(*exc_info) File "/Users/macuser/Workspaces/jxpress/trendword/.direnv/python-3.7.3/lib/python3.7/site-packages/six.py", line 693, in reraise raise value File "/Users/macuser/Workspaces/jxpress/trendword/.direnv/python-3.7.3/lib/python3.7/site-packages/tweepy/streaming.py", line 289, in _run self._read_loop(resp) File "/Users/macuser/Workspaces/jxpress/trendword/.direnv/python-3.7.3/lib/python3.7/site-packages/tweepy/streaming.py", line 351, in _read_loop self._data(next_status_obj) File "/Users/macuser/Workspaces/jxpress/trendword/.direnv/python-3.7.3/lib/python3.7/site-packages/tweepy/streaming.py", line 323, in _data if self.listener.on_data(data) is False: File "/Users/macuser/Workspaces/jxpress/trendword/.direnv/python-3.7.3/lib/python3.7/site-packages/tweepy/streaming.py", line 54, in on_data if self.on_status(status) is False: File "honban.py", line 70, in on_status for i in corpus : UnboundLocalError: local variable 'corpus' referenced before assignment
コード
renshu.py
1#上記省略 2 def on_status(self, status): 3 text = str(status.text) 4 5#日本語ツイートをファイルに書き込んでいる + ツイートの数を表示 6 if status.lang == "ja": 7 with open("testd.txt", "a", encoding="utf-8") as f: 8 f.write(text) 9 with codecs.open("testd.txt", "r", "utf-8") as f: 10 num_lines = sum(1 for line in open("testd.txt"))#総文書数 11 corpus = f.read().split("\n") 12 if len(text) != 0: 13 self.count += 1 14 15 mecab = MeCab('-d /usr/local/lib/mecab/dic/mecab-ipadic-neologd') 16 17 stop_words = [] 18 path = 'stop_words.txt' 19 with open(path) as g: 20 stop_words = g.readlines() 21 22 texts=[] 23 for i in corpus: 24 texts.append(re.sub(r"http\S+", "", i)) 25 texts.append(re.sub(r"@(\w+) ", "", i)) 26 texts.append(re.sub(r"(^RT.*)", "", i, flags=re.MULTILINE | re.DOTALL)) 27 emoji_pattern = re.compile("[" 28 u"\U0001F600-\U0001F64F" 29 u"\U0001F300-\U0001F5FF" 30 u"\U0001F680-\U0001F6FF" 31 u"\U0001F1E0-\U0001F1FF" 32 "]+", flags=re.UNICODE) 33 texts.append(emoji_pattern.sub("", i)) 34 texts.append(i) 35 text = list(set(texts)) 36 print(text)
補足情報(FW/ツールのバージョンなど)
iOS 10.14.6, Python 3.7.4, Atom
回答1件
あなたの回答
tips
プレビュー