回答編集履歴

3

リファクタ

2018/06/25 15:45

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -2,19 +2,19 @@
2
2
 
3
3
  ```Python
4
4
 
5
- data_to_synset = {}
5
+ words_to_synonym = {}
6
6
 
7
- for datum in data:
7
+ for word in words:
8
8
 
9
- synset = wn.synsets(datum, lang='eng')
9
+ synonyms = wn.synsets(word, lang='eng')
10
10
 
11
- if synset:
11
+ if synonyms:
12
12
 
13
- data_to_synset[datum] = synset[0]
13
+ words_to_synonym[word] = synonyms[0]
14
14
 
15
15
 
16
16
 
17
- print(data_to_synset)
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 = {datum: {} for datum in data_to_synset}
29
+ words_matrix = {word: {} for word in words_to_synonym}
30
30
 
31
31
 
32
32
 
33
- it = product(data_to_synset.items(), repeat=2)
33
+ it = product(words_to_synonym.items(), repeat=2)
34
34
 
35
- for (datum_x, synset_x), (datum_y, synset_y) in it:
35
+ for (word_x, synonym_x), (word_y, synonym_y) in it:
36
36
 
37
- if datum_x is datum_y:
37
+ if word_x is word_y:
38
38
 
39
39
  continue
40
40
 
41
41
 
42
42
 
43
- deg_similarity = synset_x.path_similarity(synset_y)
43
+ deg_similarity = synonym_x.path_similarity(synonym_y)
44
44
 
45
45
  if deg_similarity > 0:
46
46
 
47
- words_matrix[datum_x][datum_y] = deg_similarity
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

コードの修正

2018/06/25 15:45

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ```Python
4
4
 
5
- data_with_synset = []
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_with_synset.append((datum, synset[0]))
13
+ data_to_synset[datum] = synset[0]
14
14
 
15
15
 
16
16
 
17
- print(data_with_synset)
17
+ print(data_to_synset)
18
18
 
19
19
  ```
20
20
 
21
21
 
22
22
 
23
- このように単語とその第一synsetのペアのリストを作っておきます。
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_with_synset, repeat=2)
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
- print(datum_x, datum_y)
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

修正

2018/06/25 15:39

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -26,11 +26,13 @@
26
26
 
27
27
  ---
28
28
 
29
- そして、各組み合わせを知りたいので、itertools.combinationsを使います。
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 combinations(data_with_synset, 2):
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
+ このループ内で後は条件を満たすものを抽出すれば良いです。