質問編集履歴
1
ご回答を受けて修正・新たな問題の追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -64,6 +64,48 @@
|
|
64
64
|
0.1111111111111111
|
65
65
|
```
|
66
66
|
|
67
|
+
###ご回答を受けて修正・新たな問題
|
68
|
+
```Python
|
69
|
+
import csv
|
70
|
+
f = open("sample.csv", encoding='utf-8' )
|
71
|
+
lines = f.readlines()
|
72
|
+
|
73
|
+
#行の長さを確認する
|
74
|
+
import pandas as pd
|
75
|
+
import numpy as np
|
76
|
+
print(len(lines))
|
77
|
+
|
78
|
+
dataset1 = pd.read_csv("sample.csv")
|
79
|
+
dataset1
|
80
|
+
|
81
|
+
#1行目からlen(lines)まで比較、2行目からlen(lines)まで比較を繰り返す
|
82
|
+
from nltk.corpus import wordnet as wn
|
83
|
+
for i in range(0, len(lines)-2):
|
84
|
+
for j in range(1, len(lines)-1):
|
85
|
+
similarity = wn.synsets(lines[i], lang='eng')[0].path_similarity(wn.synsets(lines[j], lang='eng')[0])
|
86
|
+
if similarity > 0.3:
|
87
|
+
print( lines[i] + '-' + lines[j] + similarity)
|
88
|
+
```
|
89
|
+
問題①
|
90
|
+
エラー文
|
91
|
+
以下のエラー文が新たに出力され、
|
92
|
+
IndexError: list index out of range
|
93
|
+
という意味が理解できず、修正方法がわかりません。
|
94
|
+
```
|
95
|
+
IndexError Traceback (most recent call last)
|
96
|
+
<ipython-input-7-acc73adcca82> in <module>()
|
97
|
+
3 for i in range(0, len(lines)-2):
|
98
|
+
4 for j in range(1, len(lines)-1):
|
99
|
+
----> 5 similarity = wn.synsets(lines[i], lang='eng')[0].path_similarity(wn.synsets(lines[j], lang='eng')[0])
|
100
|
+
6 if similarity > 0.3:
|
101
|
+
7 print( lines[i] + '-' +lines[j] + similarity)
|
102
|
+
|
103
|
+
IndexError: list index out of range
|
104
|
+
````
|
105
|
+
|
106
|
+
②
|
107
|
+
あとは、例えば「りんご - みかん」の類似度計算が何らかの理由でエラーになってしまった場合、現状のコードでは、そのあとの「りんご - ねこ」の計算は実行されないと思うのですが、例外処理はどこに書くことになるでしょうか。
|
108
|
+
|
67
109
|
### 補足情報(FW/ツールのバージョンなど)
|
68
110
|
Python 3.6.3
|
69
111
|
Mac OS High Sierra
|