forked from sunlightlabs/fcc-net-neutrality-comments
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform_corpus.py
61 lines (32 loc) · 1.08 KB
/
transform_corpus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# coding: utf-8
# In[1]:
import sys
import os
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), os.path.pardir))
# In[2]:
import logging
logging.basicConfig(filename='log/transform_corpus.log', format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
# In[3]:
import settings
#reload(settings)
# In[7]:
from gensim import corpora
from gensim.corpora import dictionary
from gensim import models
if len(sys.argv) > 1:
fname_suffix = sys.argv[1]
else:
fname_suffix = ''
# In[6]:
corpus_fname = 'corpus' + fname_suffix + '.mm'
tfidf_corpus_fname = 'tfidf_corpus' + fname_suffix + '.mm'
my_dict = dictionary.Dictionary.load(os.path.join(settings.PERSIST_DIR, 'my_dict'))
corpus = corpora.MmCorpus(os.path.join(settings.PERSIST_DIR, corpus_fname))
# In[8]:
tfidf = models.TfidfModel(corpus)
# In[10]:
tfidf_corpus = tfidf[corpus]
tfidf.save(os.path.join(settings.PERSIST_DIR, 'tfidf_model' + fname_suffix ))
# In[11]:
corpora.MmCorpus.serialize(os.path.join(settings.PERSIST_DIR, tfidf_corpus_fname),
tfidf_corpus)