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のチュートリアルをほとんどそのまま用いているものです
どうすればよろしいですか
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/09 08:40
2017/06/09 08:53
2017/06/09 09:03