質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

3497閲覧

pythonのコードについてです

kohekoh

総合スコア140

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2017/06/09 08:13

pythonに関してです

python

1import logging 2logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO) 3 4from gensim import corpora 5import csv 6 7#ファイル読み込み 8kakunou = open('example.txt') 9documents = [] 10for i in kakunou: 11 documents.append(i) 12 13# remove common words and tokenize 14stoplist = set('for a of the and to in'.split()) 15texts = [[word for word in document.lower().split() if word not in stoplist] 16 for document in documents] 17 18# remove words that appear only once 19from collections import defaultdict 20 21frequency = defaultdict(int) 22for text in texts: 23 for token in text: 24 frequency[token] += 1 25texts = [[token for token in text if frequency[token] > 1] 26 for text in texts] 27 28dictionary = corpora.Dictionary(texts) 29dictionary.save('/tmp/deerwester.dict') # store the dictionary, for future reference 30 31corpus = [dictionary.doc2bow(text) for text in texts] 32corpora.MmCorpus.serialize('/tmp/deerwester.mm', corpus) # store to disk, for later use 33 34with open('corpus1.csv','w') as x: 35 writer = csv.writer(x) 36 writer.writerows(corpus) 37 x.close() 38 39kakunou.close()

このコードを実行すると

python

12017-06-09 17:07:29,112 : INFO : 'pattern' package not found; tag filters are not available for English 22017-06-09 17:07:31,915 : INFO : adding document #0 to Dictionary(0 unique tokens: []) 32017-06-09 17:07:31,916 : INFO : built Dictionary(102 unique tokens: ['product/productid:', 'b003ai2vga', 'review/userid:', 'review/profilename:', 'review/helpfulness:']...) from 27 documents (total 333 corpus positions) 42017-06-09 17:07:31,916 : INFO : saving Dictionary object under /tmp/deerwester.dict, separately None 5Traceback (most recent call last): 6 File "C:\Users\ユーザ名\AppData\Local\conda\conda\envs\anaconda\lib\site-packages\gensim\utils.py", line 493, in save 7 _pickle.dump(self, fname_or_handle, protocol=pickle_protocol) 8TypeError: file must have a 'write' attribute 9 10During handling of the above exception, another exception occurred: 11 12Traceback (most recent call last): 13 File "corpus(new).py", line 29, in <module> 14 dictionary.save('/tmp/deerwester.dict') # store the dictionary, for future reference 15 File "C:\Users\ユーザ名\AppData\Local\conda\conda\envs\anaconda\lib\site-packages\gensim\utils.py", line 497, in save 16 pickle_protocol=pickle_protocol) 17 File "C:\Users\ユーザ名\AppData\Local\conda\conda\envs\anaconda\lib\site-packages\gensim\utils.py", line 369, in _smart_save 18 pickle(self, fname, protocol=pickle_protocol) 19 File "C:\Users\ユーザ名\AppData\Local\conda\conda\envs\anaconda\lib\site-packages\gensim\utils.py", line 919, in pickle 20 with smart_open(fname, 'wb') as fout: # 'b' for binary, needed on Windows 21 File "C:\Users\ユーザ名\AppData\Local\conda\conda\envs\anaconda\lib\site-packages\smart_open\smart_open_lib.py", line 140, in smart_open 22 return file_smart_open(parsed_uri.uri_path, mode) 23 File "C:\Users\ユーザ名\AppData\Local\conda\conda\envs\anaconda\lib\site-packages\smart_open\smart_open_lib.py", line 644, in file_smart_open 24 return compression_wrapper(open(fname, mode), fname, mode) 25FileNotFoundError: [Errno 2] No such file or directory: '/tmp/deerwester.dict'

このようなエラーをはかれます

gensimのチュートリアルをほとんどそのまま用いているものです

どうすればよろしいですか

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

FileNotFoundError: [Errno 2] No such file or directory: '/tmp/deerwester.dict'

推測ですが、あなたは、Windows環境でスクリプトを動かしているので、そもそも /tmp などというディレクトリ(フォルダ)が存在しないので上記のようなエラーが出てるんじゃないでしょうか。

試しに下記のように変更してみてください。

# /tmp/をなくす dictionary.save('deerwester.dict') # store the dictionary, for future reference corpus = [dictionary.doc2bow(text) for text in texts] # /tmp/をなくす corpora.MmCorpus.serialize('deerwester.mm', corpus) # store to disk, for later use

保存先の /tmp/ を削除することで、スクリプトを実行しているカレントのディレクトリ(フォルダ)にそれぞれファイルが生成されて上記エラーが回避できそうな気がします。

投稿2017/06/09 08:24

tell_k

総合スコア2120

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kohekoh

2017/06/09 08:40

回答ありがとうございます。 それは考えて、tmpを抜かして実行しました しかし、 PermissionError: [Errno 13] Permission denied: '/deerwester.dict' このようなエラーが発生してしまいます
tell_k

2017/06/09 08:53

/deerwester.dict というエラーですが、先頭の / は抜いたけどダメだったていうことでしょうか?もしそうでなければ / も削除してみてください。
kohekoh

2017/06/09 09:03

できました! ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問