======= storage ======= Storage is a backend used to store data for the bayesian network. Let's work in a special database:: >>> db_file = 'test.db' >>> import settings >>> settings.SQLURI = 'sqlite:///%s' % db_file >>> import os >>> if os.path.exists(db_file): ... os.remove(db_file) It works with languages, words, categories, and words within categories:: >>> from storage import SQLStorage Each storage is affiliated with a user, so each user can have it's own bayesian data:: >>> storage = SQLStorage('tester') Next we can store languages, since each word is in a given language:: >>> storage.list_languages() [] >>> storage.add_language('fr') >>> storage.add_language('en') >>> storage.list_languages() [u'fr', u'en'] >>> storage.del_language('fr') >>> storage.list_languages() [u'en'] We can work with categories:: >>> storage.list_categories() [] >>> storage.add_category('one') >>> storage.add_category('two') >>> storage.list_categories() [u'one', u'two'] >>> storage.del_category('one') >>> storage.list_categories() [u'two'] And words, that are specific to a language. If `list_words` does not have the language as a parameter, it will return all words of all languages:: >>> res = storage.list_words() >>> res Since we get a generator (for optimisation purpose) we need to act upon:: >>> list(res) [] >>> storage.add_word('coucou', 'fr') >>> storage.add_word('hello', 'en') >>> list(storage.list_words()) [(u'coucou', u'fr'), (u'hello', u'en')] >>> list(storage.list_words('fr')) [(u'coucou', u'fr')] We can also add words within categories:: >>> storage.add_word('coucou', 'fr', ('cat1', 'cat2')) This will add categories automatically:: >>> storage.list_categories() [u'two', u'cat1', u'cat2'] And get for each word its category:: >>> storage.word_categories('coucou', 'fr') [u'cat1', u'cat2'] This classification is updatable:: >>> storage.add_word('coucou', 'fr', ('cat1',)) >>> storage.word_categories('coucou', 'fr') [u'cat1'] We can count words: >>> storage.word_count() 2 We can also remove words:: >>> storage.del_word('coucou') >>> list(storage.list_words()) [(u'hello', u'en')] Let's remove the test db:: >>> import os >>> os.remove(db_file)