root / classifier / doc / storage.txt

Revision 162:ca1529c62df9, 2.4 kB (checked in by Tarek Ziad?? <tarek@…>, 11 months ago)

fixes

Line 
1=======
2storage
3=======
4
5Storage is a backend used to store data for the bayesian network.
6
7Let'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
17It works with languages, words, categories, and words within categories::
18
19    >>> from storage import SQLStorage
20
21Each storage is affiliated with a user, so each user can have it's own
22bayesian data::
23
24    >>> storage = SQLStorage('tester')
25
26Next 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
38We 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
51And words, that are specific to a language. If `list_words` does
52not have the language as a parameter, it will return all words of
53all languages::
54
55    >>> res = storage.list_words()
56    >>> res
57    <generator object at 0x...>
58
59Since 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
70We can also add words within categories::
71
72    >>> storage.add_word('coucou', 'fr', ('cat1', 'cat2'))
73
74This will add categories automatically::
75
76    >>> storage.list_categories()
77    [u'two', u'cat1', u'cat2']
78
79And get for each word its category::
80
81    >>> storage.word_categories('coucou', 'fr')
82    [u'cat1', u'cat2']
83
84This classification is updatable::
85
86    >>> storage.add_word('coucou', 'fr', ('cat1',))
87    >>> storage.word_categories('coucou', 'fr')
88    [u'cat1']
89
90We can count words:
91
92    >>> storage.word_count()
93    2
94
95We can also remove words::
96
97    >>> storage.del_word('coucou')
98    >>> list(storage.list_words())
99    [(u'hello', u'en')]
100
101
102Let's remove the test db::
103
104    >>> import os
105    >>> os.remove(db_file)
106
Note: See TracBrowser for help on using the browser.