回答編集履歴
3
リファクタ
test
CHANGED
@@ -2,19 +2,19 @@
|
|
2
2
|
|
3
3
|
```Python
|
4
4
|
|
5
|
-
d
|
5
|
+
words_to_synonym = {}
|
6
6
|
|
7
|
-
for d
|
7
|
+
for word in words:
|
8
8
|
|
9
|
-
syns
|
9
|
+
synonyms = wn.synsets(word, lang='eng')
|
10
10
|
|
11
|
-
if syns
|
11
|
+
if synonyms:
|
12
12
|
|
13
|
-
d
|
13
|
+
words_to_synonym[word] = synonyms[0]
|
14
14
|
|
15
15
|
|
16
16
|
|
17
|
-
print(d
|
17
|
+
print(words_to_synonym)
|
18
18
|
|
19
19
|
```
|
20
20
|
|
@@ -26,25 +26,25 @@
|
|
26
26
|
|
27
27
|
```Python
|
28
28
|
|
29
|
-
words_matrix = {d
|
29
|
+
words_matrix = {word: {} for word in words_to_synonym}
|
30
30
|
|
31
31
|
|
32
32
|
|
33
|
-
it = product(d
|
33
|
+
it = product(words_to_synonym.items(), repeat=2)
|
34
34
|
|
35
|
-
for (d
|
35
|
+
for (word_x, synonym_x), (word_y, synonym_y) in it:
|
36
36
|
|
37
|
-
if d
|
37
|
+
if word_x is word_y:
|
38
38
|
|
39
39
|
continue
|
40
40
|
|
41
41
|
|
42
42
|
|
43
|
-
deg_similarity = syn
|
43
|
+
deg_similarity = synonym_x.path_similarity(synonym_y)
|
44
44
|
|
45
45
|
if deg_similarity > 0:
|
46
46
|
|
47
|
-
words_matrix[d
|
47
|
+
words_matrix[word_x][word_y] = deg_similarity
|
48
48
|
|
49
49
|
|
50
50
|
|
@@ -54,7 +54,7 @@
|
|
54
54
|
|
55
55
|
|
56
56
|
|
57
|
-
そしたらこんな感じの結果が出ます。(出力
|
57
|
+
そしたらこんな感じの結果が出ます。(出力は少し成形しています。)
|
58
58
|
|
59
59
|
```plain
|
60
60
|
|
2
コードの修正
test
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
```Python
|
4
4
|
|
5
|
-
data_
|
5
|
+
data_to_synset = {}
|
6
6
|
|
7
7
|
for datum in data:
|
8
8
|
|
@@ -10,38 +10,92 @@
|
|
10
10
|
|
11
11
|
if synset:
|
12
12
|
|
13
|
-
data_
|
13
|
+
data_to_synset[datum] = synset[0]
|
14
14
|
|
15
15
|
|
16
16
|
|
17
|
-
print(data_
|
17
|
+
print(data_to_synset)
|
18
18
|
|
19
19
|
```
|
20
20
|
|
21
21
|
|
22
22
|
|
23
|
-
このように単語と
|
23
|
+
このようにキーとして単語を、値として単語に対する第一synsetを持つ辞書を作っておきます。
|
24
|
+
|
25
|
+
そして、各要素の直積を知りたいので、[itertools.product](https://docs.python.jp/3/library/itertools.html#itertools.product)を使います。
|
26
|
+
|
27
|
+
```Python
|
28
|
+
|
29
|
+
words_matrix = {datum: {} for datum in data_to_synset}
|
24
30
|
|
25
31
|
|
26
32
|
|
27
|
-
---
|
28
|
-
|
29
|
-
そして、各要素の直積を知りたいので、itertools.productを使います。
|
30
|
-
|
31
|
-
```Python
|
32
|
-
|
33
|
-
it = product(data_
|
33
|
+
it = product(data_to_synset.items(), repeat=2)
|
34
34
|
|
35
35
|
for (datum_x, synset_x), (datum_y, synset_y) in it:
|
36
36
|
|
37
|
-
|
37
|
+
if datum_x is datum_y:
|
38
|
+
|
39
|
+
continue
|
38
40
|
|
39
41
|
|
40
42
|
|
43
|
+
deg_similarity = synset_x.path_similarity(synset_y)
|
44
|
+
|
45
|
+
if deg_similarity > 0:
|
46
|
+
|
47
|
+
words_matrix[datum_x][datum_y] = deg_similarity
|
48
|
+
|
49
|
+
|
50
|
+
|
41
|
-
|
51
|
+
print(words_matrix)
|
42
52
|
|
43
53
|
```
|
44
54
|
|
45
55
|
|
46
56
|
|
47
|
-
この
|
57
|
+
そしたらこんな感じの結果が出ます。(出力を少し成形しています。)
|
58
|
+
|
59
|
+
```plain
|
60
|
+
|
61
|
+
{'cats': {'clocks': 0.06666666666666667,
|
62
|
+
|
63
|
+
'cloud': 0.05555555555555555,
|
64
|
+
|
65
|
+
'orange': 0.05263157894736842,
|
66
|
+
|
67
|
+
'pigs': 0.125},
|
68
|
+
|
69
|
+
'clocks': {'cats': 0.06666666666666667,
|
70
|
+
|
71
|
+
'cloud': 0.0625,
|
72
|
+
|
73
|
+
'orange': 0.058823529411764705,
|
74
|
+
|
75
|
+
'pigs': 0.0625},
|
76
|
+
|
77
|
+
'cloud': {'cats': 0.05555555555555555,
|
78
|
+
|
79
|
+
'clocks': 0.0625,
|
80
|
+
|
81
|
+
'orange': 0.07692307692307693,
|
82
|
+
|
83
|
+
'pigs': 0.05263157894736842},
|
84
|
+
|
85
|
+
'orange': {'cats': 0.05263157894736842,
|
86
|
+
|
87
|
+
'clocks': 0.058823529411764705,
|
88
|
+
|
89
|
+
'cloud': 0.07692307692307693,
|
90
|
+
|
91
|
+
'pigs': 0.05},
|
92
|
+
|
93
|
+
'pigs': {'cats': 0.125,
|
94
|
+
|
95
|
+
'clocks': 0.0625,
|
96
|
+
|
97
|
+
'cloud': 0.05263157894736842,
|
98
|
+
|
99
|
+
'orange': 0.05}}
|
100
|
+
|
101
|
+
```
|
1
修正
test
CHANGED
@@ -26,11 +26,13 @@
|
|
26
26
|
|
27
27
|
---
|
28
28
|
|
29
|
-
そして、各
|
29
|
+
そして、各要素の直積を知りたいので、itertools.productを使います。
|
30
30
|
|
31
31
|
```Python
|
32
32
|
|
33
|
+
it = product(data_with_synset, repeat=2)
|
34
|
+
|
33
|
-
for (datum_x, synset_x), (datum_y, synset_y) in
|
35
|
+
for (datum_x, synset_x), (datum_y, synset_y) in it:
|
34
36
|
|
35
37
|
print(datum_x, datum_y)
|
36
38
|
|
@@ -42,4 +44,4 @@
|
|
42
44
|
|
43
45
|
|
44
46
|
|
45
|
-
このループ内で後は条件を満たす
|
47
|
+
このループ内で後は条件を満たすものを抽出すれば良いです。
|