key-word-filter-on 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.travis.yml +9 -0
- data/CHANGELOG +35 -0
- data/Gemfile +3 -0
- data/Guardfile +5 -0
- data/LICENSE +22 -0
- data/MIT-LICENSE +20 -0
- data/README.md +29 -0
- data/README.rdoc +244 -0
- data/Rakefile +13 -0
- data/key-word-filter-on.gemspec +17 -0
- data/key_word_filter.gemspec +22 -0
- data/lib/filter_key_word_on/key_word.rb +162 -0
- data/lib/generators/key_word_filter_on/migration/migration_generator.rb +42 -0
- data/lib/generators/key_word_filter_on/migration/templates/active_record/migration.rb +20 -0
- data/lib/key-word-filter-on/key_word_filters.da +756 -0
- data/lib/key-word-filter-on/version.rb +3 -0
- data/lib/key-word-filter-on.rb +8 -0
- data/rails/init.rb +1 -0
- data/uninstall.rb +1 -0
- metadata +67 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
== 2011-08-21
|
2
|
+
* escape _ and % for mysql and postgres (@tilsammans)
|
3
|
+
* Now depends on mysql2 gem
|
4
|
+
* tagged_with :any is chainable now (@jeffreyiacono)
|
5
|
+
* tagged_with(nil) returns scoped object
|
6
|
+
* Case-insensitivity for TaggedModel.tagged_with for PostgreSQL database
|
7
|
+
* tagged_with(' ') returns scoped object
|
8
|
+
* remove warning for rails 3.1 about class_inheritable_attribute
|
9
|
+
* use ActiveRecord migration_number to avoid clashs (@atd)
|
10
|
+
|
11
|
+
== 2010-02-17
|
12
|
+
* Converted the plugin to be compatible with Rails3
|
13
|
+
|
14
|
+
== 2009-12-02
|
15
|
+
|
16
|
+
* PostgreSQL is now supported (via morgoth)
|
17
|
+
|
18
|
+
== 2008-07-17
|
19
|
+
|
20
|
+
* Can now use a named_scope to find tags!
|
21
|
+
|
22
|
+
== 2008-06-23
|
23
|
+
|
24
|
+
* Can now find related objects of another class (tristanzdunn)
|
25
|
+
* Removed extraneous down migration cruft (azabaj)
|
26
|
+
|
27
|
+
== 2008-06-09
|
28
|
+
|
29
|
+
* Added support for Single Table Inheritance
|
30
|
+
* Adding gemspec and rails/init.rb for gemified plugin
|
31
|
+
|
32
|
+
== 2007-12-12
|
33
|
+
|
34
|
+
* Added ability to use dynamic tag contexts
|
35
|
+
* Fixed missing migration generator
|
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 lijia.tong
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2007 Michael Bleigh and Intridea Inc.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Key::Word::Filter::On
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'key-word-filter-on'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install key-word-filter-on
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/README.rdoc
ADDED
@@ -0,0 +1,244 @@
|
|
1
|
+
= ActsAsTaggableOn
|
2
|
+
{<img src="https://secure.travis-ci.org/mbleigh/acts-as-taggable-on.png" />}[http://travis-ci.org/mbleigh/acts-as-taggable-on]
|
3
|
+
|
4
|
+
This plugin was originally based on Acts as Taggable on Steroids by Jonathan Viney.
|
5
|
+
It has evolved substantially since that point, but all credit goes to him for the
|
6
|
+
initial tagging functionality that so many people have used.
|
7
|
+
|
8
|
+
For instance, in a social network, a user might have tags that are called skills,
|
9
|
+
interests, sports, and more. There is no real way to differentiate between tags and
|
10
|
+
so an implementation of this type is not possible with acts as taggable on steroids.
|
11
|
+
|
12
|
+
Enter Acts as Taggable On. Rather than tying functionality to a specific keyword
|
13
|
+
(namely "tags"), acts as taggable on allows you to specify an arbitrary number of
|
14
|
+
tag "contexts" that can be used locally or in combination in the same way steroids
|
15
|
+
was used.
|
16
|
+
|
17
|
+
== Installation
|
18
|
+
|
19
|
+
=== Rails 2.x
|
20
|
+
|
21
|
+
Not supported any more! It is time for update guys.
|
22
|
+
|
23
|
+
=== Rails 3.x
|
24
|
+
|
25
|
+
To use it, add it to your Gemfile:
|
26
|
+
|
27
|
+
gem 'acts-as-taggable-on', '~> 2.3.1'
|
28
|
+
|
29
|
+
==== Post Installation
|
30
|
+
|
31
|
+
1. rails generate acts_as_taggable_on:migration
|
32
|
+
2. rake db:migrate
|
33
|
+
|
34
|
+
== Testing
|
35
|
+
|
36
|
+
Acts As Taggable On uses RSpec for its test coverage. Inside the gem
|
37
|
+
directory, you can run the specs for RoR 3.x with:
|
38
|
+
|
39
|
+
rake spec
|
40
|
+
|
41
|
+
== Usage
|
42
|
+
|
43
|
+
class User < ActiveRecord::Base
|
44
|
+
# Alias for <tt>acts_as_taggable_on :tags</tt>:
|
45
|
+
acts_as_taggable
|
46
|
+
acts_as_taggable_on :skills, :interests
|
47
|
+
end
|
48
|
+
|
49
|
+
@user = User.new(:name => "Bobby")
|
50
|
+
@user.tag_list = "awesome, slick, hefty" # this should be familiar
|
51
|
+
@user.skill_list = "joking, clowning, boxing" # but you can do it for any context!
|
52
|
+
@user.skill_list # => ["joking","clowning","boxing"] as TagList
|
53
|
+
@user.save
|
54
|
+
|
55
|
+
@user.tags # => [<Tag name:"awesome">,<Tag name:"slick">,<Tag name:"hefty">]
|
56
|
+
@user.skills # => [<Tag name:"joking">,<Tag name:"clowning">,<Tag name:"boxing">]
|
57
|
+
|
58
|
+
@frankie = User.create(:name => "Frankie", :skill_list => "joking, flying, eating")
|
59
|
+
User.skill_counts # => [<Tag name="joking" count=2>,<Tag name="clowning" count=1>...]
|
60
|
+
@frankie.skill_counts
|
61
|
+
|
62
|
+
To preserve the order in which tags are created use acts_as_ordered_taggable:
|
63
|
+
|
64
|
+
class User < ActiveRecord::Base
|
65
|
+
# Alias for <tt>acts_as_ordered_taggable_on :tags</tt>:
|
66
|
+
acts_as_ordered_taggable
|
67
|
+
acts_as_ordered_taggable_on :skills, :interests
|
68
|
+
end
|
69
|
+
|
70
|
+
@user = User.new(:name => "Bobby")
|
71
|
+
@user.tag_list = "east, south"
|
72
|
+
@user.save
|
73
|
+
|
74
|
+
@user.tag_list = "north, east, south, west"
|
75
|
+
@user.save
|
76
|
+
|
77
|
+
@user.reload
|
78
|
+
@user.tag_list # => ["north", "east", "south", "west"]
|
79
|
+
|
80
|
+
|
81
|
+
=== Finding Tagged Objects
|
82
|
+
|
83
|
+
Acts As Taggable On utilizes named_scopes to create an association for tags.
|
84
|
+
This way you can mix and match to filter down your results, and it also improves
|
85
|
+
compatibility with the will_paginate gem:
|
86
|
+
|
87
|
+
class User < ActiveRecord::Base
|
88
|
+
acts_as_taggable_on :tags, :skills
|
89
|
+
scope :by_join_date, order("created_at DESC")
|
90
|
+
end
|
91
|
+
|
92
|
+
User.tagged_with("awesome").by_date
|
93
|
+
User.tagged_with("awesome").by_date.paginate(:page => params[:page], :per_page => 20)
|
94
|
+
|
95
|
+
# Find a user with matching all tags, not just one
|
96
|
+
User.tagged_with(["awesome", "cool"], :match_all => true)
|
97
|
+
|
98
|
+
# Find a user with any of the tags:
|
99
|
+
User.tagged_with(["awesome", "cool"], :any => true)
|
100
|
+
|
101
|
+
# Find a user that not tags with awesome or cool:
|
102
|
+
User.tagged_with(["awesome", "cool"], :exclude => true)
|
103
|
+
|
104
|
+
# Find a user with any of tags based on context:
|
105
|
+
User.tagged_with(['awesome, cool'], :on => :tags, :any => true).tagged_with(['smart', 'shy'], :on => :skills, :any => true)
|
106
|
+
|
107
|
+
You can also use :wild => true option along with :any or :exclude option. It will looking for %awesome% and %cool% in sql.
|
108
|
+
|
109
|
+
Tip: User.tagged_with([]) or '' will return [], but not all records.
|
110
|
+
|
111
|
+
=== Relationships
|
112
|
+
|
113
|
+
You can find objects of the same type based on similar tags on certain contexts.
|
114
|
+
Also, objects will be returned in descending order based on the total number of
|
115
|
+
matched tags.
|
116
|
+
|
117
|
+
@bobby = User.find_by_name("Bobby")
|
118
|
+
@bobby.skill_list # => ["jogging", "diving"]
|
119
|
+
|
120
|
+
@frankie = User.find_by_name("Frankie")
|
121
|
+
@frankie.skill_list # => ["hacking"]
|
122
|
+
|
123
|
+
@tom = User.find_by_name("Tom")
|
124
|
+
@tom.skill_list # => ["hacking", "jogging", "diving"]
|
125
|
+
|
126
|
+
@tom.find_related_skills # => [<User name="Bobby">,<User name="Frankie">]
|
127
|
+
@bobby.find_related_skills # => [<User name="Tom">]
|
128
|
+
@frankie.find_related_skills # => [<User name="Tom">]
|
129
|
+
|
130
|
+
=== Dynamic Tag Contexts
|
131
|
+
|
132
|
+
In addition to the generated tag contexts in the definition, it is also possible
|
133
|
+
to allow for dynamic tag contexts (this could be user generated tag contexts!)
|
134
|
+
|
135
|
+
@user = User.new(:name => "Bobby")
|
136
|
+
@user.set_tag_list_on(:customs, "same, as, tag, list")
|
137
|
+
@user.tag_list_on(:customs) # => ["same","as","tag","list"]
|
138
|
+
@user.save
|
139
|
+
@user.tags_on(:customs) # => [<Tag name='same'>,...]
|
140
|
+
@user.tag_counts_on(:customs)
|
141
|
+
User.tagged_with("same", :on => :customs) # => [@user]
|
142
|
+
|
143
|
+
=== Tag Ownership
|
144
|
+
|
145
|
+
Tags can have owners:
|
146
|
+
|
147
|
+
class User < ActiveRecord::Base
|
148
|
+
acts_as_tagger
|
149
|
+
end
|
150
|
+
|
151
|
+
class Photo < ActiveRecord::Base
|
152
|
+
acts_as_taggable_on :locations
|
153
|
+
end
|
154
|
+
|
155
|
+
@some_user.tag(@some_photo, :with => "paris, normandy", :on => :locations)
|
156
|
+
@some_user.owned_taggings
|
157
|
+
@some_user.owned_tags
|
158
|
+
@some_photo.locations_from(@some_user) # => ["paris", "normandy"]
|
159
|
+
@some_photo.owner_tags_on(@some_user, :locations) # => [#<ActsAsTaggableOn::Tag id: 1, name: "paris">...]
|
160
|
+
@some_photo.owner_tags_on(nil, :locations) # => Ownerships equivalent to saying @some_photo.locations
|
161
|
+
@some_user.tag(@some_photo, :with => "paris, normandy", :on => :locations, :skip_save => true) #won't save @some_photo object
|
162
|
+
|
163
|
+
=== Dirty objects
|
164
|
+
|
165
|
+
@bobby = User.find_by_name("Bobby")
|
166
|
+
@bobby.skill_list # => ["jogging", "diving"]
|
167
|
+
|
168
|
+
@boddy.skill_list_changed? #=> false
|
169
|
+
@boddy.changes #=> {}
|
170
|
+
|
171
|
+
@bobby.skill_list = "swimming"
|
172
|
+
@bobby.changes.should == {"skill_list"=>["jogging, diving", ["swimming"]]}
|
173
|
+
@boddy.skill_list_changed? #=> true
|
174
|
+
|
175
|
+
@bobby.skill_list_change.should == ["jogging, diving", ["swimming"]]
|
176
|
+
|
177
|
+
=== Tag cloud calculations
|
178
|
+
|
179
|
+
To construct tag clouds, the frequency of each tag needs to be calculated.
|
180
|
+
Because we specified +acts_as_taggable_on+ on the <tt>User</tt> class, we can
|
181
|
+
get a calculation of all the tag counts by using <tt>User.tag_counts_on(:customs)</tt>. But what if we wanted a tag count for
|
182
|
+
an single user's posts? To achieve this we call tag_counts on the association:
|
183
|
+
|
184
|
+
User.find(:first).posts.tag_counts_on(:tags)
|
185
|
+
|
186
|
+
A helper is included to assist with generating tag clouds.
|
187
|
+
|
188
|
+
Here is an example that generates a tag cloud.
|
189
|
+
|
190
|
+
Helper:
|
191
|
+
|
192
|
+
module PostsHelper
|
193
|
+
include ActsAsTaggableOn::TagsHelper
|
194
|
+
end
|
195
|
+
|
196
|
+
Controller:
|
197
|
+
|
198
|
+
class PostController < ApplicationController
|
199
|
+
def tag_cloud
|
200
|
+
@tags = Post.tag_counts_on(:tags)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
View:
|
205
|
+
|
206
|
+
<% tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
|
207
|
+
<%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %>
|
208
|
+
<% end %>
|
209
|
+
|
210
|
+
CSS:
|
211
|
+
|
212
|
+
.css1 { font-size: 1.0em; }
|
213
|
+
.css2 { font-size: 1.2em; }
|
214
|
+
.css3 { font-size: 1.4em; }
|
215
|
+
.css4 { font-size: 1.6em; }
|
216
|
+
|
217
|
+
== Configuration
|
218
|
+
|
219
|
+
If you would like to remove unused tag objects after removing taggings, add
|
220
|
+
|
221
|
+
ActsAsTaggableOn.remove_unused_tags = true
|
222
|
+
|
223
|
+
If you want force tags to be saved downcased:
|
224
|
+
|
225
|
+
ActsAsTaggableOn.force_lowercase = true
|
226
|
+
|
227
|
+
If you want tags to be saved parametrized (you can redefine to_param as well):
|
228
|
+
|
229
|
+
ActsAsTaggableOn.force_parameterize = true
|
230
|
+
|
231
|
+
|
232
|
+
== Contributors
|
233
|
+
|
234
|
+
We have a long list of valued contributors. {Check them all}[https://github.com/mbleigh/acts-as-taggable-on/contributors]
|
235
|
+
|
236
|
+
== Maintainers
|
237
|
+
|
238
|
+
* Artem Kramarenko (artemk)
|
239
|
+
|
240
|
+
== Author
|
241
|
+
|
242
|
+
* Michael Bleigh
|
243
|
+
|
244
|
+
Copyright (c) 2007-2011 Michael Bleigh (http://mbleigh.com/) and Intridea Inc. (http://intridea.com/), released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
Bundler.setup :default, :development
|
4
|
+
|
5
|
+
desc 'Default: run specs'
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
require 'rspec/core/rake_task'
|
9
|
+
RSpec::Core::RakeTask.new do |t|
|
10
|
+
t.pattern = "spec/**/*_spec.rb"
|
11
|
+
end
|
12
|
+
|
13
|
+
Bundler::GemHelper.install_tasks
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/key-word-filter-on/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["lijia.tong"]
|
6
|
+
gem.email = ["wtuyuupe@163.com"]
|
7
|
+
gem.description = %q{Filter sensitive keyword method.}
|
8
|
+
gem.summary = %q{Filtering illegal characters and sensitive words.}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "key-word-filter-on"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = KeyWordFilterOn::VERSION
|
17
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
$:.push File.dirname(__FILE__) + '/lib'
|
2
|
+
require 'key-word-filter-on/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.name = %q{key-word-filter-on}
|
6
|
+
gem.authors = ["Lijia.Tong"]
|
7
|
+
gem.date = %q{2012-08-05}
|
8
|
+
gem.description = %q{Filter sensitive keyword method.}
|
9
|
+
gem.summary = "Filtering illegal characters and sensitive words."
|
10
|
+
gem.email = %q{wtuyuupe@163.com}
|
11
|
+
gem.homepage = 'http://hi.baidu.com/new/user_tony'
|
12
|
+
|
13
|
+
gem.add_runtime_dependency 'rails', '~> 3.0'
|
14
|
+
gem.add_development_dependency 'rspec', '~> 2.6'
|
15
|
+
|
16
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
gem.files = `git ls-files`.split("\n")
|
18
|
+
# gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
gem.name = "key-word-filter-on"
|
20
|
+
gem.require_paths = ['lib']
|
21
|
+
gem.version = KeyWordFilterOn::VERSION
|
22
|
+
end
|
@@ -0,0 +1,162 @@
|
|
1
|
+
module FilterKeyWordOn
|
2
|
+
|
3
|
+
|
4
|
+
def self.included(base) #当这个module在被include的时候触发
|
5
|
+
base.send :include, InstanceMethods #引入实例方法
|
6
|
+
base.send :extend, ClassMethods # 引入类方法
|
7
|
+
end
|
8
|
+
|
9
|
+
module InstanceMethods
|
10
|
+
#TODO
|
11
|
+
end#InstanceMethods
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
#TODO
|
15
|
+
end#ClassMethods
|
16
|
+
class KeyWordFilter < ActiveRecord::Base
|
17
|
+
attr_accessible :content, :replace
|
18
|
+
end
|
19
|
+
|
20
|
+
content, replace = ''
|
21
|
+
# 敏感字过滤替换关键字方法
|
22
|
+
# 作者: 佟立家
|
23
|
+
# 最后更新时间: 2011-12-31
|
24
|
+
#
|
25
|
+
# ==== 参数
|
26
|
+
# * <tt>content</tt> - 过滤内容
|
27
|
+
#
|
28
|
+
# ==== 示例
|
29
|
+
#
|
30
|
+
# KeyWordFilter.replace_content(content)
|
31
|
+
#
|
32
|
+
# ==== 返回
|
33
|
+
# true/false
|
34
|
+
#
|
35
|
+
def self.replace_content(content)
|
36
|
+
return handle_word(content)
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
# 查询关键字生成数据, 多维数组伸平
|
41
|
+
#
|
42
|
+
#
|
43
|
+
# 作者: 佟立家
|
44
|
+
# 最后更新时间: 2011-12-31
|
45
|
+
#
|
46
|
+
#
|
47
|
+
# ==== 返回
|
48
|
+
# ['1','1']
|
49
|
+
#
|
50
|
+
def self.replacement_hash
|
51
|
+
Hash[*FilterKeyWordOn::KeyWordFilter.all.collect{|re| [re.content,re.replace] }.flatten]
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
#
|
56
|
+
##
|
57
|
+
# 作者: 佟立家
|
58
|
+
# 最后更新时间: 2011-12-31
|
59
|
+
#
|
60
|
+
# ==== 参数
|
61
|
+
# * <tt>object</tt> -
|
62
|
+
#
|
63
|
+
# ==== 示例
|
64
|
+
#
|
65
|
+
#
|
66
|
+
# ==== 返回
|
67
|
+
#
|
68
|
+
def self.filter_word_tree(object = nil)
|
69
|
+
word_tree = Array.new(256) << 0
|
70
|
+
object = replacement_hash if object.nil?
|
71
|
+
object.each do |word,replace|
|
72
|
+
temp = word_tree
|
73
|
+
bytes = word.bytes.to_a
|
74
|
+
len = bytes.size
|
75
|
+
|
76
|
+
bytes.each_with_index do |asicc_code,arr_index|
|
77
|
+
if arr_index < len - 1
|
78
|
+
if temp[asicc_code].nil?
|
79
|
+
node = [Array.new(256),0]
|
80
|
+
temp[asicc_code] = node
|
81
|
+
elsif temp[asicc_code] == 1
|
82
|
+
node = [Array.new(256),1]
|
83
|
+
temp[asicc_code] = node
|
84
|
+
else
|
85
|
+
end
|
86
|
+
temp = temp[asicc_code][0]
|
87
|
+
else
|
88
|
+
temp[asicc_code] = 1
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
[word_tree,0]
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
# 关健字过滤
|
98
|
+
# 关健字过滤方法
|
99
|
+
# 替换明感 字符
|
100
|
+
#
|
101
|
+
# 作者: 佟立家
|
102
|
+
# 最后更新时间: 2011-12-31
|
103
|
+
#
|
104
|
+
# ==== 参数
|
105
|
+
# * <tt>do_words</tt> -
|
106
|
+
# * <tt>replace</tt> -
|
107
|
+
# * <tt>word_tree</tt> -
|
108
|
+
# * <tt>word_hash</tt> -
|
109
|
+
#
|
110
|
+
# ==== 示例
|
111
|
+
#
|
112
|
+
# KeyWordFilter.replace_content(content)
|
113
|
+
#
|
114
|
+
# ==== 返回
|
115
|
+
# true/false
|
116
|
+
#
|
117
|
+
def self.handle_word(do_words,replace = true,word_tree = nil,word_hash = nil)
|
118
|
+
word_tree = filter_word_tree if word_tree.nil?
|
119
|
+
word_hash = replacement_hash if word_hash.nil?
|
120
|
+
temp = word_tree
|
121
|
+
nodeTree = word_tree
|
122
|
+
words = []
|
123
|
+
word = []
|
124
|
+
to_replace= []
|
125
|
+
a = 0
|
126
|
+
byte_words = do_words.bytes.to_a
|
127
|
+
while a < byte_words.size
|
128
|
+
index = byte_words[a]
|
129
|
+
temp = temp[0][index]
|
130
|
+
if temp.nil?
|
131
|
+
temp = nodeTree
|
132
|
+
a = a - word.size
|
133
|
+
word = []
|
134
|
+
to_replace = []
|
135
|
+
elsif temp == 1 or temp[1] == 1
|
136
|
+
word << index
|
137
|
+
to_replace << a
|
138
|
+
words << word
|
139
|
+
|
140
|
+
if replace
|
141
|
+
replace_word = word_hash[word.pack("C*").force_encoding("UTF-8")].bytes.to_a
|
142
|
+
byte_words[(a-to_replace.size + 1),to_replace.size] = replace_word
|
143
|
+
a = (a - to_replace.size + 1) + (replace_word.size - 1)
|
144
|
+
else
|
145
|
+
a = a - word.size + 1
|
146
|
+
end
|
147
|
+
word = []
|
148
|
+
to_replace = []
|
149
|
+
temp = nodeTree
|
150
|
+
else
|
151
|
+
word << index
|
152
|
+
to_replace << a
|
153
|
+
end
|
154
|
+
a += 1
|
155
|
+
end
|
156
|
+
return byte_words.pack("C*").force_encoding("UTF-8") if replace
|
157
|
+
words.collect{|e| e.collect{|ch|ch.chr}.join }
|
158
|
+
end
|
159
|
+
|
160
|
+
end #FilterKeyWordOn
|
161
|
+
|
162
|
+
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
module KeyWordFilterOn
|
5
|
+
class MigrationGenerator < Rails::Generators::Base
|
6
|
+
include Rails::Generators::Migration
|
7
|
+
|
8
|
+
desc "Generates migration for Tag and Tagging models"
|
9
|
+
|
10
|
+
def self.orm
|
11
|
+
Rails::Generators.options[:rails][:orm]
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.source_root
|
15
|
+
File.join(File.dirname(__FILE__), 'templates', (orm.to_s unless orm.class.eql?(String)) )
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.orm_has_migration?
|
19
|
+
[:active_record].include? orm
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
def self.next_migration_number(dirname)
|
25
|
+
if ActiveRecord::Base.timestamped_migrations
|
26
|
+
migration_number = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
|
27
|
+
migration_number += 1
|
28
|
+
migration_number.to_s
|
29
|
+
else
|
30
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def create_migration_file
|
35
|
+
if self.class.orm_has_migration?
|
36
|
+
migration_template 'migration.rb', 'db/migrate/create_key_word_filters'
|
37
|
+
end
|
38
|
+
`cp #{File.expand_path('../../../../', __FILE__)}/key-word-filter-on/key_word_filters.da config`
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class CreateKeyWordFilters < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :key_word_filters do |t|
|
4
|
+
t.column :content, :string # 过滤内容
|
5
|
+
t.column :replace, :string # 替换字符
|
6
|
+
t.timestamps
|
7
|
+
end
|
8
|
+
|
9
|
+
# 初始化关键字数据信息
|
10
|
+
path = "#{Rails.root}/config/key_word_filters.da"
|
11
|
+
File.open(path) do |file|
|
12
|
+
#say "init key_word_datas"
|
13
|
+
file.each_line{|line| FilterKeyWordOn::KeyWordFilter.create(:content => line.strip, :replace => "**") } #添加数据
|
14
|
+
end if File.exist?(path)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.down
|
18
|
+
drop_table :key_word_filters
|
19
|
+
end
|
20
|
+
end
|