質問編集履歴
1
ご回答を受けて修正・新たな問題の追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -130,6 +130,90 @@
|
|
130
130
|
|
131
131
|
|
132
132
|
|
133
|
+
###ご回答を受けて修正・新たな問題
|
134
|
+
|
135
|
+
```Python
|
136
|
+
|
137
|
+
import csv
|
138
|
+
|
139
|
+
f = open("sample.csv", encoding='utf-8' )
|
140
|
+
|
141
|
+
lines = f.readlines()
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
#行の長さを確認する
|
146
|
+
|
147
|
+
import pandas as pd
|
148
|
+
|
149
|
+
import numpy as np
|
150
|
+
|
151
|
+
print(len(lines))
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
dataset1 = pd.read_csv("sample.csv")
|
156
|
+
|
157
|
+
dataset1
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
#1行目からlen(lines)まで比較、2行目からlen(lines)まで比較を繰り返す
|
162
|
+
|
163
|
+
from nltk.corpus import wordnet as wn
|
164
|
+
|
165
|
+
for i in range(0, len(lines)-2):
|
166
|
+
|
167
|
+
for j in range(1, len(lines)-1):
|
168
|
+
|
169
|
+
similarity = wn.synsets(lines[i], lang='eng')[0].path_similarity(wn.synsets(lines[j], lang='eng')[0])
|
170
|
+
|
171
|
+
if similarity > 0.3:
|
172
|
+
|
173
|
+
print( lines[i] + '-' + lines[j] + similarity)
|
174
|
+
|
175
|
+
```
|
176
|
+
|
177
|
+
問題①
|
178
|
+
|
179
|
+
エラー文
|
180
|
+
|
181
|
+
以下のエラー文が新たに出力され、
|
182
|
+
|
183
|
+
IndexError: list index out of range
|
184
|
+
|
185
|
+
という意味が理解できず、修正方法がわかりません。
|
186
|
+
|
187
|
+
```
|
188
|
+
|
189
|
+
IndexError Traceback (most recent call last)
|
190
|
+
|
191
|
+
<ipython-input-7-acc73adcca82> in <module>()
|
192
|
+
|
193
|
+
3 for i in range(0, len(lines)-2):
|
194
|
+
|
195
|
+
4 for j in range(1, len(lines)-1):
|
196
|
+
|
197
|
+
----> 5 similarity = wn.synsets(lines[i], lang='eng')[0].path_similarity(wn.synsets(lines[j], lang='eng')[0])
|
198
|
+
|
199
|
+
6 if similarity > 0.3:
|
200
|
+
|
201
|
+
7 print( lines[i] + '-' +lines[j] + similarity)
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
IndexError: list index out of range
|
206
|
+
|
207
|
+
````
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
②
|
212
|
+
|
213
|
+
あとは、例えば「りんご - みかん」の類似度計算が何らかの理由でエラーになってしまった場合、現状のコードでは、そのあとの「りんご - ねこ」の計算は実行されないと思うのですが、例外処理はどこに書くことになるでしょうか。
|
214
|
+
|
215
|
+
|
216
|
+
|
133
217
|
### 補足情報(FW/ツールのバージョンなど)
|
134
218
|
|
135
219
|
Python 3.6.3
|