@@ -13,6 +13,7 @@ A Ruby library for text classification using Bayesian and Latent Semantic Indexi
1313- [ Installation] ( #installation )
1414- [ Bayesian Classifier] ( #bayesian-classifier )
1515- [ LSI (Latent Semantic Indexing)] ( #lsi-latent-semantic-indexing )
16+ - [ Persistence] ( #persistence )
1617- [ Performance] ( #performance )
1718- [ Development] ( #development )
1819- [ Contributing] ( #contributing )
@@ -93,25 +94,6 @@ classifier.classify "Congratulations! You've won a prize!"
9394# => "Spam"
9495```
9596
96- ### Persistence with Madeleine
97-
98- ``` ruby
99- require ' classifier'
100- require ' madeleine'
101-
102- m = SnapshotMadeleine .new (" classifier_data" ) {
103- Classifier ::Bayes .new (' Interesting' , ' Uninteresting' )
104- }
105-
106- m.system .train_interesting " fascinating article about science"
107- m.system .train_uninteresting " boring repetitive content"
108- m.take_snapshot
109-
110- # Later, restore and use:
111- m.system .classify " new scientific discovery"
112- # => "Interesting"
113- ```
114-
11597### Learn More
11698
11799- [ Bayes Basics Guide] ( https://rubyclassifier.com/docs/guides/bayes/basics ) - In-depth documentation
@@ -161,6 +143,55 @@ lsi.search "programming", 3
161143- [ LSI Basics Guide] ( https://rubyclassifier.com/docs/guides/lsi/basics ) - In-depth documentation
162144- [ Wikipedia: Latent Semantic Analysis] ( http://en.wikipedia.org/wiki/Latent_semantic_analysis )
163145
146+ ## Persistence
147+
148+ Save and load trained classifiers with pluggable storage backends. Works with both Bayes and LSI classifiers.
149+
150+ ### File Storage
151+
152+ ``` ruby
153+ require ' classifier'
154+
155+ classifier = Classifier ::Bayes .new (' Spam' , ' Ham' )
156+ classifier.train_spam " Buy now! Limited offer!"
157+ classifier.train_ham " Meeting tomorrow at 3pm"
158+
159+ # Configure storage and save
160+ classifier.storage = Classifier ::Storage ::File .new (path: " spam_filter.json" )
161+ classifier.save
162+
163+ # Load later
164+ loaded = Classifier ::Bayes .load (storage: classifier.storage)
165+ loaded.classify " Claim your prize now!"
166+ # => "Spam"
167+ ```
168+
169+ ### Custom Storage Backends
170+
171+ Create backends for Redis, PostgreSQL, S3, or any storage system:
172+
173+ ``` ruby
174+ class RedisStorage < Classifier ::Storage ::Base
175+ def initialize (redis: , key: )
176+ super ()
177+ @redis , @key = redis, key
178+ end
179+
180+ def write (data ) = @redis .set(@key , data )
181+ def read = @redis .get(@key )
182+ def delete = @redis .del(@key )
183+ def exists? = @redis .exists?(@key )
184+ end
185+
186+ # Use it
187+ classifier.storage = RedisStorage .new (redis: Redis .new , key: " classifier:spam" )
188+ classifier.save
189+ ```
190+
191+ ### Learn More
192+
193+ - [ Persistence Guide] ( https://rubyclassifier.com/docs/guides/persistence/basics ) - Full documentation with examples
194+
164195## Performance
165196
166197### Native C Extension vs Pure Ruby
0 commit comments