質問編集履歴

2

修正

2017/07/24 13:28

投稿

kohekoh
kohekoh

スコア140

test CHANGED
File without changes
test CHANGED
@@ -141,3 +141,9 @@
141
141
  tfidf()
142
142
 
143
143
  ```
144
+
145
+
146
+
147
+ ちなみにこのテキストファイルは30万行ほどのテキストファイルです
148
+
149
+ このときCPU使用率は40%ほどです

1

修正

2017/07/24 13:28

投稿

kohekoh
kohekoh

スコア140

test CHANGED
File without changes
test CHANGED
@@ -25,3 +25,119 @@
25
25
  コードに何か問題があるのか
26
26
 
27
27
  他に何か考えられることはありますか?
28
+
29
+
30
+
31
+ ```python
32
+
33
+ import nltk
34
+
35
+ import numpy as np
36
+
37
+ import json
38
+
39
+ import nltk_exa as nl
40
+
41
+ import time
42
+
43
+ from multiprocessing import Pool
44
+
45
+ #import multiprocessing
46
+
47
+ import sys
48
+
49
+
50
+
51
+ def subcalc(word):
52
+
53
+ subdoc = []
54
+
55
+ lists = []
56
+
57
+ collection = nltk.TextCollection(word) #サイトにのっていた
58
+
59
+ #uniqterms = list(set(collection)) #ここも上と同じサイトに載っていた
60
+
61
+ wo=[]
62
+
63
+ for term in set(word):
64
+
65
+ if(collection.tf_idf(term, word) > 0):
66
+
67
+ wo.append([term,collection.tf_idf(term, word)]) #ここも上のサイトにのってる
68
+
69
+ #print(wo)
70
+
71
+ wo.sort(key=lambda x:x[1]) #keyに無名関数lambdaをいれてる woの1番目の要素(WO(1,2)だったら”2”)でソート
72
+
73
+ wo.reverse()
74
+
75
+ try:
76
+
77
+ slice1 = np.array(wo[:20]) #先頭の文字から終了インデックスまでが抽出
78
+
79
+ lists = slice1[:,0] #[:]は戦闘から終了のインデックスまで抽出と、slice1の0番目を格納
80
+
81
+ subdoc.append(list(lists)) #listsが文字列だから、リストに格納
82
+
83
+ del wo
84
+
85
+ except:
86
+
87
+ print(wo)
88
+
89
+ return subdoc
90
+
91
+
92
+
93
+ def tfidf():
94
+
95
+ t1 = time.time()
96
+
97
+ doc0 = []
98
+
99
+ doc = []
100
+
101
+ word0 = []
102
+
103
+ word = []
104
+
105
+ f = open("/Users//Dropbox/prg/dataset/word0_notuseful012.txt")
106
+
107
+ for row in f:
108
+
109
+ word0.append(row.split("]["))
110
+
111
+ f.close()
112
+
113
+ for i in word0[0]:
114
+
115
+ word.append(str(i).replace("[","").replace("]","").replace(",","").replace("'","").replace("\"","").split())
116
+
117
+ #word.pop()
118
+
119
+ ttt = time.time()
120
+
121
+ p = Pool(4)
122
+
123
+ #a = subcalc(word) #1コアによる実行
124
+
125
+ #print(a)
126
+
127
+ doc = p.map(subcalc, word) #複数コアによる実行
128
+
129
+ t3 = time.time()
130
+
131
+ #print(doc[0][0])
132
+
133
+ #print('processing time(nltkはこんだけかかってる)(終わり): ' + str(ttt - tt) + '(sec)')
134
+
135
+ print('processing time2(終わり): ' + str(t3 - ttt) + '(sec)')
136
+
137
+
138
+
139
+ if __name__ == "__main__":
140
+
141
+ tfidf()
142
+
143
+ ```