| 1 | ======= |
|---|
| 2 | storage |
|---|
| 3 | ======= |
|---|
| 4 | |
|---|
| 5 | Storage is a backend used to store data for the bayesian network. |
|---|
| 6 | |
|---|
| 7 | Let's work in a special database:: |
|---|
| 8 | |
|---|
| 9 | >>> db_file = 'test.db' |
|---|
| 10 | >>> import settings |
|---|
| 11 | >>> settings.SQLURI = 'sqlite:///%s' % db_file |
|---|
| 12 | >>> import os |
|---|
| 13 | >>> if os.path.exists(db_file): |
|---|
| 14 | ... os.remove(db_file) |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | It works with languages, words, categories, and words within categories:: |
|---|
| 18 | |
|---|
| 19 | >>> from storage import SQLStorage |
|---|
| 20 | |
|---|
| 21 | Each storage is affiliated with a user, so each user can have it's own |
|---|
| 22 | bayesian data:: |
|---|
| 23 | |
|---|
| 24 | >>> storage = SQLStorage('tester') |
|---|
| 25 | |
|---|
| 26 | Next we can store languages, since each word is in a given language:: |
|---|
| 27 | |
|---|
| 28 | >>> storage.list_languages() |
|---|
| 29 | [] |
|---|
| 30 | >>> storage.add_language('fr') |
|---|
| 31 | >>> storage.add_language('en') |
|---|
| 32 | >>> storage.list_languages() |
|---|
| 33 | [u'fr', u'en'] |
|---|
| 34 | >>> storage.del_language('fr') |
|---|
| 35 | >>> storage.list_languages() |
|---|
| 36 | [u'en'] |
|---|
| 37 | |
|---|
| 38 | We can work with categories:: |
|---|
| 39 | |
|---|
| 40 | >>> storage.list_categories() |
|---|
| 41 | [] |
|---|
| 42 | >>> storage.add_category('one') |
|---|
| 43 | >>> storage.add_category('two') |
|---|
| 44 | >>> storage.list_categories() |
|---|
| 45 | [u'one', u'two'] |
|---|
| 46 | >>> storage.del_category('one') |
|---|
| 47 | >>> storage.list_categories() |
|---|
| 48 | [u'two'] |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | And words, that are specific to a language. If `list_words` does |
|---|
| 52 | not have the language as a parameter, it will return all words of |
|---|
| 53 | all languages:: |
|---|
| 54 | |
|---|
| 55 | >>> res = storage.list_words() |
|---|
| 56 | >>> res |
|---|
| 57 | <generator object at 0x...> |
|---|
| 58 | |
|---|
| 59 | Since we get a generator (for optimisation purpose) we need to act upon:: |
|---|
| 60 | |
|---|
| 61 | >>> list(res) |
|---|
| 62 | [] |
|---|
| 63 | >>> storage.add_word('coucou', 'fr') |
|---|
| 64 | >>> storage.add_word('hello', 'en') |
|---|
| 65 | >>> list(storage.list_words()) |
|---|
| 66 | [(u'coucou', u'fr'), (u'hello', u'en')] |
|---|
| 67 | >>> list(storage.list_words('fr')) |
|---|
| 68 | [(u'coucou', u'fr')] |
|---|
| 69 | |
|---|
| 70 | We can also add words within categories:: |
|---|
| 71 | |
|---|
| 72 | >>> storage.add_word('coucou', 'fr', ('cat1', 'cat2')) |
|---|
| 73 | |
|---|
| 74 | This will add categories automatically:: |
|---|
| 75 | |
|---|
| 76 | >>> storage.list_categories() |
|---|
| 77 | [u'two', u'cat1', u'cat2'] |
|---|
| 78 | |
|---|
| 79 | And get for each word its category:: |
|---|
| 80 | |
|---|
| 81 | >>> storage.word_categories('coucou', 'fr') |
|---|
| 82 | [u'cat1', u'cat2'] |
|---|
| 83 | |
|---|
| 84 | This classification is updatable:: |
|---|
| 85 | |
|---|
| 86 | >>> storage.add_word('coucou', 'fr', ('cat1',)) |
|---|
| 87 | >>> storage.word_categories('coucou', 'fr') |
|---|
| 88 | [u'cat1'] |
|---|
| 89 | |
|---|
| 90 | We can count words: |
|---|
| 91 | |
|---|
| 92 | >>> storage.word_count() |
|---|
| 93 | 2 |
|---|
| 94 | |
|---|
| 95 | We can also remove words:: |
|---|
| 96 | |
|---|
| 97 | >>> storage.del_word('coucou') |
|---|
| 98 | >>> list(storage.list_words()) |
|---|
| 99 | [(u'hello', u'en')] |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | Let's remove the test db:: |
|---|
| 103 | |
|---|
| 104 | >>> import os |
|---|
| 105 | >>> os.remove(db_file) |
|---|
| 106 | |
|---|