Merge lp:~zeitgeist/zeitgeist/mostusedwith into lp:zeitgeist/0.1
- mostusedwith
- Merge into 0.8-python
Proposed by
Seif Lotfy
Status: | Merged |
---|---|
Merged at revision: | not available |
Proposed branch: | lp:~zeitgeist/zeitgeist/mostusedwith |
Merge into: | lp:zeitgeist/0.1 |
Diff against target: |
292 lines (+67/-41) 7 files modified
_zeitgeist/engine/main.py (+23/-18) _zeitgeist/engine/remote.py (+9/-3) test/data/apriori_events.js (+1/-1) test/engine-test.py (+18/-10) test/remote-test.py (+2/-2) zeitgeist/client.py (+12/-5) zeitgeist/datamodel.py (+2/-2) |
To merge this branch: | bzr merge lp:~zeitgeist/zeitgeist/mostusedwith |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Zeitgeist Framework Team | Pending | ||
Review via email: mp+20154@code.launchpad.net |
Commit message
Description of the change
To post a comment you must log in.
Revision history for this message
Seif Lotfy (seif) wrote : | # |
- 1347. By Seif Lotfy
-
fixed test case
- 1348. By Seif Lotfy
-
removed second query
- 1349. By Seif Lotfy
-
merge and fix issues
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file '_zeitgeist/engine/main.py' | |||
2 | --- _zeitgeist/engine/main.py 2010-02-16 19:36:42 +0000 | |||
3 | +++ _zeitgeist/engine/main.py 2010-02-25 18:14:12 +0000 | |||
4 | @@ -28,7 +28,7 @@ | |||
5 | 28 | import logging | 28 | import logging |
6 | 29 | 29 | ||
7 | 30 | from zeitgeist.datamodel import Event as OrigEvent, StorageState, TimeRange, \ | 30 | from zeitgeist.datamodel import Event as OrigEvent, StorageState, TimeRange, \ |
9 | 31 | ResultType, get_timestamp_for_now | 31 | ResultType, get_timestamp_for_now, Interpretation |
10 | 32 | from _zeitgeist.engine.datamodel import Event, Subject | 32 | from _zeitgeist.engine.datamodel import Event, Subject |
11 | 33 | from _zeitgeist.engine.extension import ExtensionsCollection, load_class | 33 | from _zeitgeist.engine.extension import ExtensionsCollection, load_class |
12 | 34 | from _zeitgeist.engine import constants | 34 | from _zeitgeist.engine import constants |
13 | @@ -258,7 +258,7 @@ | |||
14 | 258 | return self._find_events(1, *args) | 258 | return self._find_events(1, *args) |
15 | 259 | 259 | ||
16 | 260 | def find_related_uris(self, timerange, event_templates, result_event_templates, | 260 | def find_related_uris(self, timerange, event_templates, result_event_templates, |
18 | 261 | result_storage_state): | 261 | result_storage_state, num_results, result_type): |
19 | 262 | """ | 262 | """ |
20 | 263 | Return a list of subject URIs commonly used together with events | 263 | Return a list of subject URIs commonly used together with events |
21 | 264 | matching the given template, considering data from within the indicated | 264 | matching the given template, considering data from within the indicated |
22 | @@ -273,13 +273,14 @@ | |||
23 | 273 | 273 | ||
24 | 274 | #templates = event_templates + result_event_templates | 274 | #templates = event_templates + result_event_templates |
25 | 275 | 275 | ||
27 | 276 | events = self.find_events( timerange, result_event_templates, | 276 | events = self.find_events(timerange, result_event_templates, |
28 | 277 | result_storage_state, 0, 1) | 277 | result_storage_state, 0, 1) |
29 | 278 | 278 | ||
30 | 279 | subject_uris = [] | 279 | subject_uris = [] |
31 | 280 | for event in event_templates: | 280 | for event in event_templates: |
34 | 281 | if not event.subjects[0].uri in subject_uris: | 281 | if len(event.subjects) > 0: |
35 | 282 | subject_uris.append(event.subjects[0].uri) | 282 | if not event.subjects[0].uri in subject_uris: |
36 | 283 | subject_uris.append(event.subjects[0].uri) | ||
37 | 283 | 284 | ||
38 | 284 | def create_buckets(events): | 285 | def create_buckets(events): |
39 | 285 | """ | 286 | """ |
40 | @@ -293,37 +294,41 @@ | |||
41 | 293 | buckets.append({}) | 294 | buckets.append({}) |
42 | 294 | buckets[len(buckets)-1][event.subjects[0].uri] = event | 295 | buckets[len(buckets)-1][event.subjects[0].uri] = event |
43 | 295 | return buckets | 296 | return buckets |
44 | 297 | |||
45 | 296 | buckets = create_buckets(events) | 298 | buckets = create_buckets(events) |
46 | 297 | 299 | ||
47 | 298 | keys_counter = {} | 300 | keys_counter = {} |
48 | 299 | keys_subjects = {} | ||
49 | 300 | min_support = 0 | ||
50 | 301 | 301 | ||
51 | 302 | for bucket in buckets: | 302 | for bucket in buckets: |
52 | 303 | counter = 0 | 303 | counter = 0 |
53 | 304 | for event in event_templates: | 304 | for event in event_templates: |
54 | 305 | if bucket.has_key(event.subjects[0].uri): | 305 | if bucket.has_key(event.subjects[0].uri): |
55 | 306 | counter += 1 | 306 | counter += 1 |
56 | 307 | break | ||
57 | 307 | if counter > 0: | 308 | if counter > 0: |
58 | 308 | for key in bucket.keys(): | 309 | for key in bucket.keys(): |
59 | 309 | if not key in subject_uris: | 310 | if not key in subject_uris: |
60 | 310 | if not keys_counter.has_key(key): | 311 | if not keys_counter.has_key(key): |
61 | 311 | keys_counter[key] = 0 | 312 | keys_counter[key] = 0 |
62 | 312 | keys_counter[key] += 1 | 313 | keys_counter[key] += 1 |
63 | 313 | min_support += 1 | ||
64 | 314 | keys_subjects[key] = bucket[key] | ||
65 | 315 | |||
66 | 316 | |||
67 | 317 | min_support = min_support / len(keys_counter.keys()) | ||
68 | 318 | 314 | ||
69 | 319 | sets = [[v, k] for k, v in keys_counter.iteritems()] | 315 | sets = [[v, k] for k, v in keys_counter.iteritems()] |
70 | 320 | sets.sort() | 316 | sets.sort() |
77 | 321 | 317 | sets.reverse() | |
78 | 322 | results = [] | 318 | |
79 | 323 | for r in sets: | 319 | if result_type == 0: |
80 | 324 | if r[0] >= min_support: | 320 | return map(lambda result: result[1], sets[:num_results]) |
81 | 325 | results.append(r[1]) | 321 | elif result_type == 1: |
82 | 326 | return results | 322 | events = [] |
83 | 323 | for r in sets: | ||
84 | 324 | event = Event() | ||
85 | 325 | subject = Subject() | ||
86 | 326 | subject.uri = r[1] | ||
87 | 327 | event.set_subjects([subject]) | ||
88 | 328 | events.append(event) | ||
89 | 329 | events = self.find_events(timerange, events, result_storage_state, num_results, 2) | ||
90 | 330 | return map(lambda event: event.subjects[0].uri, events) | ||
91 | 331 | raise NotImplementedError, "Unsupported ResultType." | ||
92 | 327 | 332 | ||
93 | 328 | def insert_events(self, events, sender=None): | 333 | def insert_events(self, events, sender=None): |
94 | 329 | t = time.time() | 334 | t = time.time() |
95 | 330 | 335 | ||
96 | === modified file '_zeitgeist/engine/remote.py' | |||
97 | --- _zeitgeist/engine/remote.py 2010-01-27 15:08:51 +0000 | |||
98 | +++ _zeitgeist/engine/remote.py 2010-02-25 18:14:12 +0000 | |||
99 | @@ -4,6 +4,7 @@ | |||
100 | 4 | # | 4 | # |
101 | 5 | # Copyright © 2009-2010 Siegfried-Angel Gevatter Pujals <rainct@ubuntu.com> | 5 | # Copyright © 2009-2010 Siegfried-Angel Gevatter Pujals <rainct@ubuntu.com> |
102 | 6 | # Copyright © 2009 Mikkel Kamstrup Erlandsen <mikkel.kamstrup@gmail.com> | 6 | # Copyright © 2009 Mikkel Kamstrup Erlandsen <mikkel.kamstrup@gmail.com> |
103 | 7 | # Copyright © 2010 Seif Lotfy <seif@lotfy.com> | ||
104 | 7 | # | 8 | # |
105 | 8 | # This program is free software: you can redistribute it and/or modify | 9 | # This program is free software: you can redistribute it and/or modify |
106 | 9 | # it under the terms of the GNU Lesser General Public License as published by | 10 | # it under the terms of the GNU Lesser General Public License as published by |
107 | @@ -86,10 +87,10 @@ | |||
108 | 86 | sender=sender)) | 87 | sender=sender)) |
109 | 87 | 88 | ||
110 | 88 | @dbus.service.method(constants.DBUS_INTERFACE, | 89 | @dbus.service.method(constants.DBUS_INTERFACE, |
112 | 89 | in_signature="(xx)a("+constants.SIG_EVENT+")a("+constants.SIG_EVENT+")u", | 90 | in_signature="(xx)a("+constants.SIG_EVENT+")a("+constants.SIG_EVENT+")uuu", |
113 | 90 | out_signature="as") | 91 | out_signature="as") |
114 | 91 | def FindRelatedUris(self, time_range, event_templates, | 92 | def FindRelatedUris(self, time_range, event_templates, |
116 | 92 | result_event_templates, storage_state): | 93 | result_event_templates, storage_state, num_events, result_type): |
117 | 93 | """Warning: This API is EXPERIMENTAL and is not fully supported yet. | 94 | """Warning: This API is EXPERIMENTAL and is not fully supported yet. |
118 | 94 | 95 | ||
119 | 95 | Get a list of URIs of subjects which frequently occur together | 96 | Get a list of URIs of subjects which frequently occur together |
120 | @@ -122,11 +123,16 @@ | |||
121 | 122 | available. The list of possible values is enumerated in the | 123 | available. The list of possible values is enumerated in the |
122 | 123 | :class:`StorageState <zeitgeist.datamodel.StorageState>` class | 124 | :class:`StorageState <zeitgeist.datamodel.StorageState>` class |
123 | 124 | :type storage_state: unsigned 32 bit integer, DBus signature :const:`u` | 125 | :type storage_state: unsigned 32 bit integer, DBus signature :const:`u` |
124 | 126 | :param num_events: maximal amount of returned events | ||
125 | 127 | :type num_events: unsigned integer | ||
126 | 128 | :param result_type: unsigned integer 0 for relevancy 1 for recency | ||
127 | 129 | :type order: unsigned integer | ||
128 | 125 | :returns: A list of URIs matching the described criteria | 130 | :returns: A list of URIs matching the described criteria |
129 | 126 | :rtype: An array of strings, DBus signature :const:`as`. | 131 | :rtype: An array of strings, DBus signature :const:`as`. |
130 | 127 | """ | 132 | """ |
131 | 133 | event_templates = map(Event, event_templates) | ||
132 | 128 | return _engine.find_related_uris(time_range, event_templates, | 134 | return _engine.find_related_uris(time_range, event_templates, |
134 | 129 | result_event_templates, storage_state) | 135 | result_event_templates, storage_state, num_events, result_type) |
135 | 130 | 136 | ||
136 | 131 | @dbus.service.method(constants.DBUS_INTERFACE, | 137 | @dbus.service.method(constants.DBUS_INTERFACE, |
137 | 132 | in_signature="(xx)a("+constants.SIG_EVENT+")uuu", | 138 | in_signature="(xx)a("+constants.SIG_EVENT+")uuu", |
138 | 133 | 139 | ||
139 | === modified file 'test/data/apriori_events.js' | |||
140 | --- test/data/apriori_events.js 2010-02-07 03:44:19 +0000 | |||
141 | +++ test/data/apriori_events.js 2010-02-25 18:14:12 +0000 | |||
142 | @@ -390,4 +390,4 @@ | |||
143 | 390 | } | 390 | } |
144 | 391 | ] | 391 | ] |
145 | 392 | } | 392 | } |
147 | 393 | ] | 393 | ] |
148 | 394 | \ No newline at end of file | 394 | \ No newline at end of file |
149 | 395 | 395 | ||
150 | === modified file 'test/engine-test.py' | |||
151 | --- test/engine-test.py 2010-02-10 02:17:34 +0000 | |||
152 | +++ test/engine-test.py 2010-02-25 18:14:12 +0000 | |||
153 | @@ -490,28 +490,36 @@ | |||
154 | 490 | TimeRange.always(), [], StorageState.Any, 0, ResultType.LeastRecentActor) | 490 | TimeRange.always(), [], StorageState.Any, 0, ResultType.LeastRecentActor) |
155 | 491 | self.assertEquals([e[0][1] for e in events], ["100", "101", "105"]) | 491 | self.assertEquals([e[0][1] for e in events], ["100", "101", "105"]) |
156 | 492 | 492 | ||
162 | 493 | def testRelatedForEvents(self): | 493 | def testRelatedForEventsSortRelevancy(self): |
163 | 494 | import_events("test/data/apriori_events.js", self.engine) | 494 | import_events("test/data/apriori_events.js", self.engine) |
164 | 495 | result = self.engine.find_related_uris( | 495 | result = self.engine.find_related_uris( |
165 | 496 | TimeRange.always(), [Event.new_for_values(subject_uri = "i2")], [], | 496 | TimeRange.always(), [Event.new_for_values(subject_uri = "i2")], [], |
166 | 497 | StorageState.Any) | 497 | StorageState.Any, 2, 0) |
167 | 498 | self.assertEquals(result, ["i1", "i3"]) | ||
168 | 499 | |||
169 | 500 | def testRelatedForEventsSortRecency(self): | ||
170 | 501 | import_events("test/data/apriori_events.js", self.engine) | ||
171 | 502 | result = self.engine.find_related_uris( | ||
172 | 503 | TimeRange.always(), [Event.new_for_values(subject_uri = "i2")], [], | ||
173 | 504 | StorageState.Any, 2, 1) | ||
174 | 498 | self.assertEquals(result, ["i3", "i1"]) | 505 | self.assertEquals(result, ["i3", "i1"]) |
175 | 499 | 506 | ||
176 | 500 | def testRelatedForMultipleEvents(self): | 507 | def testRelatedForMultipleEvents(self): |
177 | 501 | import_events("test/data/apriori_events.js", self.engine) | 508 | import_events("test/data/apriori_events.js", self.engine) |
178 | 502 | result = self.engine.find_related_uris( | 509 | result = self.engine.find_related_uris( |
179 | 503 | TimeRange.always(), [Event.new_for_values(subject_uri = "i1"), | 510 | TimeRange.always(), [Event.new_for_values(subject_uri = "i1"), |
183 | 504 | Event.new_for_values(subject_uri = "i4")], | 511 | Event.new_for_values(subject_uri = "i4")], [], |
184 | 505 | [], StorageState.Any), | 512 | StorageState.Any, 2, 0), |
185 | 506 | self.assertEquals(result, (["i3", "i2"],)) | 513 | self.assertEquals(result, (["i2", "i3"],)) |
186 | 507 | 514 | ||
187 | 508 | def testRelatedForEventsWithManifestation(self): | 515 | def testRelatedForEventsWithManifestation(self): |
188 | 509 | import_events("test/data/apriori_events.js", self.engine) | 516 | import_events("test/data/apriori_events.js", self.engine) |
189 | 510 | result = self.engine.find_related_uris(TimeRange.always(), | 517 | result = self.engine.find_related_uris(TimeRange.always(), |
190 | 511 | [Event.new_for_values(subject_uri = "i4")], | 518 | [Event.new_for_values(subject_uri = "i4")], |
191 | 512 | [Event.new_for_values(subject_manifestation="stfu:File")], | 519 | [Event.new_for_values(subject_manifestation="stfu:File")], |
194 | 513 | StorageState.Any) | 520 | StorageState.Any, |
195 | 514 | self.assertEquals(result, ["i3", "i5", "i1"]) | 521 | 10, 0) |
196 | 522 | self.assertEquals(result, ["i1", "i5", "i3"]) | ||
197 | 515 | 523 | ||
198 | 516 | if __name__ == "__main__": | 524 | if __name__ == "__main__": |
199 | 517 | unittest.main() | 525 | unittest.main() |
200 | 518 | 526 | ||
201 | === modified file 'test/remote-test.py' | |||
202 | --- test/remote-test.py 2010-02-16 09:43:31 +0000 | |||
203 | +++ test/remote-test.py 2010-02-25 18:14:12 +0000 | |||
204 | @@ -224,9 +224,9 @@ | |||
205 | 224 | 224 | ||
206 | 225 | def callback(uris): | 225 | def callback(uris): |
207 | 226 | mainloop.quit() | 226 | mainloop.quit() |
209 | 227 | self.assertEquals(uris, ["i3", "i2", "i1", "i5"]) | 227 | self.assertEquals(uris, ["i2", "i1", "i5", "i3"]) |
210 | 228 | 228 | ||
212 | 229 | result = self.client.find_related_uris_for_uris(["i4"], callback) | 229 | result = self.client.find_related_uris_for_uris(["i4"], callback, num_events=4, result_type=0) |
213 | 230 | mainloop.run() | 230 | mainloop.run() |
214 | 231 | 231 | ||
215 | 232 | def testFindEventsForValues(self): | 232 | def testFindEventsForValues(self): |
216 | 233 | 233 | ||
217 | === modified file 'zeitgeist/client.py' | |||
218 | --- zeitgeist/client.py 2010-02-16 09:43:31 +0000 | |||
219 | +++ zeitgeist/client.py 2010-02-25 18:14:12 +0000 | |||
220 | @@ -670,8 +670,8 @@ | |||
221 | 670 | error_handler=self._safe_error_handler(error_handler)) | 670 | error_handler=self._safe_error_handler(error_handler)) |
222 | 671 | 671 | ||
223 | 672 | def find_related_uris_for_events(self, event_templates, uris_reply_handler, | 672 | def find_related_uris_for_events(self, event_templates, uris_reply_handler, |
226 | 673 | time_range = None, result_event_templates=[], | 673 | error_handler=None, time_range = None, result_event_templates=[], |
227 | 674 | storage_state=StorageState.Any, error_handler=None): | 674 | storage_state=StorageState.Any, num_events=10, result_type=0): |
228 | 675 | """ | 675 | """ |
229 | 676 | Warning: This API is EXPERIMENTAL and is not fully supported yet. | 676 | Warning: This API is EXPERIMENTAL and is not fully supported yet. |
230 | 677 | 677 | ||
231 | @@ -690,6 +690,10 @@ | |||
232 | 690 | as subjects of events matching these templates | 690 | as subjects of events matching these templates |
233 | 691 | :param storage_state: The returned URIs must have this | 691 | :param storage_state: The returned URIs must have this |
234 | 692 | :class:`storage state <zeitgeist.datamodel.StorageState>` | 692 | :class:`storage state <zeitgeist.datamodel.StorageState>` |
235 | 693 | :param num_events: number of related uris you want to have returned | ||
236 | 694 | :param result_type: sorting of the results by | ||
237 | 695 | 0 for relevancy | ||
238 | 696 | 1 for recency | ||
239 | 693 | :param error_handler: An optional callback in case of errors. | 697 | :param error_handler: An optional callback in case of errors. |
240 | 694 | Must take a single argument being the error raised by the | 698 | Must take a single argument being the error raised by the |
241 | 695 | server. The default behaviour in case of errors is to call | 699 | server. The default behaviour in case of errors is to call |
242 | @@ -704,15 +708,16 @@ | |||
243 | 704 | time_range = TimeRange.always() | 708 | time_range = TimeRange.always() |
244 | 705 | 709 | ||
245 | 706 | self._iface.FindRelatedUris(time_range, event_templates, | 710 | self._iface.FindRelatedUris(time_range, event_templates, |
247 | 707 | result_event_templates, storage_state, | 711 | result_event_templates, storage_state, num_events, result_type, |
248 | 708 | reply_handler=self._safe_reply_handler(uris_reply_handler), | 712 | reply_handler=self._safe_reply_handler(uris_reply_handler), |
249 | 709 | error_handler=self._safe_error_handler(error_handler, | 713 | error_handler=self._safe_error_handler(error_handler, |
250 | 710 | uris_reply_handler, | 714 | uris_reply_handler, |
252 | 711 | [])) | 715 | []) |
253 | 716 | ) | ||
254 | 712 | 717 | ||
255 | 713 | def find_related_uris_for_uris(self, subject_uris, uris_reply_handler, | 718 | def find_related_uris_for_uris(self, subject_uris, uris_reply_handler, |
256 | 714 | time_range=None, result_event_templates=[], | 719 | time_range=None, result_event_templates=[], |
258 | 715 | storage_state=StorageState.Any, error_handler=None): | 720 | storage_state=StorageState.Any, num_events=10, result_type=0, error_handler=None): |
259 | 716 | """ | 721 | """ |
260 | 717 | Warning: This API is EXPERIMENTAL and is not fully supported yet. | 722 | Warning: This API is EXPERIMENTAL and is not fully supported yet. |
261 | 718 | 723 | ||
262 | @@ -728,6 +733,8 @@ | |||
263 | 728 | time_range=time_range, | 733 | time_range=time_range, |
264 | 729 | result_event_templates=result_event_templates, | 734 | result_event_templates=result_event_templates, |
265 | 730 | storage_state=storage_state, | 735 | storage_state=storage_state, |
266 | 736 | num_events = num_events, | ||
267 | 737 | result_type = result_type, | ||
268 | 731 | error_handler=error_handler) | 738 | error_handler=error_handler) |
269 | 732 | 739 | ||
270 | 733 | def install_monitor (self, time_range, event_templates, | 740 | def install_monitor (self, time_range, event_templates, |
271 | 734 | 741 | ||
272 | === modified file 'zeitgeist/datamodel.py' | |||
273 | --- zeitgeist/datamodel.py 2010-01-30 13:35:22 +0000 | |||
274 | +++ zeitgeist/datamodel.py 2010-02-25 18:14:12 +0000 | |||
275 | @@ -4,7 +4,7 @@ | |||
276 | 4 | # | 4 | # |
277 | 5 | # Copyright © 2009 Mikkel Kamstrup Erlandsen <mikkel.kamstrup@gmail.com> | 5 | # Copyright © 2009 Mikkel Kamstrup Erlandsen <mikkel.kamstrup@gmail.com> |
278 | 6 | # Copyright © 2009 Markus Korn <thekorn@gmx.de> | 6 | # Copyright © 2009 Markus Korn <thekorn@gmx.de> |
280 | 7 | # Copyright © 2009 Seif Lotfy <seif@lotfy.com> | 7 | # Copyright © 2009-2010 Seif Lotfy <seif@lotfy.com> |
281 | 8 | # Copyright © 2009-2010 Siegfried-Angel Gevatter Pujals <rainct@ubuntu.com> | 8 | # Copyright © 2009-2010 Siegfried-Angel Gevatter Pujals <rainct@ubuntu.com> |
282 | 9 | # | 9 | # |
283 | 10 | # This program is free software: you can redistribute it and/or modify | 10 | # This program is free software: you can redistribute it and/or modify |
284 | @@ -532,7 +532,7 @@ | |||
285 | 532 | "ordered ascendently by the popularity of the actor")) | 532 | "ordered ascendently by the popularity of the actor")) |
286 | 533 | MostRecentActor = enum_factory(("The last event of each different actor")) | 533 | MostRecentActor = enum_factory(("The last event of each different actor")) |
287 | 534 | LeastRecentActor = enum_factory(("The first event of each different actor")) | 534 | LeastRecentActor = enum_factory(("The first event of each different actor")) |
289 | 535 | 535 | ||
290 | 536 | class Subject(list): | 536 | class Subject(list): |
291 | 537 | """ | 537 | """ |
292 | 538 | Represents a subject of an :class:`Event`. This class is both used to | 538 | Represents a subject of an :class:`Event`. This class is both used to |
This branch fixes https:/ /bugs.edge. launchpad. net/zeitgeist/ +bug/498878
Please take stabs at it