回答編集履歴
4
色々
test
CHANGED
@@ -18,23 +18,21 @@
|
|
18
18
|
|
19
19
|
Postモデルと
|
20
20
|
|
21
|
-
|
21
|
+
```ruby
|
22
22
|
|
23
|
-
|
23
|
+
acts_as_taggable
|
24
24
|
|
25
25
|
before_save :standarize_text,:save_tags
|
26
26
|
|
27
27
|
def standarize_text
|
28
28
|
|
29
|
-
重複や入力ブレを治すため文字を大文字とかに直す半角カナも直す、nkfとか使って見てください
|
30
|
-
|
31
29
|
end
|
32
30
|
|
33
31
|
def save_tags
|
34
32
|
|
35
|
-
array=check_taggable_word(self.
|
33
|
+
array=check_taggable_word(self.title)
|
36
34
|
|
37
|
-
self.tag_list.add(array)
|
35
|
+
self.tag_list.add(array)
|
38
36
|
|
39
37
|
end
|
40
38
|
|
@@ -58,7 +56,45 @@
|
|
58
56
|
|
59
57
|
```
|
60
58
|
|
59
|
+
Commentモデルに
|
61
60
|
|
61
|
+
```ruby
|
62
|
+
|
63
|
+
before_save :standarize_text,:save_tags
|
64
|
+
|
65
|
+
def standarize_text
|
66
|
+
|
67
|
+
重複や入力ブレを治すため文字を大文字とかに直す半角カナも直す、nkfとか使って見てください
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
def save_tags
|
72
|
+
|
73
|
+
array=check_taggable_word(self.comment)
|
74
|
+
|
75
|
+
self.post.tag_list.add(array)
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
def check_taggable_word(text)
|
80
|
+
|
81
|
+
ary=Array.new
|
82
|
+
|
83
|
+
nm = Natto::MeCab.new
|
84
|
+
|
85
|
+
nm.parse(text) do |n|
|
86
|
+
|
87
|
+
ary<<n.surface
|
88
|
+
|
89
|
+
end //mekabuで要素に分解
|
90
|
+
|
91
|
+
tags=ActsAsTaggableOn::Tag.pluck(:name)//タグを取得
|
92
|
+
|
93
|
+
return ary & tags //共通部分を返す
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
```
|
62
98
|
|
63
99
|
こんな感じですかね
|
64
100
|
|
3
修正
test
CHANGED
@@ -40,13 +40,13 @@
|
|
40
40
|
|
41
41
|
def check_taggable_word(text)
|
42
42
|
|
43
|
-
ar
|
43
|
+
ary=Array.new
|
44
44
|
|
45
45
|
nm = Natto::MeCab.new
|
46
46
|
|
47
47
|
nm.parse(text) do |n|
|
48
48
|
|
49
|
-
ar
|
49
|
+
ary<<n.surface
|
50
50
|
|
51
51
|
end //mekabuで要素に分解
|
52
52
|
|
2
間違いを修正しました
test
CHANGED
@@ -3,6 +3,10 @@
|
|
3
3
|
gem 'natto'
|
4
4
|
|
5
5
|
をgemfileに追加
|
6
|
+
|
7
|
+
bundle install
|
8
|
+
|
9
|
+
rake acts_as_taggable_on_engine:install:migrations
|
6
10
|
|
7
11
|
rake db::migrateを実行
|
8
12
|
|
1
追記
test
CHANGED
@@ -61,3 +61,89 @@
|
|
61
61
|
mekabuライブラリで文章を単語に分解して作ったarrayと
|
62
62
|
|
63
63
|
tagに登録されている全ての単語のtagsの共通部分を取っています
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
あと新たなtagが追加されるたびに全てのコメントにそのタグがないかチェックして
|
68
|
+
|
69
|
+
追加する機能が必要です。
|
70
|
+
|
71
|
+
パッチを追加してact_as_taggle_onのtag classを拡張して見てください
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
|
75
|
+
module ActAsTaggleOnPatch
|
76
|
+
|
77
|
+
module TagPatch
|
78
|
+
|
79
|
+
def self.included(base)
|
80
|
+
|
81
|
+
base.extend(ClassMethods)
|
82
|
+
|
83
|
+
base.send(:include, InstanceMethods)
|
84
|
+
|
85
|
+
base.class_eval do
|
86
|
+
|
87
|
+
after_commit :set_this_tag_for_post
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
module ClassMethods
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
module InstanceMethods
|
100
|
+
|
101
|
+
def set_this_tag_for_post
|
102
|
+
|
103
|
+
regexp=Regexp.new(self.name)
|
104
|
+
|
105
|
+
Post.all.each do |post|
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
tag=post.comment.match(regexp)
|
110
|
+
|
111
|
+
if tag
|
112
|
+
|
113
|
+
post.tag_list.add(self.name)
|
114
|
+
|
115
|
+
post.comments.each do |comment|
|
116
|
+
|
117
|
+
c=comment.comment.match(regexp)
|
118
|
+
|
119
|
+
if c
|
120
|
+
|
121
|
+
post.tag_list.add(self.name)
|
122
|
+
|
123
|
+
brake
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
```
|
140
|
+
|
141
|
+
もちろんinitializer.rbに
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
|
145
|
+
ActsAsTaggableOn::Tag.send(:include,ActsAsTaggableOnPatch::TagPatch)
|
146
|
+
|
147
|
+
```
|
148
|
+
|
149
|
+
を入れてください
|